Kafka-python retrieve the list of topics
Asked Answered
M

3

23

I'm using kafka-python and I'm wondering if there is a way for showing all the topics.

Something like this:

./bin/kafka-topics.sh --list --zookeeper localhost:2181
Multiphase answered 15/4, 2016 at 8:15 Comment(2)
did you try with kafka-python.readthedocs.org/en/1.0.2/apidoc/… ?Brewhouse
Many thanks, is what i was looking for. If you want to write an answer I will mark it as correct. Maybe could be useful also for others.Multiphase
D
57
import kafka
consumer = kafka.KafkaConsumer(group_id='test', bootstrap_servers=['server'])
consumer.topics()
Denote answered 27/7, 2017 at 10:20 Comment(1)
Sounds good, doesn't work "ValueError: Topic name "*" is illegal, it contains a character other than ASCII alphanumerics, ".", "_" and "-""Soucy
E
5

You need to use the KafkaAdminClient, not KafkaConsumer:

import kafka
admin_client = kafka.KafkaAdminClient(bootstrap_servers=['server'])
admin_client.list_topics()
Extrusion answered 29/3, 2022 at 22:42 Comment(0)
B
1

Try with the method KafkaConsumer.topics().

Brewhouse answered 15/4, 2016 at 8:35 Comment(2)
This does not seem to poll Kafka for the available topics, only lists the topics that a consumer instance is subscribed to.Forepleasure
This is not a static method, so must create an instance of KafkaConsumer to use it, but it turns out you can do that without a topic list, and it will in fact list the topics from kafkaForepleasure

© 2022 - 2024 — McMap. All rights reserved.