0
0
KafkaHow-ToBeginner ยท 3 min read

How to List Topics in Kafka Using Command Line

To list all topics in Kafka, use the kafka-topics.sh --list --bootstrap-server <broker-address> command. This shows all existing topics managed by the Kafka cluster.
๐Ÿ“

Syntax

The command to list Kafka topics uses the kafka-topics.sh script with the --list option. You must specify the Kafka broker address with --bootstrap-server.

  • kafka-topics.sh: Kafka command line tool for topic management.
  • --list: Lists all topics in the Kafka cluster.
  • --bootstrap-server <broker-address>: Connects to the Kafka broker (e.g., localhost:9092).
bash
kafka-topics.sh --list --bootstrap-server localhost:9092
๐Ÿ’ป

Example

This example shows how to list all topics on a Kafka broker running locally on port 9092.

bash
kafka-topics.sh --list --bootstrap-server localhost:9092
Output
my-first-topic user-events system-logs
โš ๏ธ

Common Pitfalls

Common mistakes when listing Kafka topics include:

  • Not specifying the --bootstrap-server option or using the wrong broker address.
  • Using the deprecated --zookeeper option instead of --bootstrap-server in newer Kafka versions.
  • Running the command without Kafka tools installed or not in the Kafka bin directory.
bash
Wrong (deprecated):
kafka-topics.sh --list --zookeeper localhost:2181

Right (modern):
kafka-topics.sh --list --bootstrap-server localhost:9092
๐Ÿ“Š

Quick Reference

Remember these tips when listing Kafka topics:

  • Always use --bootstrap-server with the broker address.
  • Ensure Kafka is running and accessible at the specified address.
  • Run the command from Kafka's bin directory or add it to your PATH.
โœ…

Key Takeaways

Use kafka-topics.sh with --list and --bootstrap-server to list Kafka topics.
Avoid using the deprecated --zookeeper option for listing topics.
Make sure Kafka broker address is correct and reachable.
Run the command from Kafka's bin directory or ensure kafka-topics.sh is in your PATH.