Apache Kafka create topic from code
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.
Apache Kafka is a powerful distributed streaming platform that enables you to build real-time streaming data pipelines and applications. At the heart of its architecture are topics, where topics are categories or feeds to which records are published. In this article, we'll delve into how to programmatically create Kafka topics using Apache Kafka’s APIs, focusing on the use of the Java client.
Understanding Kafka Topics
Before diving into the code, it's essential to understand what topics are and why they are important. A Kafka topic is a logical channel to which producers send records (messages) and from which consumers read these records. Topics in Kafka are multi-subscriber; that is, they can be consumed by many consumers simultaneously.
Topics are split into partitions to allow Kafka to scale processing by distributing data across multiple nodes in a Kafka cluster. Each partition is an ordered, immutable sequence of records, and each record in a partition is assigned a unique offset.
Creating Topics Programmatically
Apache Kafka provides administrative APIs that you can use to manage topics. The AdminClient API is an interface for managing and inspecting topics, brokers, and other Kafka objects. Below is an example of how you might use this API in Java to create a topic.
Step-by-Step Implementation
- Set up the Project
- Add Kafka clients library to your project. If you're using Maven, you can add the following dependency to your
pom.xml:
- Create an Instance of
AdminClient- Use
AdminClient.create()to instantiate a new client. You’ll need to provide a set of properties, particularly the broker’s address.
- Define Topic Specifications
- Specify the topic name, number of partitions, and replication factor. Replication factor defines how many copies of the data will be created.
- Create the Topic
- Use the
createTopics()method fromAdminClient. This method takes a collection ofNewTopicobjects.
- Handle Exceptions
- Ensure to handle
InterruptedExceptionandExecutionExceptionto catch any issues that might occur during the creation of the topic.
- Closing the AdminClient
- Do not forget to close the
AdminClientinstance once your operations are complete to free up resources.
Summary Table
The following table summarizes the key configurations for creating a topic:
| Property | Description | Example Value |
BOOTSTRAP_SERVERS_CONFIG | The Kafka server to connect to. | "localhost:9092" |
topic | The name of the topic. | "myNewTopic" |
partitions | Number of partitions of the topic. | 3 |
replicationFactor | Replication factor for each partition in the topic. | 1 |
Additional Considerations
When creating topics, it's critical to consider the following:
- Topic Naming: Choose a convention for naming topics that reflect their purpose and possibly the data type or source.
- Partition Count: More partitions allow greater parallelism in processing, but also more overhead in management and replication.
- Error Handling: Robust error handling in production applications ensures stability and facilitates troubleshooting.
Creating topics programmatically offers flexibility and the ability to automate aspects of system configuration and scaling. Understanding and leveraging the Kafka Admin API effectively can provide significant benefits in managing your Kafka environment programmatically.
Related reading
- Apache Kafka Default Encoder Not Working
- Apache Kafka doens't start after SSL configuration
- Apache Kafka example error Failed to send message after 3 tries
- Apache Kafka Exception causing close of session <xx> due to java.io.IOException Unreasonable length
- Apache kafka Failed to acquire lock on file .lock in tmp/kafka-logs
- Apache Kafka Failed to Update Metadata/java.nio.channels.ClosedChannelException
- Apache Kafka for Time Series Data Persistence
- Apache Kafka How to check, that an event has been fully handled?

System Design Fundamentals
Build a strong foundation in designing scalable, reliable distributed systems.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.