Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • Java Tutorial
  • Java Spring
  • Spring Interview Questions
  • Java SpringBoot
  • Spring Boot Interview Questions
  • Spring MVC
  • Spring MVC Interview Questions
  • Java Hibernate
  • Hibernate Interview Questions
  • Advance Java Projects
  • Java Interview Questions
Open In App
Next Article:
How to Count number of Lines in a Git Repository?
Next article icon

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

Last Updated : 08 Jul, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

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:

Java
package com.gfg;  import org.apache.kafka.clients.admin.*; import org.apache.kafka.common.TopicPartition; import org.apache.kafka.common.TopicPartitionInfo;  import java.util.*; import java.util.concurrent.ExecutionException;  /**  * KafkaTopicMessageCount class is used to calculate the number of messages in a Kafka topic.  * It connects to a Kafka broker, retrieves offsets for each partition of the topic, and calculates the message count.  */ public class KafkaTopicMessageCount {      /**      * Main method to execute the message count calculation.      *       * @param args command line arguments      * @throws ExecutionException if the computation threw an exception      * @throws InterruptedException if the current thread was interrupted while waiting      */     public static void main(String[] args) throws ExecutionException, InterruptedException {         // Define the topic to be analyzed         String topic = "my-new-topic";                  // Set up properties for the AdminClient         Properties properties = new Properties();         properties.put(AdminClientConfig.BOOTSTRAP_SERVERS_CONFIG, "localhost:9092");          // Create an AdminClient with the specified properties         try (AdminClient adminClient = AdminClient.create(properties)) {             // Get the message counts for the specified topic             Map<TopicPartition, Long> messageCounts = getMessageCountsForTopic(adminClient, topic);                          // Print the message counts for each partition             messageCounts.forEach((tp, count) -> System.out.println("Partition: " + tp.partition() + ", Message Count: " + count));         }     }      /**      * Retrieves the message counts for each partition of the specified topic.      *       * @param adminClient the AdminClient instance to interact with the Kafka broker      * @param topic the name of the Kafka topic      * @return a map of TopicPartition to message count      * @throws ExecutionException if the computation threw an exception      * @throws InterruptedException if the current thread was interrupted while waiting      */     public static Map<TopicPartition, Long> getMessageCountsForTopic(AdminClient adminClient, String topic) throws ExecutionException, InterruptedException {         Map<TopicPartition, Long> messageCounts = new HashMap<>();          // Fetch the beginning offsets for each partition         List<TopicPartition> partitions = getTopicPartitions(adminClient, topic);         Map<TopicPartition, ListOffsetsResult.ListOffsetsResultInfo> beginningOffsets = adminClient.listOffsets(                 partitions.stream().collect(HashMap::new, (m, v) -> m.put(v, OffsetSpec.earliest()), HashMap::putAll)         ).all().get();          // Fetch the end offsets for each partition         Map<TopicPartition, ListOffsetsResult.ListOffsetsResultInfo> endOffsets = adminClient.listOffsets(                 partitions.stream().collect(HashMap::new, (m, v) -> m.put(v, OffsetSpec.latest()), HashMap::putAll)         ).all().get();          // Calculate the number of messages in each partition         for (TopicPartition partition : partitions) {             long beginningOffset = beginningOffsets.get(partition).offset();             long endOffset = endOffsets.get(partition).offset();             long messageCount = endOffset - beginningOffset;             messageCounts.put(partition, messageCount);         }          return messageCounts;     }      /**      * Retrieves the list of TopicPartitions for the specified topic.      *       * @param adminClient the AdminClient instance to interact with the Kafka broker      * @param topic the name of the Kafka topic      * @return a list of TopicPartitions      * @throws ExecutionException if the computation threw an exception      * @throws InterruptedException if the current thread was interrupted while waiting      */     private static List<TopicPartition> getTopicPartitions(AdminClient adminClient, String topic) throws ExecutionException, InterruptedException {         // Describe the topic to get information about its partitions         DescribeTopicsResult describeTopicsResult = adminClient.describeTopics(Collections.singletonList(topic));         TopicDescription topicDescription = describeTopicsResult.all().get().get(topic);         List<TopicPartition> partitions = new ArrayList<>();                  // Create a list of TopicPartitions from the topic description         for (TopicPartitionInfo partitionInfo : topicDescription.partitions()) {             partitions.add(new TopicPartition(topic, partitionInfo.partition()));         }         return partitions;     } } 


Step 3: Add the KafkaClient Dependency

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

XML
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0"          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"          xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">     <modelVersion>4.0.0</modelVersion>      <groupId>com.gfg</groupId>     <artifactId>kafka-topics</artifactId>     <version>1.0-SNAPSHOT</version>      <properties>         <maven.compiler.source>17</maven.compiler.source>         <maven.compiler.target>17</maven.compiler.target>         <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>     </properties>       <dependencies>         <!-- 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> </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.


Next Article
How to Count number of Lines in a Git Repository?

M

maheshkp8kl
Improve
Article Tags :
  • Advance Java
  • Apache Kafka Java

Similar Reads

  • How to get all Topics in Apache Kafka?
    Apache Kafka is an open-source event streaming platform that is used to build real-time data pipelines and also to build streaming applications. Kafka is specially designed to handle a large amount of data in a scalable way. In this article, we will learn how to get all topics in Apache Kafka. Steps
    2 min read
  • How to Subscribe to the Topic in Apache Kafka from the Java application?
    Subscribing to a Kafka topic from a Java application requires setting up a Kafka consumer that reads messages from a specific topic. This is a key part of many microservice architectures where services must process messages asynchronously. Apache Kafka provides a robust and scalable platform for bui
    4 min read
  • Creating a Socket to Display Message to a Single Client in Java
    This article describes the basic client-server connection where a client connects, a server sends a message to the client and the client displays the message using a socket connection. A client program sockets establish a connection with the server socket of server application then server socket con
    2 min read
  • How to Count number of Lines in a Git Repository?
    Counting the number of lines in a Git repository can be useful for various reasons, such as understanding the size of the project or monitoring codebase growth over time. There are several methods to achieve this, including using Git command line tools, the CLOC (Count Lines of Code) tool, and custo
    4 min read
  • How To Install Apache Kafka on Mac OS?
    Setting up Apache Kafka on your Mac OS operating system framework opens up many opportunities for taking care of continuous data and data streams effectively. Whether you're a designer, IT specialist, or tech geek, figuring out how to introduce Kafka locally permits you to examine and construct appl
    4 min read
  • Topics, Partitions, and Offsets in Apache Kafka
    Apache Kafka is a publish-subscribe messaging system. A messaging system let you send messages between processes, applications, and servers. Broadly Speaking, Apache Kafka is software where topics (A topic might be a category) can be defined and further processed. In this article, we are going to di
    6 min read
  • How Kafka Producers, Message Keys, Message Format and Serializers Work in Apache Kafka?
    Kafka Producers are going to write data to topics and topics are made of partitions. Now the producers in Kafka will automatically know to which broker and partition to write based on your message and in case there is a Kafka broker failure in your cluster the producers will automatically recover fr
    5 min read
  • Implementing Request Response in Java Apache Kafka
    Apache Kafka is a very powerful distributed event streaming platform that can be used for building real-time pipelines and streaming applications. It is highly scalable, fault-tolerant, and provides high throughput. One of the common patterns used in the Kafka application is the request-response pat
    6 min read
  • Effective Strategy to Avoid Duplicate Messages in Apache Kafka Consumer
    Apache Kafka is a good choice for distributed messaging systems because of its robust nature. In this article, we will explore advanced strategies to avoid duplicate messages in Apache Kafka consumers. Challenge of Duplicate Message ConsumptionApache Kafka's at-least-once delivery system ensures mes
    3 min read
  • What is Apache Kafka and How Does it Work?
    Apache Kafka is a distributed, high-throughput, real-time, low-latency data streaming platform. It's built to transport large volumes of data in real-time between systems, without needing to develop hundreds of intricate integrations. Rather than integrating each system with each other system, you c
    9 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences