由于老版本的kafka缺少shell,导致无法通过命令直接进行查询,所以通过python代码,实现消费情况查询
安装必须的包
#pyhon2.5
pip install kafka-python==1.4.7
python脚本
#!/usr/bin/env python
import sys
from kafka import KafkaConsumer, TopicPartition
if len(sys.argv) != 2:
print("Usage: python queryConsumer.py <topic_name>")
sys.exit(1)
bootstrap_servers = ['localhost:9092']
topic_name = sys.argv[1]
consumer = KafkaConsumer(
bootstrap_servers=bootstrap_servers,
auto_offset_reset='earliest',
enable_auto_commit=False,
consumer_timeout_ms=1000
)
partitions = consumer.partitions_for_topic(topic_name)
print("Partitions: ", partitions)
for partition in partitions:
tp = TopicPartition(topic_name, partition)
consumer.assign([tp])
consumer.seek_to_end(tp)
last_offset = consumer.position(tp)
print("Partition: ", partition, "Last offset: ", last_offset)
consumer.close()
查询
[root@vm_ tools]# python queryConsumer.py test
('Partitions: ', set([0]))
('Partition: ', 0, 'Last offset: ', 1)