如何在 Python 中使用 kafka 客户端描述主题

Ash*_*win 3 python kafka-python

我是 python 中 kafka 客户端的初学者,我需要一些帮助来描述使用客户端的主题。

我能够使用以下代码列出我所有的 kafka 主题:-

consumer = kafka.KafkaConsumer(group_id='test', bootstrap_servers=['kafka1'])
topicList = consumer.topics()
Run Code Online (Sandbox Code Playgroud)

Ash*_*win 8

在参考了多篇文章和代码示例后,我能够使用 confluent_kafka 通过 describe_configs 来做到这一点。

链接 1 [Confluent-kafka-python] 链接 2 Git 示例

下面是我的示例代码!!

from confluent_kafka.admin import AdminClient, NewTopic, NewPartitions, ConfigResource
import confluent_kafka
import concurrent.futures

#Creation of config
conf = {'bootstrap.servers': 'kafka1','session.timeout.ms': 6000}
adminClient = AdminClient(conf)
topic_configResource = adminClient.describe_configs([ConfigResource(confluent_kafka.admin.RESOURCE_TOPIC, "myTopic")])
    for j in concurrent.futures.as_completed(iter(topic_configResource.values())):
        config_response = j.result(timeout=1)
Run Code Online (Sandbox Code Playgroud)


小智 5

我已经找到了如何用 kafka-python 做到这一点:

from kafka.admin import KafkaAdminClient, ConfigResource, ConfigResourceType
KAFKA_URL = "localhost:9092" # kafka broker
KAFKA_TOPIC = "test" # topic name

admin_client = KafkaAdminClient(bootstrap_servers=[KAFKA_URL])
configs = admin_client.describe_configs(config_resources=[ConfigResource(ConfigResourceType.TOPIC, KAFKA_TOPIC)])
config_list = configs.resources[0][4]
Run Code Online (Sandbox Code Playgroud)

config_list(元组列表)中,您拥有该主题的所有配置。