VOOZH about

URL: https://www.geeksforgeeks.org/advance-java/how-to-get-number-of-messages-in-a-topic-in-apache-kafka-in-java/

⇱ How to Get Number of Messages in a Topic in Apache Kafka in Java? - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

How to Get Number of Messages in a Topic in Apache Kafka in Java?

Last Updated : 23 Jul, 2025

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.

Implementation to Get the Number of Messages in a Topic in Apache Kafka

Step 1: Create a Maven Project

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:

👁 Folder Structure


Step 2: Create the KafkaTopicMessageCount Class

Create the KafkaTopicMessageCount class to interact with the Kafka broker:


Step 3: Add the KafkaClient Dependency

Open the pom.xml file and add the KafkaClient dependency to the project.


Step 4: Run the application

Run the application, it will display the number of messages in each partition of the specified topic.

👁 Number of Messages Output

This example demonstrates the accurate count of the messages currently in the each partition of the specified topic in the Apache Kafka.

Comment
Article Tags:

Explore