How to Create Topic in Kafka: Simple Steps and Examples
To create a topic in Kafka, use the
kafka-topics.sh command with the --create option, specifying the topic name, number of partitions, and replication factor. For example, kafka-topics.sh --create --topic my-topic --bootstrap-server localhost:9092 --partitions 3 --replication-factor 1 creates a topic named my-topic with 3 partitions and replication factor 1.Syntax
The basic syntax to create a Kafka topic is:
kafka-topics.sh: The Kafka command line tool to manage topics.--create: Flag to create a new topic.--topic <topic-name>: Name of the topic to create.--bootstrap-server <host:port>: Kafka broker address to connect.--partitions <number>: Number of partitions for the topic.--replication-factor <number>: Number of replicas for fault tolerance.
bash
kafka-topics.sh --create --topic <topic-name> --bootstrap-server <host:port> --partitions <num> --replication-factor <num>
Example
This example creates a topic named my-topic with 3 partitions and a replication factor of 1 on a Kafka broker running locally.
bash
kafka-topics.sh --create --topic my-topic --bootstrap-server localhost:9092 --partitions 3 --replication-factor 1
Output
Created topic "my-topic".
Common Pitfalls
Common mistakes when creating Kafka topics include:
- Not specifying the
--bootstrap-serveror using the wrong broker address. - Setting a replication factor higher than the number of available brokers, which causes errors.
- Using an existing topic name without realizing it, which will not create a new topic.
- Forgetting to specify partitions, which defaults to 1 but may not suit your needs.
Always check your Kafka cluster status and broker count before setting replication factor.
bash
## Wrong: replication factor higher than brokers kafka-topics.sh --create --topic test-topic --bootstrap-server localhost:9092 --partitions 2 --replication-factor 3 ## Right: replication factor not exceeding brokers kafka-topics.sh --create --topic test-topic --bootstrap-server localhost:9092 --partitions 2 --replication-factor 1
Output
Error: Replication factor: 3 larger than available brokers: 1
Created topic "test-topic".
Quick Reference
| Option | Description |
|---|---|
| --create | Create a new topic |
| --topic | Name of the topic |
| --bootstrap-server | Kafka broker address |
| --partitions | Number of partitions |
| --replication-factor | Number of replicas for fault tolerance |
Key Takeaways
Use kafka-topics.sh with --create and specify topic name, partitions, and replication factor.
Ensure replication factor does not exceed the number of Kafka brokers available.
Always provide the correct --bootstrap-server address to connect to your Kafka cluster.
Check if the topic already exists before creating to avoid conflicts.
Partitions control parallelism; replication factor controls fault tolerance.