[英]How to describe a topic using kafka client in Python
I'm beginner to kafka client in python, i need some help to describe the topics using the client.我是 python 中 kafka 客户端的初学者,我需要一些帮助来描述使用客户端的主题。
I was able to list all my kafka topics using the following code:-我能够使用以下代码列出我所有的 kafka 主题:-
consumer = kafka.KafkaConsumer(group_id='test', bootstrap_servers=['kafka1'])
topicList = consumer.topics()
After referring multiple articles and code samples, I was able to do this through describe_configs using confluent_kafka.在参考了多篇文章和代码示例后,我能够使用 confluent_kafka 通过 describe_configs 来做到这一点。
Link 1 [Confluent-kafka-python] Link 2 Git Sample链接 1 [Confluent-kafka-python]链接 2 Git 示例
Below is my sample code!!下面是我的示例代码!!
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)
I have found how to do it with kafka-python:我已经找到了如何用 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]
In config_list (list of tuples) you have all the configs for the topic.在config_list (元组列表)中,您拥有该主题的所有配置。
Refer: https://docs.confluent.io/current/clients/confluent-kafka-python/参考: https : //docs.confluent.io/current/clients/confluent-kafka-python/
kafka.admin.TopicMetadata.partitions provide: confluent_kafka.admin.PartitionMetadata (Partition id, leader, replicas, isrs) kafka.admin.TopicMetadata.partitions 提供:confluent_kafka.admin.PartitionMetadata(分区id、leader、replicas、isrs)
from confluent_kafka.admin import AdminClient kafka_admin = AdminClient({"bootstrap.servers": bootstrap_servers}) for topic in topics: x = kafka_admin.list_topics(topic=topic) print x.topics, '\\n' for key, value in x.topics.items(): for keyy, valuey in value.partitions.items(): print keyy, ' Partition id : ', valuey, 'leader : ', valuey.leader,' replica: ', valuey.replicas
Interestingly, for Java this functionality ( describeTopics()
) sits within the KafkaAdminCLient.java .有趣的是,对于 Java,此功能 (
describeTopics()
) 位于KafkaAdminCLient.java 中。
So, I was trying to look for the python equivalent of the same and I discovered the code repository of kafka-python .所以,我试图寻找相同的 python 等价物,我发现了kafka-python的代码库。
The documentation (in-line comments) in admin-client equivalent in kafka-python package says the following: kafka-python 包中的 admin-client 等效文档(内嵌注释)说明如下:
describe topics functionality is in ClusterMetadata Note: if implemented here, send the request to the controller
I then switched to cluster.py file in the same repository.然后我切换到同一个存储库中的cluster.py 文件。 This contains the
topics()
function that you've used to retrieve the list of topics and the following 2 functions that could help you achieve the describe
functionality:这包含您用来检索主题列表的
topics()
函数和以下两个可以帮助您实现describe
功能的函数:
partitions_for_topic()
- Return set of all partitions for topic (whether available or not) partitions_for_topic()
- 返回主题的所有分区集(无论是否可用)available_partitions_for_topic()
- Return set of partitions with known leaders available_partitions_for_topic()
- 返回具有已知领导者的分区集Note : I haven't tried this myself so I'm not entierly sure if the behaviour would be identical to what you would see in the result for kafka-topics --describe ...
command but worth a try.注意:我自己还没有尝试过,所以我不能完全确定行为是否与您在
kafka-topics --describe ...
命令的结果中看到的相同,但值得一试。
I hope this helps!我希望这有帮助!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.