0
0
Kafkadevops~10 mins

Topic creation in Kafka - Step-by-Step Execution

Choose your learning style9 modes available
Process Flow - Topic creation
Start
Define Topic Name
Set Configurations
Send Create Topic Request
Kafka Server Validates
If Valid
Topic Created
If Invalid
Error Returned
End
This flow shows how a topic is created by defining its name and settings, sending a request to Kafka, which then validates and either creates the topic or returns an error.
Execution Sample
Kafka
from kafka.admin import KafkaAdminClient, NewTopic

admin_client = KafkaAdminClient(bootstrap_servers='localhost:9092')
new_topic = NewTopic(name='my_topic', num_partitions=3, replication_factor=1)
admin_client.create_topics(new_topics=[new_topic])
This code creates a Kafka topic named 'my_topic' with 3 partitions and a replication factor of 1.
Process Table
StepActionInput/ConditionResult
1Initialize KafkaAdminClientbootstrap_servers='localhost:9092'Client ready to send requests
2Define NewTopicname='my_topic', num_partitions=3, replication_factor=1Topic object created
3Call create_topicsnew_topics=[new_topic]Request sent to Kafka server
4Kafka server validatesTopic name valid, partitions and replication validTopic created successfully
5Return from create_topicsNo errorsTopic creation confirmed
💡 Topic created successfully after validation by Kafka server
Status Tracker
VariableStartAfter Step 2After Step 3Final
admin_clientNoneKafkaAdminClient instanceKafkaAdminClient instanceKafkaAdminClient instance
new_topicNoneNewTopic(name='my_topic', num_partitions=3, replication_factor=1)NewTopic object passed to create_topicsNewTopic object created on server
Key Moments - 2 Insights
Why do we need to specify the number of partitions and replication factor when creating a topic?
Partitions determine how data is split and parallelized; replication factor controls fault tolerance. This is shown in execution_table step 2 where these values are set before creation.
What happens if the topic name is invalid or already exists?
Kafka server validates the request (step 4). If invalid or duplicate, an error is returned instead of creating the topic.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the result after step 3?
ARequest sent to Kafka server
BTopic created successfully
CError returned
DClient initialization
💡 Hint
Check the 'Result' column for step 3 in execution_table
At which step does Kafka validate the topic creation request?
AStep 2
BStep 3
CStep 4
DStep 5
💡 Hint
Look at the 'Action' column in execution_table for validation
If you change the replication factor to 2, which variable in variable_tracker changes?
Aadmin_client
Bnew_topic
Cbootstrap_servers
DNone
💡 Hint
Replication factor is part of the NewTopic object tracked in variable_tracker
Concept Snapshot
Kafka Topic Creation:
- Use KafkaAdminClient to connect
- Define NewTopic with name, partitions, replication
- Call create_topics with the NewTopic list
- Kafka validates and creates topic
- Errors if invalid or duplicate
Full Transcript
This visual trace shows how to create a Kafka topic programmatically. First, a KafkaAdminClient is initialized to connect to the Kafka server. Then, a NewTopic object is defined with the topic name, number of partitions, and replication factor. The create_topics method is called with this topic list, sending a request to the Kafka server. The server validates the topic name and settings. If valid, the topic is created successfully; otherwise, an error is returned. Variables like admin_client and new_topic track the client and topic state through the process.