栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

python 消费kafka数据

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

python 消费kafka数据

安装kafka 库

pip install kafka-python

消费 topic 下所有数据

from kafka import KafkaConsumer, TopicPartition


# 创建消费者
consumer = KafkaConsumer(bootstrap_servers='host:port')
topic = 'test_topic'
# 获取topic的分区
partition = list(consumer.partitions_for_topic(topic=topic))[0]
print(partition)
topic_partition = TopicPartition(topic=topic, partition=partition)
# 指定消费的topic
consumer.assign([topic_partition])
# 获取 offset 的起始位置和结束位置
start_offset = consumer.beginning_offsets([topic_partition])[topic_partition]
end_offset = consumer.end_offsets([topic_partition])[topic_partition]
print(start_offset)
print(end_offset)
# 将 offset 重置到起始位置,也可以重置到任意位置 consumer.seek()
consumer.seek_to_beginning((topic_partition))
# end_offset 是对应没有数据的 offset 消费会被阻塞, 消费到end_offset - 1就已经获取全部数据了
for i in range(start_offset, end_offset):
    msg = next(consumer)
    print(msg)
    print(msg.value)

持续消费kafka中的数据

# 没有数据的时候会被阻塞,直到有可以消费的数据
for msg in consumer:
    print(msg)
    print(msg.value)

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/1026764.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号