I'm using 0.9.0.0 version of Kafka and I want to count the number of messages in a topic without using the admin script kafka-console-consumer.sh.
I have tried all the commands in the answer Java, How to get number of messages in a topic in apache kafka but none are yielding the result. Can anyone help me out here?
You could try to execute the command below:
Then, sum up all the counts for each partition.
Updated: Java implementation
You can also do this using awk and a simple loop
Technically speaking you can simply consume all messages from the topic and count them:
Example:
However
kafka.tools.GetOffsetShell
approach will give you the offsets and not the actual number of messages in the topic. It means if the topic gets compacted you will get two differed numbers if you count messages by consuming them or by reading offsets.Topic compaction: https://kafka.apache.org/documentation.html#design_compactionbasics
you can sum up all counts by using this :