Python简单使用kafka
在Windows上安装kafka可以参考: https://blog.csdn.net/weixin_38004638/article/details/91893910
简介
Kafka是分布式流处理系统(RabbitMQ仅仅只是消息队列),是一个分布式、分区的、多副本的、多订阅者,基于Zookeeper协调的分布式日志系统。kafka的吞吐量是很高的,至于为什么这么高可以参考这篇文章 https://zhuanlan.zhihu.com/p/183808742
这里是我使用kafka后产生的消息文件
test-0分区
话不多说,上代码
生产者
from kafka import KafkaProducer, TopicPartition
import json
producer = KafkaProducer(bootstrap_servers=['127.0.0.1:9092'])
for i in range(3):
msg = {'asd':'azxc'}
msg1 = "zxc"
print(json.dumps(msg).encode())
producer.send(topic='test', value=msg1.encode(),partition=0,key='456'.encode())
producer.close()
消费者(简单使用)
from kafka import KafkaConsumer
consumer = KafkaConsumer('test', bootstrap_servers=['127.0.0.1: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)
消费者(指定分区)
from kafka import KafkaConsumer, TopicPartition
consumer = KafkaConsumer(bootstrap_servers=['127.0.0.1:9092'])
# 指定消费的主体与分区
partition = TopicPartition('test', 0)
consumer.assign([partition])
# 指定从分区的offset开始消费
consumer.seek(partition, 0)
for msg in consumer:
recv = "%s:%d:%d: key=%s value=%s" % (msg.topic, msg.partition, msg.offset, msg.key, msg.value)
print(recv)
本文来自博客园,作者:糖烤栗子&,转载请注明原文链接:https://www.cnblogs.com/grocerystore/p/15121101.html