Skip to main content
ahallam
Gold ⭐️
March 16, 2018

Script : How to count the total number of partitions Kafka cluster.

  • October 5, 2026 at 9:12 AM
  • 0 replies
  • 10926 views
  • Translate All

Quick command to find the total number of partitions in a Kafka cluster, it could help for example in Mirror Maker sizing.
Please replace ZK_SERVER values with your cluster details.

cd /tmp
zookeeper="ZK_SERVER1:2181,ZK_SERVER2:2181,ZK_SERVER3:2181"
sum=0
for i in $(/usr/hdp/current/kafka-broker/bin/kafka-topics.sh --list --zookeeper $zookeeper ); do count=$(/usr/hdp/current/kafka-broker/bin/kafka-topics.sh --describe --zookeeper $zookeeper --topic $i |grep Leader | wc -l); sum=`expr $sum + $count` ; echo 'total partitions is ' $sum; done

If you want to count partitions with specific filter on the name for Topics

zookeeper="ZK_SERVER1:2181,ZK_SERVER2:2181,ZK_SERVER3:2181"
sum=0
for i in $(/usr/hdp/current/kafka-broker/bin/kafka-topics.sh --list --zookeeper $zookeeper | grep 'FILTER'); do count=$(/usr/hdp/current/kafka-broker/bin/kafka-topics.sh --describe --zookeeper $zookeeper --topic $i |grep Leader | wc -l); sum=`expr $sum + $count` ; echo 'total partitions is ' $sum; done