0
0
Kafkadevops~5 mins

Topic creation in Kafka - Commands & Configuration

Choose your learning style9 modes available
Introduction
Kafka topics are like folders where messages are stored. Creating a topic lets you organize and separate different streams of data so your apps can read and write messages clearly.
When you want to separate logs from different services to keep data organized.
When you need to create a new channel for a new feature in your app to send messages.
When you want to control how many copies of data are kept for safety.
When you want to set how many parts a topic has to allow multiple consumers to read in parallel.
When you want to test sending and receiving messages on a fresh topic.
Commands
This command creates a new Kafka topic named 'example-topic' with 3 partitions and a replication factor of 1, connecting to the Kafka server at localhost on port 9092.
Terminal
kafka-topics.sh --create --topic example-topic --bootstrap-server localhost:9092 --partitions 3 --replication-factor 1
Expected OutputExpected
Created topic example-topic.
--topic - Name of the topic to create
--partitions - Number of partitions for parallelism
--replication-factor - Number of copies of data for fault tolerance
This command lists all topics currently available on the Kafka server to verify that 'example-topic' was created.
Terminal
kafka-topics.sh --list --bootstrap-server localhost:9092
Expected OutputExpected
example-topic __consumer_offsets
--list - List all topics
--bootstrap-server - Kafka server address
This command shows detailed information about the 'example-topic', including partition count and replication details.
Terminal
kafka-topics.sh --describe --topic example-topic --bootstrap-server localhost:9092
Expected OutputExpected
Topic: example-topic PartitionCount: 3 ReplicationFactor: 1 Configs: Topic: example-topic Partition: 0 Leader: 0 Replicas: 0 Isr: 0 Topic: example-topic Partition: 1 Leader: 0 Replicas: 0 Isr: 0 Topic: example-topic Partition: 2 Leader: 0 Replicas: 0 Isr: 0
--describe - Show topic details
--topic - Specify the topic to describe
Key Concept

If you remember nothing else from this pattern, remember: creating a Kafka topic sets up a named place to store and organize messages with partitions and replication for performance and safety.

Common Mistakes
Not specifying the replication factor when creating a topic
Kafka requires a replication factor to know how many copies of data to keep; missing it causes an error or defaults that may not suit your needs.
Always include --replication-factor with a valid number when creating a topic.
Using the wrong Kafka server address in --bootstrap-server
If the address is wrong, the command cannot connect to Kafka and fails to create or list topics.
Use the correct hostname and port where your Kafka broker is running, like localhost:9092.
Trying to create a topic that already exists without checking
Kafka will return an error saying the topic exists, causing confusion or script failure.
List existing topics first with --list to avoid duplicates before creating a new topic.
Summary
Use kafka-topics.sh with --create to make a new topic with partitions and replication.
Verify topic creation by listing all topics with --list.
Check topic details like partitions and replicas with --describe.