首页 > 解决方案 > 为什么数据不使用 python 发送到 KAFKA 主题?

问题描述

我不知道有什么问题。我从 MYSQL 数据库中读取数据并想将其发送到 Kafka。但是脚本运行没有问题并停留在发送时刻

def parse(d,param):  # распарс
    r={}
    if str(type(d)) == "<class 'dict'>":
        # print(d)
        return (d)
    return -1

s_con = pymysql.connect(host="xxx", user="xxx", password="xxx", port=3306,
                                db="xxx", cursorclass=pymysql.cursors.SSCursor)
s_cur = s_con.cursor()

s_cur.execute(s_sql) 
         
producer = KafkaProducer(bootstrap_servers=param["BOOTSTRAP_SERVERS"],
                                 value_serializer=lambda v: json.dumps(v).encode('utf-8'),)
print(producer)
while True:
    r = [dict((s_cur.description[i][0], value) \
                      for i, value in enumerate(row)) for row in s_cur.fetchmany(
                1000)] 

    for d in r:
       j = parse(d, param)
       print(j)
       print(param["TOPIC"])
       producer.send(param["TOPIC"], value=j)
       sleep(5)

结果如下

在此处输入图像描述

标签: pythonmysqlsqlapache-kafka

解决方案


推荐阅读