Mark As Completed Discussion

Kafka Architecture

Kafka is designed as a distributed system and consists of several key components that work together to provide its powerful capabilities. Let's explore the architecture of Kafka and its components.

Topics

At the core of Kafka's architecture are the topics. A topic represents a stream of records organized in partitions. Each partition is an ordered and immutable sequence of records that can be continually appended to.

To create a topic, you can use the Kafka AdminClient API in Java. Here's an example code snippet that demonstrates how to create a topic:

SNIPPET
1// Kafka Topics
2import org.apache.kafka.clients.admin.AdminClient;
3import org.apache.kafka.clients.admin.AdminClientConfig;
4import org.apache.kafka.clients.admin.NewTopic;
5
6import java.util.Collections;
7import java.util.Properties;
8
9public class KafkaTopicExample {
10    public static void main(String[] args) {
11        String topicName = "my-topic";
12        int numPartitions = 3;
13        short replicationFactor = 1;
14
15        Properties adminClientProps = new Properties();
16        adminClientProps.put(AdminClientConfig.BOOTSTRAP_SERVERS_CONFIG, "localhost:9092");
17
18        try (AdminClient adminClient = AdminClient.create(adminClientProps)) {
19            // Create a new topic
20            NewTopic newTopic = new NewTopic(topicName, numPartitions, replicationFactor);
21            adminClient.createTopics(Collections.singletonList(newTopic)).all().get();
22
23            System.out.println("Topic created successfully");
24        } catch (Exception e) {
25            e.printStackTrace();
26        }
27    }
28}
JAVA
OUTPUT
:001 > Cmd/Ctrl-Enter to run, Cmd/Ctrl-/ to comment