Java, How to get number of messages in a topic in apache kafka

Chetan picture Chetan · Feb 18, 2015 · Viewed 137.6k times · Source

I am using apache kafka for messaging. I have implemented the producer and consumer in Java. How can we get the number of messages in a topic?

Answer

ssemichev picture ssemichev · Feb 15, 2016

It is not java, but may be useful

./bin/kafka-run-class.sh kafka.tools.GetOffsetShell 
  --broker-list <broker>:  <port> 
  --topic <topic-name> --time -1 --offsets 1 
  | awk -F  ":" '{sum += $3} END {print sum}'