簡體   English   中英

使用 Kafka-Python 在 Kafka 中查找消費者組下的主題

[英]Find the topics under a consumer group in Kafka using Kafka-Python

我的 python 腳本的輸入是消費者組的名稱,output 應該是消費者組下的主題列表。
我的代碼目前不返回當前偏移量為 -1 的主題。
有沒有更好的方法,我可以通過Kafka-python獲取消費者組下所有主題的列表。
我實際上正在尋找使用 Kafka 工具替換描述組 cmd 中的主題。

from kafka import KafkaAdminClient
topics_in_groups = {};
client = KafkaAdminClient(bootstrap_server='localhost:9092')
for group in client.list_consumer_groups():
  topics_in_groups[group[0]] = [];
  topic_dict = client.list_consumer_group_offsets(group[0]);
  for topic in topic_dict:
    topics_in_groups[group[0]].append(topic.topic)

client.list_consumer_group_offsets(group[0])將為您獲取消費者組的元數據,以下代碼將為您提供組的主題和分區列表。

topics_in_groups = []
for topic,prtitions in topic_dict:
    topics_in_groups.append({"topic":topic, "partition": partition})

我試過這個並且工作。

from kafka import KafkaAdminClient
topics_in_groups = {}
client = KafkaAdminClient(bootstrap_servers=['broker:9092'])

for group in client.list_consumer_groups():
    topics_in_groups[group[0]] = []

for group in topics_in_groups.keys():
    my_topics = []
    topic_dict = client.list_consumer_group_offsets(group)
    for topic in topic_dict:
        my_topics.append(topic.topic)
        topics_in_groups[group] = list(set(my_topics))

for key , value in topics_in_groups.items():
    print(key, "\n\t", value)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM