简体   繁体   English

使用来自 KAFKA 主题的数据并从中提取字段并使用 python 存储在 MySQL 中

[英]Consume data from KAFKA topic and extract fields from it and store in MySQL using python

I want to consume data from a Kafka topic with the following command as follows:我想使用以下命令使用来自 Kafka 主题的数据,如下所示:

bin/kafka-console-consumer.sh --bootstrap-server localhost:9092 --topic myTestTopic --from-beginning bin/kafka-console-consumer.sh --bootstrap-server localhost:9092 --topic myTestTopic --from-beginning

Then this will output the following (just pasting top 2 lines output, but it will be many lines...):然后这将输出以下内容(仅粘贴前 2 行输出,但它将是多行...):

&time=1561768216000&gameCategory=PINPOINT&category=ONE&uniqueId=2518Z-0892A-0030O-16H70&transactionType=CRD&familyId=000-222-115-11119&realTs=1561768319000&sortId=1&msg=SET-UP+PRAYER+%26+intercession+begins+in+just+30+minutes.&remoteIpAddress=127.0.0.1&userAgent=HTTP&
&uniqueId=872541806296826880&time=1571988786000&gameCategory=NOTIFY&category=TWO&transactionType=CRD&familyId=401-222-115-89387&sortId=1&realTs=1571988989000&msg=This-is+a+reminder.&remoteIpAddress=127.0.0.1&userAgent=HTTPS&

I want to consume the following from the output:我想从输出中使用以下内容:

  • realTs房地产

  • familyId家庭 ID

  • msg留言

  • uniqueId唯一身份

and you can see that each element is seperated by an ampersand ('&').并且您可以看到每个元素都由与号 ('&') 分隔。 They are not always in same index/place so I'm not sure if I need a regex?它们并不总是在相同的索引/位置,所以我不确定是否需要正则表达式? Eventually when I do the query on a local running MySQL, i'd see this:最终,当我在本地运行的 MySQL 上进行查询时,我会看到:

describe testTable;描述测试表;

+----------+--------------+------+-----+---------+-------+
| Field    | Type         | Null | Key | Default | Extra |
+----------+--------------+------+-----+---------+-------+
| realTs   | bigint(20)   | YES  |     | NULL    |       |
| familyId | varchar(255) | YES  |     | NULL    |       |
| msg      | text         | YES  |     | NULL    |       |
| uniqueId | varchar(255) | YES  |     | NULL    |       |
+----------+--------------+------+-----+---------+-------+
4 rows in set (0.00 sec)

SELECT * FROM testTable; SELECT * FROM testTable;

+---------------+-------------------+-----------------------------------------------------------+-------------------------+
| realTs        | familyId          | msg                                                       | uniqueId                |
+---------------+-------------------+-----------------------------------------------------------+-------------------------+
| 1561768319000 | 000-222-115-11119 | SET-UP+PRAYER+%26+intercession+begins+in+just+30+minutes. | 2518Z-0892A-0030O-16H70 |
| 1571988989000 | 401-222-115-89387 | This-is+a+reminder.                                       | 872541806296826880      |
+---------------+-------------------+-----------------------------------------------------------+-------------------------+

What do I have so far?到目前为止我有什么? I have a mysql-connector with python where I can connect to a local mysql etc, but i'm struggling with parsing this and inserting it...我有一个带有 python 的 mysql 连接器,我可以在其中连接到本地 mysql 等,但是我正在努力解析它并插入它......

With Python, you can use urllib.parse.parse_qs to retrieve URL query string components in a Python dictionary that you can iterate later to insert data in your MySQL database.使用 Python,您可以使用urllib.parse.parse_qs在 Python 字典中检索 URL 查询字符串组件,您可以稍后迭代以在 MySQL 数据库中插入数据。

For instance:例如:

from urllib.parse import parse_qs
line = "&time=1561768216000&gameCategory=PINPOINT&category=ONE&uniqueId=2518Z-0892A-0030O-16H70&transactionType=CRD&familyId=000-222-115-11119&realTs=1561768319000&sortId=1&msg=SET-UP+PRAYER+%26+intercession+begins+in+just+30+minutes.&remoteIpAddress=127.0.0.1&userAgent=HTTP&uniqueId=872541806296826880&time=1571988786000&gameCategory=NOTIFY&category=TWO&transactionType=CRD&familyId=401-222-115-89387&sortId=1&realTs=1571988989000&msg=This-is+a+reminder.&remoteIpAddress=127.0.0.1&userAgent=HTTPS&"

o = parse_qs(line)

print(o)

Result:结果:

{'time': ['1561768216000', '1571988786000'], 'gameCategory': ['PINPOINT', 'NOTIFY'], 'category': ['ONE', 'TWO'], 'uniqueId': ['2518Z-0892A-0030O-16H70', '872541806296826880'], 'transactionType': ['CRD', 'CRD'], 'familyId': ['000-222-115-11119', '401-222-115-89387'], 'realTs': ['1561768319000', '1571988989000'], 'sortId': ['1', '1'], 'msg': ['SET-UP PRAYER & intercession begins in just 30 minutes.', 'This-is a reminder.'], 'remoteIpAddress': ['127.0.0.1', '127.0.0.1'], 'userAgent': ['HTTP', 'HTTPS']}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用 Python 使用来自 Confluent Kafka 主题的数据并退出 - Consume Data from Confluent Kafka Topic and Exit using Python 无法使用 confluent-kafka-python 从 Kafka Topic 消费到新的消费者组 - Cannot consume from Kafka Topic using confluent-kafka-python to a new consumer-group 从 Kafka 主题中提取特定数据 - Extract particular data from Kafka topic 使用 Python 从特定的 Kafka 主题中读取 - Read from specific Kafka topic using Python 将数据从一个 Kafka 主题移动到另一个主题在 Kafka Python 中不起作用 - Moving data from one Kafka topic to other is not working in Kafka Python kafka-python:通过运行并发进程/脚本同时产生和消费来自同一主题的消息 - kafka-python: produce and consume messages from same topic at the same time by running concurrent process/scripts 微服务可以向/从同一个 kafka 主题发布和消费吗 - Can a microservice publish and consume to/from same kafka topic 如何在python中使用来自kafka的多个代理的数据? - How to consume data from multiple brokers of kafka in python? 如何使用Spark Streaming和Python使用来自Kafka的JSON记录? - How to consume JSON records from Kafka using Spark Streaming and Python? 如何使用 Kafka 从/向 Docker 生产/消费数据? - How to produce/consume data from/to Docker using Kafka?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM