![]() |
VOOZH | about |
Apache Kafka is the distributed event streaming platform capable of handling high throughput, low latency, and fault tolerance. One of the common tasks when working with Kafka is determining the number of messages on a specific topic. This article will guide you through the process of using Java to retrieve the number of messages in the Kafka topic.
To get the number of messages in the Kafka topic, we can use the AdminClient provided by Kafka. This client allows you to fetch the beginning and end offsets of each partition in the topic. By subtracting the beginning offset from the end offset. We can determine the number of messages in each partition and summing these values gives you the total number of messages in the topic.
Create a new maven project using IntelliJ Idea and add the following dependencies to the project.
Dependency:
<!-- https://mvnrepository.com/artifact/org.apache.kafka/kafka-clients -->
<dependency>
<groupId>org.apache.kafka</groupId>
<artifactId>kafka-clients</artifactId>
<version>3.7.0</version>
</dependency>
</dependencies>After project creation done, the the folder structure in the IDE will look like the below image:
Create the KafkaTopicMessageCount class to interact with the Kafka broker:
Open the pom.xml file and add the KafkaClient dependency to the project.
Run the application, it will display the number of messages in each partition of the specified topic.
This example demonstrates the accurate count of the messages currently in the each partition of the specified topic in the Apache Kafka.