How to Describe a Topic in Kafka: Syntax and Examples
To describe a topic in Kafka, use the
kafka-topics.sh --describe --topic <topic-name> --bootstrap-server <broker-address> command. This shows details like partitions, replicas, and leader information for the specified topic.Syntax
The command to describe a Kafka topic uses the kafka-topics.sh tool with these parts:
--describe: tells Kafka to show topic details.--topic <topic-name>: specifies the topic you want to describe.--bootstrap-server <broker-address>: points to the Kafka broker to connect to.
bash
kafka-topics.sh --describe --topic <topic-name> --bootstrap-server <broker-address>
Example
This example shows how to describe a topic named my-topic on a Kafka broker running at localhost:9092. It lists partitions, leaders, replicas, and in-sync replicas.
bash
kafka-topics.sh --describe --topic my-topic --bootstrap-server localhost:9092Output
Topic: my-topic PartitionCount: 3 ReplicationFactor: 2 Configs:
Topic: my-topic Partition: 0 Leader: 1 Replicas: 1,2 Isr: 1,2
Topic: my-topic Partition: 1 Leader: 2 Replicas: 2,1 Isr: 2,1
Topic: my-topic Partition: 2 Leader: 1 Replicas: 1,2 Isr: 1,2
Common Pitfalls
Common mistakes when describing Kafka topics include:
- Not specifying the
--bootstrap-serveroption or using the wrong broker address, causing connection errors. - Using
--zookeeperoption with newer Kafka versions (2.4+), which is deprecated. - Misspelling the topic name or describing a topic that does not exist, resulting in no output or errors.
bash
Wrong (deprecated): kafka-topics.sh --describe --topic my-topic --zookeeper localhost:2181 Right (modern): kafka-topics.sh --describe --topic my-topic --bootstrap-server localhost:9092
Quick Reference
Remember these tips when describing Kafka topics:
- Always use
--bootstrap-serverwith the broker address. - Use exact topic names to avoid errors.
- Check Kafka version; avoid deprecated
--zookeeperoption. - Output shows partitions, leaders, replicas, and in-sync replicas.
Key Takeaways
Use
kafka-topics.sh --describe --topic <topic-name> --bootstrap-server <broker> to get topic details.Avoid using the deprecated
--zookeeper option in recent Kafka versions.Ensure the broker address and topic name are correct to get valid output.
The output includes partition count, replication factor, leaders, replicas, and in-sync replicas.
Describing topics helps understand topic distribution and replication in Kafka.