欢迎您访问365答案网,请分享给你的朋友!
生活常识 学习资料

python3连接kafka并收发消息

时间:2023-04-18
一.发送消息到kafka topic

安装pykafka库

1.通过interpreter setting安装pykafka库

或者通过命令安装2.pip3 install pykafka

#!/usr/bin/python# -*- coding:utf-8 -*-from pykafka import KafkaClientclient = KafkaClient(hosts="10.XX0.XX0.XX4:9092") # 可接受多个client# 查看所有的topic# print(client.topics)topic = client.topics['test_78'] # 选择一个topicmessage = "test message2 test message2"with topic.get_sync_producer() as producer: producer.produce(bytes(message, encoding='utf8')) print(message)

二.消费kafka 的topic消息

安装kafka库,安装方式如上--pykafka库

#!/usr/bin/python# -*- coding:utf-8 -*-from kafka import KafkaConsumerkafka_topic = "test_78878"kafka_topic_group = "test_kafka_group"kafka_bootstrap_servers = ['10.XX0.XX0.XX4:9092']consumer = KafkaConsumer(kafka_topic,group_id=kafka_topic_group,bootstrap_servers=kafka_bootstrap_servers)for message in consumer: print ("%s:key=%s value=%s" % (message.topic, message.key,message.value))

Copyright © 2016-2020 www.365daan.com All Rights Reserved. 365答案网 版权所有 备案号:

部分内容来自互联网,版权归原作者所有,如有冒犯请联系我们,我们将在三个工作时内妥善处理。