Apache Kafka list all Topics
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Apache Kafka is an open-source stream-processing software platform developed by LinkedIn and donated to the Apache Software Foundation, written in Scala and Java. It aims to provide a high-throughput, low-latency platform for handling real-time data feeds. Here we will explore the topic management in Apache Kafka, including how to list, describe, and utilize topics effectively.
What is a Kafka Topic?
A topic in Kafka is a category or feed name to which records are published. Topics in Kafka are always multi-subscriber; that is, a topic can have zero, one, or many consumers that subscribe to the data written to it. Moreover, each topic is split into partitions, which contain records in an immutable sequence.
Listing All Topics
Kafka provides multiple ways to list all the topics. The most common method is using the Kafka command line tools that come with Kafka binaries:
Using the Kafka-topics.sh script
You can list all topics using the kafka-topics.sh script found in the bin directory of your Kafka installation:
Here, localhost:9092 should be replaced with the address of one of your Kafka brokers.
Using Kafka Admin Client API
For programmatic access, Kafka offers the Admin Client API. Here’s an example in Java:
This Java code snippet demonstrates how to list all available topics in a Kafka cluster programmatically.
Role of Topics in Kafka Architecture
Apart from serving as categories or feeds, topics play a central role in Kafka’s architecture by determining how data is partitioned and distributed across the cluster. Here's a brief look into this:
- Partitioning: Topics are divided into partitions to allow the data to be scaled horizontally. Each partition can be hosted on a different Kafka broker in the cluster.
- Replication: Kafka can replicate partitions across multiple brokers to ensure that the data is available even in the case of a broker failure.
Managing and Monitoring Kafka Topics
Managing Kafka involves not just listing topics, but also creating, modifying, and monitoring them. These tasks are essential for maintaining the health and performance of a Kafka cluster.
Creating a Topic
To create a topic, use the kafka-topics.sh tool with the --create option:
This command creates a topic named myTopic with one replication factor and four partitions.
Modifying a Topic
To change topic configurations like the number of partitions, you can use the --alter option:
This increases the number of partitions for myTopic to six.
Describing a Topic
You can get detailed information about a topic configuration using the --describe option:
Summary Table
| Feature | Description |
| Topic Creation | kafka-topics.sh --create Used for creating new topics. |
| Topic Listing | kafka-topics.sh --list Displays all available topics in the cluster. |
| Topic Modification | kafka-topics.sh --alter Used to modify properties of an existing topic. |
| Topic Description | kafka-topics.sh --describe Provides detailed configuration information of a topic. |
| Partitioning | Allows horizontal scaling by splitting data across multiple nodes. |
| Replication | Ensures data availability and durability by replicating partitions across brokers. |
Conclusion
Managing Kafka topics effectively is crucial for optimal performance and reliability of Kafka-based applications. With the command line tools and the Admin Client API, administrators and developers can handle topic management tasks simply and efficiently. Maintaining an understanding of Kafka's topic architecture and capabilities ensures that data flows smoothly and is robust against system failures.

