老版本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)
相关推荐
GPU实战笔记8 小时前
云端 Python 开发:JupyterLab 还是 VS Code Remote-SSH?
python·vs code·jupyterlab·remote-ssh·远程开发
狗狗狗狗狗乐啊8 小时前
搭一个 AI 对话工作台 AChat:从 0 到可用的完整记录(一)
python·react.js·ai编程
白猫不黑9 小时前
Python实现简易Web弱口令爆破与防护方案
python·web安全·计算机·网络安全·黑客·信息安全·渗透测试
kuuailetianzi9 小时前
Python初识:定位、优势与发展历程
python
guoran_shini10 小时前
LLM微调-训练垂类问答模型
python·lora·sft
basketball61610 小时前
Python FastAPI 介绍以及常用方法
python·fastapi·vllm·ai infra
论文复现现场10 小时前
企业知识库 RAG 用什么模型便宜?GLM-5.3-Flash 长文本 API 实测
python·rag·企业知识库·大模型api·glm-5.3-flash
梦在远山后10 小时前
从手写 Loop 到可恢复 Runtime:用 LangGraph、PostgreSQL Checkpoint 与 AG-UI 跑通中断恢复
python·langchain·agent
Yolanda_202211 小时前
8.tensorboard的使用
python