I have a kafka topic where I have to find number of messages present in a topic and number of partitions present in the topic using Java
2 Answers
You can use kafkacat command to find above information
kafkacat -L -b brokerip -> this command will show the metadata. You can find the number of partitions for each topic
kafkacat -C -b brokerip -t topicname -e -q | \ wc -l -> with this command you can find the number of messages. The alternate ways is you can check the last offset from given topic to find total number of messages

- 13
- 5
-
OP requested to find out using java – Ran Lupovich Jul 28 '21 at 07:13
https://stackoverflow.com/a/60124779/11609323
Above is example of using the metrics() api to get the lag, the lag represents the endOffsets minus currentOffset , you could get the startOffset as well as a metric() , calculating the endOffset minus startOffset will get you the number of records currently in topic's partition, sum the number of records in each partition to get total number of records in the topic,
Notice StartOffset not always be 0, as you might have retention setting pruning old messages
P.S this is how we show this information to developers in our company , we found it as better way then setting dummy consumer group for beginning and reset it

- 1,655
- 1
- 6
- 13