Kafka
Java
Topic Creation
Programming
Code Tutorial

How to create a Topic in Kafka through Java

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Apache Kafka is a robust, distributed event-streaming platform capable of handling trillions of events a day. It provides a distributed nature, fault tolerance, and scalability, primarily used for big data streaming applications such as log collection, streaming analytics, data pipelines, and event sourcing. Creating topics in Kafka is essential as they are the categories or feeds where records are stored and published.


Overview of Kafka Topics

In Kafka, a topic is a category name to which records are stored and published. All Kafka records are organized into topics. Producer applications write data to topics and consumer applications read from them. Thus, creating and configuring topics correctly is a crucial aspect of working with Kafka.

Why Java?

Java is one of the most commonly used languages with Kafka, primarily because Kafka itself is written in Java and Scala. It has extensive client support with the official Apache Kafka client, which provides APIs for producing and consuming messages, thus making it a robust choice for Kafka integration.


Prerequisites

Before creating a Kafka topic through Java, you need to ensure the following components are up and running:

  • Apache Kafka - Ensure that the Kafka server is installed and running.
  • Zookeeper - Kafka uses Zookeeper to manage and coordinate the Kafka brokers.
  • Java JDK - Java Development Kit (JDK) should be installed.
  • Kafka Client - This is the Java library necessary for Kafka interactions.

Step-by-Step Guide to Create a Kafka Topic in Java

1. Adding Kafka Client to Your Project

Include the Kafka client library in your project. If you are using Maven, add the following dependency to your pom.xml:

xml
1<dependency>
2    <groupId>org.apache.kafka</groupId>
3    <artifactId>kafka-clients</artifactId>
4    <version>3.0.0</version>
5</dependency>

2. Create a Java Class to Manage Topics

You need to create a Java class that can handle Kafka admin operations like creating a topic.

3. Setting Up Configuration

AdminClient API needs Kafka configuration. Set up the properties needed for the Kafka AdminClient:

java
1Properties props = new Properties();
2props.put(AdminClientConfig.BOOTSTRAP_SERVERS_CONFIG, "localhost:9092");
3props.put(AdminClientConfig.REQUEST_TIMEOUT_MS_CONFIG, "5000");
4AdminClient admin = AdminClient.create(props);

4. Create Topic

Define the topic name, number of partitions, and replication factors for the topic:

java
1String topicName = "NewTopic";
2int numPartitions = 3;
3short replicationFactor = 1;
4
5NewTopic newTopic = new NewTopic(topicName, numPartitions, replicationFactor);
6
7try {
8    admin.createTopics(Collections.singleton(newTopic)).all().get();
9    System.out.println("Topic created successfully");
10} catch (InterruptedException | ExecutionException e) {
11    e.printStackTrace();
12} finally {
13    admin.close();
14}

Key Points Table

ParameterDescription
AdminClientConfigUsed to configure Kafka AdminClient.
BOOTSTRAP_SERVERS_CONFIGConnection strings of Kafka brokers.
REQUEST_TIMEOUT_MS_CONFIGTimeout config for client requests.
topicNameName of the topic being created.
numPartitionsNumber of partitions for the topic. Provides parallelism and redundancy.
replicationFactorNumber of replicas for a partition. Ensures data durability.

Additional Configurations and Considerations

While creating topics, other configurations like cleanup.policy, retention.ms, can be set according to your use case.

  • cleanup.policy: Determines how old data is deleted. Can be "delete" (delete records older than retention.ms) or "compact" (compact the records as per the key).
  • retention.ms: Controls how long records are stored before being deleted.

Use these configurations carefully as they can affect system performance and data integrity.

java
1Map<String, String> configs = new HashMap<>();
2configs.put("cleanup.policy", "compact");
3configs.put("retention.ms", "2000000");
4newTopic = new NewTopic(topicName, numPartitions, replicationFactor).configs(configs);

This complex yet highly customizable system allows Kafka and Java to be powerful tools in managing large scale, high throughput data systems.


Course illustration
Course illustration

All Rights Reserved.