python处理kafka数据
1、程序作用:从多个topic中读取数据--处理数据--写入新的kafkatopic中
pip3 install kafka-python
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 | import json from kafka import KafkaProducer from kafka import KafkaConsumer #消费者 def comsum(): print( 'start consumer' ) consumer = KafkaConsumer(group_id= '1' , bootstrap_servers=[ "100.98.100.186:9092" ], ) print(consumer) consumer.subscribe([ 'test_topic' , 'topic2' ]) for message in consumer: rs=json.loads(message.value) # 这样就可以通过topic和value去做后边的操作了 #处理数据生成新的数据 dict1 = {} dict1[ 'name' ] = rs[ 'name' ] dict1[ 'msg' ] = rs[ 'msg' ] print(dict1) #生产新数据 kfk_produce(dict1) print( 'end consumer' ) #生产者 def kfk_produce(data_dict): "" " 发送 string 格式数据 : return : "" " producer = KafkaProducer(bootstrap_servers= '100.98.100.186:9092' ) msg: str = json.dumps(data_dict).encode( 'utf-8' ) producer.send( 'new_topic' , msg, partition=0) producer.close() if __name__ == '__main__' : comsum() |
2、运行环境
1 | docker run --name python- cron -d wukc /python3-cron :latest |
3、执行方式
1 | nohup python pyhon_kafka.py >kafka.log 2>&1 & |
做一个决定,并不难,难的是付诸行动,并且坚持到底。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· C#/.NET/.NET Core优秀项目和框架2025年2月简报
· Manus爆火,是硬核还是营销?
· 终于写完轮子一部分:tcp代理 了,记录一下
· 【杭电多校比赛记录】2025“钉耙编程”中国大学生算法设计春季联赛(1)