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:9092Example
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:9092Output
my-first-topic
user-events
system-logs
Common Pitfalls
Common mistakes when listing Kafka topics include:
- Not specifying the
--bootstrap-serveroption or using the wrong broker address. - Using the deprecated
--zookeeperoption instead of--bootstrap-serverin 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-serverwith the broker address. - Ensure Kafka is running and accessible at the specified address.
- Run the command from Kafka's
bindirectory 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.