Partner – Moderne – NPI EA (cat=Spring Boot)
announcement - icon

Refactor Java code safely — and automatically — with OpenRewrite.

Refactoring big codebases by hand is slow, risky, and easy to put off. That’s where OpenRewrite comes in. The open-source framework for large-scale, automated code transformations helps teams modernize safely and consistently.

Each month, the creators and maintainers of OpenRewrite at Moderne run live, hands-on training sessions — one for newcomers and one for experienced users. You’ll see how recipes work, how to apply them across projects, and how to modernize code with confidence.

Join the next session, bring your questions, and learn how to automate the kind of work that usually eats your sprint time.

Course – Black Friday 2026 – NPI EA – main – before_post_content
announcement - icon

Yes, we're now running our Black Friday Sale. All Courses are 33% off until 4th December, 2026 at 17:00 Romania time:

>> EXPLORE ACCESS NOW

Course – Black Friday 2026 – NPI – main – before_post_content
announcement - icon

Yes, we're now running our Black Friday Sale. All Courses are 33% off until 4th December, 2026 at 17:00 Romania time:

>> EXPLORE ACCESS NOW

1. Introduction

In this tutorial, we’ll take a look at different ways of retrieving the total number of partitions of a Kafka Topic. After a short introduction about what is a Kafka Partition and why we might need to retrieve this information, we’ll write the Java code to execute this operation. Then we’ll see how it’s possible to get this information using CLI.

2. Kafka Partition

A Kafka Topic can be divided into several partitions. The goal of having more than one partition is to be able to consume messages from the same topic concurrently. Because it’s useless to have more consumers than existing partitions, the number of Kafka Partitions in a topic represents the maximum parallelism level for consuming. So knowing in advance how many partitions a given topic has can be useful for sizing the respective consumers correctly.

3. Retrieve the Partition Number using Java

To retrieve the number of partitions for a specific topic using Java, we can rely on the KafkaProducer.partitionFor(topic) method. This method will return the partition metadata for the given topic:

Properties producerProperties = new Properties();
// producerProperties.put("key","value") ... 
KafkaProducer<String, String> producer = new KafkaProducer<>(producerProperties)
List<PartitionInfo> info = producer.partitionsFor(TOPIC);
Assertions.assertEquals(3, info.size());

The size of the PartitionInfo list returned by this method will be exactly equal to the number of partitions configured for the specific topic.

If we don’t have access to a Producer, we can achieve the same results using Kafka AdminClient in a slightly more complex way:

Properties props = new Properties();
// props.put("key","value") ...
AdminClient client = AdminClient.create(props)){
DescribeTopicsResult describeTopicsResult = client.describeTopics(Collections.singletonList(TOPIC));
Map<String, KafkaFuture> values = describeTopicsResult.values();
KafkaFuture topicDescription = values.get(TOPIC);
Assertions.assertEquals(3, topicDescription.get().partitions().size());

In this case, we are relying on the KafkaClient.describeTopic(topic) method, which returns a DescribeTopicsResult object containing a Map of future tasks to be executed. Here we are retrieving only the TopicDescription of the Topic we need and, finally, retrieving the number of partitions.

4. Retrieve the Partition Number using CLI

We have a couple of options to retrieve the number of partitions of a given Topic with CLI.

First of all, we can rely on the shell scripts that come with every Kafka install and run:

$ kafka-topics --describe --bootstrap-server localhost:9092 --topic topic_name

This command will output a complete description of the topic specified:

Topic:topic_name        PartitionCount:3        ReplicationFactor:1     Configs: ... 

Another option is to use Kafkacat, a non-JVM-based consumer and producer for Kafka. With the metadata list mode (-L), this shell utility shows the current state of the Kafka cluster, including all its topics and partitions. To show metadata information of a specific topic, we can run the following:

$ kafkacat -L -b localhost:9092 -t topic_name

The output of this command will be:

Metadata for topic topic_name (from broker 1: mybroker:9092/1):
  topic "topic_name" with 3 partitions:
    partition 0, leader 3, replicas: 1,2,3, isrs: 1,2,3
    partition 1, leader 1, replicas: 1,2,3, isrs: 1,2,3
    partition 2, leader 1, replicas: 1,2, isrs: 1,2

We can see how this shell utility command also shows useful details about a specific topic and its partitions.

5. Conclusion

In this short tutorial, we have seen how it’s possible to retrieve the total number of partitions of a specific Kafka Topic using Java and CLI.

We have first seen why retrieving this information is useful, and then we used KafkaProducer and KafkaAdmin. Finally, we have used shell commands with Kafka script utilities and KafkaCat.

The code backing this article is available on GitHub. Once you're logged in as a Baeldung Pro Member, start learning and coding on the project.
Partner – Moderne – NPI EA (tag=Refactoring)
announcement - icon

Modern Java teams move fast — but codebases don’t always keep up. Frameworks change, dependencies drift, and tech debt builds until it starts to drag on delivery. OpenRewrite was built to fix that: an open-source refactoring engine that automates repetitive code changes while keeping developer intent intact.

The monthly training series, led by the creators and maintainers of OpenRewrite at Moderne, walks through real-world migrations and modernization patterns. Whether you’re new to recipes or ready to write your own, you’ll learn practical ways to refactor safely and at scale.

If you’ve ever wished refactoring felt as natural — and as fast — as writing code, this is a good place to start.

Course – Black Friday 2026 – NPI EA – main – after_post_content

announcement - icon

Yes, we're now running our Black Friday Sale. All Courses are 33% off until 4th December, 2026 at 17:00 Romania time:

>> EXPLORE ACCESS NOW

Course – Black Friday 2026 – NPI – main – after_post_content

announcement - icon

Yes, we're now running our Black Friday Sale. All Courses are 33% off until 4th December, 2026 at 17:00 Romania time:

>> EXPLORE ACCESS NOW