Gopi Krishnan
Gopi Krishnan

Reputation: 31

Find total number of topic in kafka cluster

I have a kafka cluster with 3 brokers, I would like to know the total number of topics in the cluster.

Is there a script or any command to find out the total.

Upvotes: 2

Views: 1534

Answers (1)

Mickael Maison
Mickael Maison

Reputation: 26875

There are multiple ways to find this out:

  • AdminClient API: You can use listTopics() and just call size() on names().
  • kafka-topics.sh tool: You can use ./kafka-topics.sh --zookeeper localhost --list | wc -l.

A topic is just a group of partitions. For Kafka it's the same if you have 1 topics with 10 partitions or 10 topics with 1 partition each. However, 10 topics with 1 partitions is not the same as 10 topics with 10 partitions each. That's why, in practice, you are usually interested in the number of partitions.

To count partitions:

  • Metrics: Brokers expose a metric called kafka.server:type=ReplicaManager,name=PartitionCount
  • kafka-topics.sh tool: You can use ./kafka-topics.sh --zookeeper localhost --describe | grep "Partition:" | wc -l
  • AdminClient API: You first need to get the topic list (via listTopics()) and then call describeTopics(). The number of partitions for each topic is available in the TopicDescription object you get back.

Upvotes: 2

Related Questions