python连接kafka

1.安装包

pip3 install kafka-python

2.消费者

from kafka import KafkaConsumer
import json

consumer = KafkaConsumer('sink',group_id='test',bootstrap_servers=['192.168.186.174:9092'])
for msg in consumer:
    recv = "%s:%d:%d: key=%s value=%s" % (msg.topic, msg.partition, msg.offset, msg.key, msg.value)
    print(recv)
    print("接收成功")

3.生产者

from kafka import KafkaProducer

producer = KafkaProducer(bootstrap_servers='192.168.186.174:9092')  # 连接kafka

msg = "Hello World".encode('utf-8')  # 发送内容,必须是bytes类型
producer.send('example', msg)  # 发送的topic为example
producer.close()
print("发送成功")

 

posted @ 2022-02-24 17:57  缘故为何  阅读(940)  评论(0编辑  收藏  举报