Reputation: 352
I am new to Kafka. I have created a producer using java and sending some messages to a topic. Is there a way to programmatically find the number of messages in a particular topic.
Please share your thoughts.
Upvotes: 2
Views: 1886
Reputation: 10065
What vahid sayd could be a solution but you can also use the following one :
bin/kafka-run-class.sh kafka.tools.GetOffsetShell --broker-list localhost:9092 --topic --time -1
then you have to sum the count values.
In any case please consider that my and vahid solutions don't work if log compaction is active because in this case there could be some "gaps" between offsets so calculating just the difference doesn't provide you the number of messages.
Upvotes: 1
Reputation: 1218
KafkaConsumer API provides interfaces that can help you. There is beginningOffsets(Collection<TopicPartition> partitions)
and endOffsets(Collection<TopicPartition> partitions)
. You can call these methods for all partitions in your topic, and then find the difference between end offset and beginning offset for each topic partition. Then aggregate across all topic partitions. That should give you the number of messages in the topic. (reference)
Upvotes: 2