Kafka Topics and Partitions
Kafka organizes data into topics, which are streams of records. Each record in a topic consists of a key, a value, and an optional timestamp. Topics can be compared to categories or channels that data streams are published to.
Creating a Kafka Topic
To create a Kafka topic, you can use the Kafka AdminClient API in Java. The following code snippet demonstrates how to create a topic named my-topic
with 3 partitions and a replication factor of 1:
1import org.apache.kafka.clients.admin.AdminClient;
2import org.apache.kafka.clients.admin.AdminClientConfig;
3import org.apache.kafka.clients.admin.NewTopic;
4
5import java.util.Collections;
6import java.util.Properties;
7
8public class CreateKafkaTopic {
9 public static void main(String[] args) {
10 String topicName = "my-topic";
11 int numPartitions = 3;
12 short replicationFactor = 1;
13
14 Properties properties = new Properties();
15 properties.put(AdminClientConfig.BOOTSTRAP_SERVERS_CONFIG, "localhost:9092");
16
17 try (AdminClient adminClient = AdminClient.create(properties)) {
18 NewTopic newTopic = new NewTopic(topicName, numPartitions, replicationFactor);
19 adminClient.createTopics(Collections.singletonList(newTopic)).all().get();
20
21 System.out.println("Topic created successfully");
22 } catch (Exception e) {
23 e.printStackTrace();
24 }
25 }
26}
This code creates a topic named my-topic
with 3 partitions and a replication factor of 1 using the Kafka AdminClient API. Make sure to replace localhost:9092
with the appropriate Kafka broker address.
When you run the code, the output will indicate whether the topic was created successfully.
Kafka uses partitions to distribute the load across multiple brokers and make topics scalable. Each partition is an ordered and immutable sequence of records. It allows Kafka to parallelize the read and write operations for a topic.
Note: The code provided is just an example. In a production environment, you might need to modify the code based on your specific requirements.
xxxxxxxxxx
// Example Java code to create a Kafka topic
import org.apache.kafka.clients.admin.AdminClient;
import org.apache.kafka.clients.admin.AdminClientConfig;
import org.apache.kafka.clients.admin.NewTopic;
import java.util.Collections;
import java.util.Properties;
public class CreateKafkaTopic {
public static void main(String[] args) {
String topicName = "my-topic";
int numPartitions = 3;
short replicationFactor = 1;
Properties properties = new Properties();
properties.put(AdminClientConfig.BOOTSTRAP_SERVERS_CONFIG, "localhost:9092");
try (AdminClient adminClient = AdminClient.create(properties)) {
NewTopic newTopic = new NewTopic(topicName, numPartitions, replicationFactor);
adminClient.createTopics(Collections.singletonList(newTopic)).all().get();
System.out.println("Topic created successfully");
} catch (Exception e) {
e.printStackTrace();
}
}
}