老版本kafka查询topic消费情况(python查询)

由于老版本的kafka缺少shell,导致无法通过命令直接进行查询,所以通过python代码,实现消费情况查询

安装必须的包

shell 复制代码
#pyhon2.5
pip install kafka-python==1.4.7

python脚本

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)
相关推荐
赵长辉13 分钟前
AGI-rag学习:ChromaDB使用1,txt类型文档【20251016课复习】
python·学习·llm·agi
じ☆冷颜〃4 小时前
分布式系统中网络技术的演进与异构融合架构(HFNA)
笔记·python·物联网·设计模式·架构·云计算
夜思红尘6 小时前
算法--双指针
python·算法·剪枝
人工智能训练6 小时前
OpenEnler等Linux系统中安装git工具的方法
linux·运维·服务器·git·vscode·python·ubuntu
智航GIS7 小时前
8.2 面向对象
开发语言·python
蹦蹦跳跳真可爱5898 小时前
Python----大模型(GPT-2模型训练加速,训练策略)
人工智能·pytorch·python·gpt·embedding
xwill*8 小时前
π∗0.6: a VLA That Learns From Experience
人工智能·pytorch·python
还不秃顶的计科生8 小时前
LeetCode 热题 100第二题:字母易位词分组python版本
linux·python·leetcode
weixin_462446239 小时前
exo + tinygrad:Linux 节点设备能力自动探测(NVIDIA / AMD / CPU 安全兜底)
linux·运维·python·安全
不瘦80斤不改名9 小时前
Python 日志(logging)全解析
服务器·python·php