How to create topics in apache kafka?
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Apache Kafka, developed by LinkedIn and later open-sourced under the Apache Software Foundation, is a distributed streaming platform that excels in handling high-throughput data streams. One of the fundamental components of Kafka architecture is the "topic," which acts as a category or feed for messages to be stored and published.
Understanding Kafka Topics
Kafka topics are uniquely identified within a cluster and are used to organize messages. Each topic is split into partitions which allow Kafka to scale horizontally by distributing the data across multiple brokers in the cluster. Messages within these partitions are ordered and immutable, and each message within a partition is assigned a unique sequence number known as an offset.
Creating a Topic in Apache Kafka
Creating a topic in Kafka can be achieved either using the Kafka command-line tools or programmatically using Kafka’s AdminClient API. Here are both methods detailed:
Using Kafka Command Line Tools
Kafka ships with a set of command-line tools located in the bin directory of your Kafka installation. The kafka-topics.sh script is used to create topics.
Example:
In this example:
--bootstrap-serverspecifies the Kafka server to connect to.--replication-factorspecifies the number of copies of the topic to be maintained.--partitionsdefines the number of partitions for the topic.--topicis the name of the topic you are creating.
Using AdminClient API
For those implementing Kafka interactions programmatically, the AdminClient API provides a way to manage topics from your Java applications.
Java Example:
In this Java code:
AdminClientConfig.BOOTSTRAP_SERVERS_CONFIGdefines the initial list of brokers.- The
NewTopicobject details the topic name, number of partitions, and replication factor. createTopicsis called on theAdminClientinstance to create the topic.
Topic Configuration Options
Apache Kafka allows you to configure topics according to your needs. Common configurations include:
retention.ms: Controls how long messages are retained.cleanup.policy: Determines whether old data is deleted or compacted.
Example Command:
Summary
| Feature | Command Line | AdminClient API | Configuration Key |
| Create Topic | kafka-topics.sh --create | AdminClient.create() | N/A |
| Set Partitions | --partitions | NewTopic() | N/A |
| Replication | --replication-factor | NewTopic() | N/A |
| Configure Topic | kafka-topics.sh --alter | AdminClient.alter() | Various keys |
Additional Considerations
- Topic Deletion: Deleting a topic can be done similarly using both command-line and AdminClient API. However, ensure that the topic deletion is enabled in the broker settings (
delete.topic.enablemust be set totrue). - Monitoring Tools: Tools like Kafka Manager or Confluent Control Center can also be used for easier management and monitoring of topics.
- Security: Consider implementing proper ACLs and security practices especially in production environments.
By properly utilizing Kafka topics, developers and organizations can effectively manage the flow of data across systems, ensuring robustness and scalability in data processing pipelines.

