安装kafka-python

pip install kafka-python

代码

from kafka import KafkaConsumer, TopicPartition

bootstrap_servers = ['localhost:9092']
topic = 'test'
group_id = 'lll'

consumer = KafkaConsumer(
    group_id="my-group",
    bootstrap_servers=bootstrap_servers,
    auto_offset_reset='latest'
)

PARTITIONS = []
for partition in consumer.partitions_for_topic(topic):
    PARTITIONS.append(TopicPartition(topic, partition))

end_offsets = consumer.end_offsets(PARTITIONS)
print(end_offsets)

#输出
#{TopicPartition(topic='test', partition=2): 354915,
#TopicPartition(topic='test', partition=1): 354926,
#TopicPartition(topic='test', partition=0): 354892}

标签: kafka, python

添加新评论