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
Amit Tripathi
  • 7,003
  • 6
  • 32
  • 58
teoreda
  • 2,392
  • 1
  • 21
  • 28
  • did you try with http://kafka-python.readthedocs.org/en/1.0.2/apidoc/KafkaConsumer.html#kafka.KafkaConsumer.topics ? – Markon Apr 15 '16 at 08:31
  • 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. – teoreda Apr 15 '16 at 08:33

3 Answers3

56
import kafka
consumer = kafka.KafkaConsumer(group_id='test', bootstrap_servers=['server'])
consumer.topics()
Ravindranath Akila
  • 209
  • 3
  • 35
  • 45
secfree
  • 4,357
  • 2
  • 28
  • 36
  • 1
    Sounds good, doesn't work "ValueError: Topic name "*" is illegal, it contains a character other than ASCII alphanumerics, ".", "_" and "-"" – Ravindranath Akila Oct 26 '17 at 07:29
3

You need to use the KafkaAdminClient, not KafkaConsumer:

import kafka
admin_client = kafka.KafkaAdminClient(bootstrap_servers=['server'])
admin_client.list_topics()
1

Try with the method KafkaConsumer.topics().

Markon
  • 4,480
  • 1
  • 27
  • 39
  • 1
    This does not seem to poll Kafka for the available topics, only lists the topics that a consumer instance is subscribed to. – Aaron McMillin Jan 19 '17 at 20:10
  • 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 kafka – Aaron McMillin Jan 20 '17 at 14:51