Steps to Get Started with Apache Kafka
Step 1: Get Kafka
Download the latest Kafka release from the official Kafka website.
Extract the downloaded archive:
$ tar -xzf kafka_2.13-3.9.0.tgz
$ cd kafka_2.13-3.9.0
Step 2: Start the Kafka Environment
Prerequisites: Ensure Java 8+ is installed on your system.
Option 1: Kafka with KRaft
- Generate a Cluster UUID:
$ KAFKA_CLUSTER_ID="$(bin/kafka-storage.sh random-uuid)"
- Format Log Directories:
$ bin/kafka-storage.sh format --standalone -t $KAFKA_CLUSTER_ID -c config/kraft/reconfig-server.properties
- Start the Kafka Server:
$ bin/kafka-server-start.sh config/kraft/reconfig-server.properties
Alternatively, use Docker images for KRaft:
JVM-based Kafka Image:
$ docker pull apache/kafka:3.9.0
$ docker run -p 9092:9092 apache/kafka:3.9.0
- GraalVM-based Kafka Image:
$ docker pull apache/kafka-native:3.9.0
$ docker run -p 9092:9092 apache/kafka-native:3.9.0
Option 2: Kafka with ZooKeeper
- Start ZooKeeper:
$ bin/zookeeper-server-start.sh config/zookeeper.properties
- Start Kafka Broker (in another terminal):
$ bin/kafka-server-start.sh config/server.properties
Step 3: Create a Topic
- Create a topic to store events:
$ bin/kafka-topics.sh --create --topic quickstart-events --bootstrap-server localhost:9092
- Verify the topic details:
$ bin/kafka-topics.sh --describe --topic quickstart-events --bootstrap-server localhost:9092
Step 4: Write Events into the Topic
- Run the producer client:
$ bin/kafka-console-producer.sh --topic quickstart-events --bootstrap-server localhost:9092
- Enter events:
> This is my first event
> This is my second event
Step 5: Read Events from the Topic
- Run the consumer client (in a new terminal):
$ bin/kafka-console-consumer.sh --topic quickstart-events --from-beginning --bootstrap-server localhost:9092
You should see the events you added.
Step 6: Import/Export Data with Kafka Connect
Edit the plugin.path property in config/connect-standalone.properties to include the path of connect-file-3.9.0.jar:
$ echo "plugin.path=libs/connect-file-3.9.0.jar" >> config/connect-standalone.properties
- Create test data:
$ echo -e "foo\nbar" > test.txt
- Start connectors:
$ bin/connect-standalone.sh config/connect-standalone.properties config/connect-file-source.properties config/connect-file-sink.properties
- Verify the output:
$ more test.sink.txt
Step 7: Process Events with Kafka Streams
Use Kafka Streams to implement real-time applications. Example for WordCount in Java:
KStream<String, String> textLines = builder.stream("quickstart-events");
KTable<String, Long> wordCounts = textLines
.flatMapValues(line -> Arrays.asList(line.toLowerCase().split(" ")))
.groupBy((keyIgnored, word) -> word)
.count();
wordCounts.toStream().to("output-topic", Produced.with(Serdes.String(), Serdes.Long()));
Step 8: Terminate the Kafka Environment
- Stop the producer and consumer clients using Ctrl+C.
- Stop the Kafka broker and ZooKeeper (if used) using Ctrl+C.
- Clean up logs:
$ rm -rf /tmp/kafka-logs /tmp/zookeeper /tmp/kraft-combined-logs
Top comments (0)