很多小伙伴想知道在python中如何连接kafka?今天实现方法它来了!小编就通过这篇文章来给大家分享一个python连接kafka的操作方法,如果有兴趣的小伙伴一定要耐心阅读完这篇文章 。
文章插图
python连接kafka的详细方法如下:
(1)首先我们需要kafka-python安装:
# PyPI安装 pip install kafka-python # conda安装 conda install -c conda-forge kafka-python # anaconda自带pip安装 /root/anaconda3/bin/pip install kafka-python(2)kafka-python生产者
producer.py
#!/usr/bin/env python # -*- coding: utf-8 -*- import datetime import json import time import uuid from kafka import KafkaProducer from kafka.errors import KafkaError producer = KafkaProducer(bootstrap_servers='100.69.222.221:9092,100.69.222.222:9092,100.69.222.223:9092') topic = 'test_20181105' def test(): print('begin') try: n = 0 while True: dic = {} dic['id'] = n n = n + 1 dic['myuuid'] = str(uuid.uuid4().hex) dic['time'] = datetime.datetime.now().strftime("%Y%m%d %H:%M:%S") producer.send(topic, json.dumps(dic).encode()) print("send:" + json.dumps(dic)) time.sleep(0.5) except KafkaError as e: print(e) finally: producer.close() print('done') if __name__ == '__main__': test()【python如何连接kafka?这篇文章教会你连接方法】服务器集群中配置好Kafka, 修改上面程序中的ip地址和端口号, 执行python脚本就可以成功将消息发送到 topic: test_20181105
send:{"id": 1411, "myuuid": "a25a3d0361f94d3b8fffd5967ab5df01", "time": "20181105 16:11:14"} send:{"id": 1412, "myuuid": "784efd5389564194941240dca66233b6", "time": "20181105 16:11:14"} send:{"id": 1413, "myuuid": "6a211195319e447aa559614662f70590", "time": "20181105 16:11:15"} send:{"id": 1414, "myuuid": "2cc45bd82baf4a1cb41ea4786e50a0df", "time": "20181105 16:11:15"} send:{"id": 1415, "myuuid": "b7dfed4919c74164b83cf3ec28e257b6", "time": "20181105 16:11:16"} send:{"id": 1416, "myuuid": "9218eceb17834c228f5ab01ca7595272", "time": "20181105 16:11:16"} send:{"id": 1417, "myuuid": "c2751c54c390453f9eedd417fb1e5a31", "time": "20181105 16:11:17"} send:{"id": 1418, "myuuid": "9bbc4ef2cfbb42148332eb979b1142cb", "time": "20181105 16:11:17"} send:{"id": 1419, "myuuid": "f4998a862494445c976137793b55ed73", "time": "20181105 16:11:18"}
推荐阅读
- anaconda如何打开创建的虚拟环境?这篇文章教会你详细操作
- Python能不能用怎么测试?Python怎么样才算安装好了
- Python安装位置怎么去找?Python默认安装在哪里
- 怎么在命令提示符中运行python程序?超详细的方法来了
- 京东双十一能分期付款吗?如何分期付款?
- 京东双十一活动方案如何操作?附详细活动方案
- 拼多多商品上架了多久补单?如何补?
- 淘宝如何安全补单?附流程步骤
- 拼多多选词公式怎么计算?如何选词?
- ps如何制作绚烂的烟花文字效果?ps怎么设计烟花文字效果?