How to Use kafka-topics Command in Apache Kafka
Use the
kafka-topics command to manage Kafka topics by specifying actions like --create, --list, --describe, or --delete along with necessary options such as --topic and --bootstrap-server. This command helps you create new topics, view existing ones, get details, or remove topics from your Kafka cluster.Syntax
The kafka-topics command uses the following general syntax:
--bootstrap-server <host:port>: Connects to the Kafka broker.--create: Creates a new topic.--list: Lists all topics.--describe: Shows details of a topic.--delete: Deletes a topic.--topic <topic-name>: Specifies the topic name.--partitions <num>: Number of partitions (for create).--replication-factor <num>: Replication factor (for create).
bash
kafka-topics --bootstrap-server <host:port> --create --topic <topic-name> --partitions <num> --replication-factor <num> kafka-topics --bootstrap-server <host:port> --list kafka-topics --bootstrap-server <host:port> --describe --topic <topic-name> kafka-topics --bootstrap-server <host:port> --delete --topic <topic-name>
Example
This example shows how to create a topic named my-topic with 3 partitions and a replication factor of 1, then list all topics, describe my-topic, and finally delete it.
bash
kafka-topics --bootstrap-server localhost:9092 --create --topic my-topic --partitions 3 --replication-factor 1 kafka-topics --bootstrap-server localhost:9092 --list kafka-topics --bootstrap-server localhost:9092 --describe --topic my-topic kafka-topics --bootstrap-server localhost:9092 --delete --topic my-topic
Output
Created topic "my-topic".
my-topic
Topic: my-topic PartitionCount: 3 ReplicationFactor: 1 Configs:
Topic: my-topic Partition: 0 Leader: 1 Replicas: 1 Isr: 1
Topic: my-topic Partition: 1 Leader: 1 Replicas: 1 Isr: 1
Topic: my-topic Partition: 2 Leader: 1 Replicas: 1 Isr: 1
Deleted topic "my-topic".
Common Pitfalls
Common mistakes when using kafka-topics include:
- Forgetting to specify
--bootstrap-server, which is required to connect to the Kafka broker. - Using
--zookeeperoption in newer Kafka versions (2.4+), which is deprecated. - Trying to delete a topic when topic deletion is disabled in Kafka configuration.
- Not specifying
--partitionsor--replication-factorwhen creating a topic, causing errors.
bash
Wrong: kafka-topics --create --topic test-topic Right: kafka-topics --bootstrap-server localhost:9092 --create --topic test-topic --partitions 1 --replication-factor 1
Quick Reference
| Option | Description |
|---|---|
| --bootstrap-server | Kafka broker address to connect |
| --create | Create a new topic |
| --list | List all topics |
| --describe | Show details of a topic |
| --delete | Delete a topic |
| --topic | Specify the topic name |
| --partitions | Number of partitions for new topic |
| --replication-factor | Replication factor for new topic |
Key Takeaways
Always specify --bootstrap-server to connect to Kafka brokers.
Use --create with --partitions and --replication-factor to make new topics.
Use --list to see all topics and --describe to get topic details.
Topic deletion requires Kafka to have deletion enabled in its config.
Avoid using deprecated --zookeeper option in recent Kafka versions.