javaapache-kafkamessagekafka-topic

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


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 particular topic?


Solution

  • The only way that comes to mind for this from a consumer point of view is to actually consume the messages and count them then.

    The Kafka broker exposes JMX counters for number of messages received since start-up but you cannot know how many of them have been purged already.

    In most common scenarios, messages in Kafka is best seen as an infinite stream and getting a discrete value of how many that is currently being kept on disk is not relevant. Furthermore things get more complicated when dealing with a cluster of brokers which all have a subset of the messages in a topic.