python获取kafka队列长度

复制代码
# pip install kafka-python

from kafka import KafkaConsumer
from kafka.structs import TopicPartition

def get_queue_length(topic_list, kafka_host, kafka_group):

    partition_list = [TopicPartition(it[1], 0) for it in topic_list]
    consumer = KafkaConsumer(bootstrap_servers=[kafka_host], group_id=kafka_group)
    consumer.assign(partition_list)
    off_set_dict = consumer.end_offsets(partition_list)
    latest_offset = list(off_set_dict.values())
    cursor_li = list(map(lambda x: consumer.position(x), partition_list))
    queue_len = sum(latest_offset) - sum(cursor_li)
    return queue_len


if __name__ == "__main__":
    # 订阅主题列表
    topic_list = ['test', ]
    # 消费组
    kafka_group = 'test_group'
    # 连接地址
    kafka_host = '127.0.0.1:9092'
    q_length = get_queue_length(topic_list, kafka_host, kafka_group)
    print(q_length)
复制代码

 

posted @   Wchime  阅读(21)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
点击右上角即可分享
微信分享提示