Apache Kafka – Topics using CLI
Last Updated :
01 Feb, 2023
In Apache Kafka, a topic is a category or stream of messages that the Kafka message broker (or cluster) stores. Producers write data to topics and consumers read from topics. A Topic in Kafka is similar to a table in a database or a stream in a stream processing system. Each topic is divided into a number of partitions, which allows for parallel processing of the data by consumers. Messages are assigned to partitions based on a partitioning key, which can be any field in the message. Topics are used to store and transmit data in Kafka. They can be used for a wide range of applications, such as real-time data processing, event-driven architectures, and log aggregation. A topic in Kafka can be created, deleted, described, or changed using the CLI. Make sure Kafka is open before you begin. The necessary parameters must be specified in order to describe a Kafka topic and obtain partition information.
- Use the hostname and port for Kafka v2.2+, for example, localhost:9092.
- Use localhost:2181 as the Zookeeper URL and port if you’re using a previous version of Kafka.
Create a Kafka Topic
- The name of the topic, number of partitions, and replication factor are required parameters.
- Use the “–create” option in the CLI kafka-topics.sh.
To create a Kafka topic called “topic1” with 3 partitions and a replication factor of 1, connect to the Kafka broker running at localhost:9092.
kafka-topics.sh --bootstrap-server localhost:9092 --topic topic1 --create --partitions 3 --replication-factor 1
Output:

Creating kafka topic
Additional important Parameters You Can Set (Advanced)
--config
You may configure topics at the topic level, using a command like –config max.message.bytes=64000.
--disable-rack-aware
Disable rack-aware replica assignment (not advised; only set if you are confident in your ability to use it)
List Kafka Topic
Use the “–list” option in the CLI for kafka-topics.sh. Listing topics when my Kafka broker is running at localhost:9092
kafka-topics.sh --bootstrap-server localhost:9092 --list
Output:

Listing kafka topics
Describe a Kafka Topic
In the CLI kafka-topics.sh, use the “–describe“ option. Describing topics when my Kafka broker is running at localhost:9092
kafka-topics.sh --bootstrap-server localhost:9092 --describe --topic topic1
Output:

Describing a kafka topic
These are some of the advanced parameters that can be used with “–list” and “–describe.”
--exclude-internal
When using the list or describe commands, ignore internal subjects (they are by default listed).
Using replication status as a filter for topics:
--at-min-isr-partitions
If enabled, only display partitions whose isr count is equal to the given minimum while explaining subjects.
--unavailable-partitions
Only display divisions with an unavailable leader.
--under-min-isr-partitions
A partition will only be shown if its isr count is below the defined minimum.
--under-replicated-partitions
only display the under-replicated partitions.
Similar Reads
Apache Kafka - Create Producer using Java
Apache Kafka is a publish-subscribe messaging system. A messaging system lets you send messages between processes, applications, and servers. Apache Kafka is software where topics (A topic might be a category) can be defined and further processed. Read more on Kafka here: What is Apache Kafka and Ho
4 min read
Apache Kafka - Create Safe Producer using Java
Apache 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 rec
5 min read
Apache Kafka Message Keys
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
What is Apache Kafka Streams?
Kafka Streams is a library for processing and analyzing data stored in Kafka. It expands on crucial stream processing ideas such as clearly separating event time from processing time, allowing for windows, and managing and querying application information simply but effectively in real time. Kafka S
4 min read
Apache Kafka - Message Compression
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
Apache Kafka - linger.ms and batch.size
After message compression, we want to control that batch. By default, Kafka wants to minimize latency. And so it'll try to send the records as soon as possible. It will have up to, five requests in flight, which means that up to five messages individually can be sent at the same time. Then, once the
4 min read
Disable SLF4J Logging in Apache Kafka
SLF4J means that Simple Logging Facade for Java serves as a logging facade, allowing applications to use various logging frameworks such as Log4j, and Logback without being tied to a specific implementation Kafka, being a distributed streaming platform, also on SLF4J for its logging and Log4j has th
2 min read
Apache Kafka Producer
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
Apache Kafka - Create High Throughput Producer using Java
Apache 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 rec
4 min read
Apache Kafka - Create Producer with Callback using Java
Apache Kafka is a publish-subscribe messaging system. A messaging system let you send messages between processes, applications, and servers. Apache Kafka is software where topics (A topic might be a category) can be defined and further processed. Read more on Kafka here: What is Apache Kafka and How
6 min read