Featured Article

Master Apache Kafka with Java: A Comprehensive Tutorial for Beginners

Kenneth Jul 13, 2026

Ever considered leveraging Apache Kafka for enterprise messaging? If you're a Java developer, understanding Kafka's usage and best practices is a game-changer. Let's embark on this comprehensive tutorial journey to master Apache Kafka with Java.

Apache Kafka Producer in Java | Simple Java Kafka Producer Example
Apache Kafka Producer in Java | Simple Java Kafka Producer Example

Kafka’s real-time message processing and asynchronous communication make it a boon for distributed systems. In this tutorial, we'll demystify Kafka, explore its key concepts, and guide you through implementing Java applications that harness Kafka's power.

Kafka intro
Kafka intro

Setting Up Apache Kafka with Java

Before we stitch code together, let's ensure Kafka is set up on your system. We'll use the official Apache Kafka distribution and its convenient Docker images.

Brij kishore Pandey on LinkedIn: 𝗪𝗵𝗮𝘁 𝗶𝘀 𝗞𝗮𝗳𝗸𝗮? Apache Kafka, initially developed by LinkedIn… | 25 comments
Brij kishore Pandey on LinkedIn: 𝗪𝗵𝗮𝘁 𝗶𝘀 𝗞𝗮𝗳𝗸𝗮? Apache Kafka, initially developed by LinkedIn… | 25 comments

First, install Docker (if you haven't already) and pull the Kafka image with Zookeeper bundled. Then run the containers with the following commands:

Kafka Environment Setup

Hoja de referencia Kafka
Hoja de referencia Kafka

Running Kafka and Zookeeper with Docker involves a few commands. First, create a network:

docker network create kafka-network

Then, run Zookeeper and Kafka:

docker run -d --name zookeeper --network kafka-network -p 2181:2181 -e ZOOKEEPER_ID=1 -e ZOOKEEPER_CLIENT_PORT=2181 confluentinc/cp-zookeeper:latest docker run -d --name kafka --network kafka-network -p 9092:9092 -e KAFKA_ADVERTISED_LISTENERS=PLAINTEXT://localhost:9092 -e KAFKA_ZOOKEEPER_CONNECT=zookeeper:2181 -e KAFKAOffsetsTopicReplicationFactor=1 -e KAFKAGroupInitialReplicationFactor=1 -e KAFKA TransactionStateLogReplicationFactor=1 confluentinc/cp-kafka:latest

Installing Kafka Java Client

the diagram shows how to use an appliance for storage and data in this application
the diagram shows how to use an appliance for storage and data in this application

The Kafka Java client library enables your Java applications to produce and consume messages from Kafka topics. To include it in your project, add this Maven dependency:

<dependency> <groupId>org.apache.kafka</groupId> <artifactId>kafka-clients</artifactId> <version>2.7.1</version> </dependency>

Producers and Consumers in Kafka Java Applications

In Kafka, data flow is driven by producers and consumers. Let's create simple examples of both.

I Took 7 Apache Kafka Courses Online - Here’s Top 5 Which Actually Helped Me?
I Took 7 Apache Kafka Courses Online - Here’s Top 5 Which Actually Helped Me?

Creating a Kafka Producer

Here's a basic Kafka producer code snippet for sending messages to a topic:

System Design Basics - Apache Kafka vs RabbitMQ vs ActiveMQ
System Design Basics - Apache Kafka vs RabbitMQ vs ActiveMQ
My Favorite FREE Courses to Learn Apache Kafka in 2025
My Favorite FREE Courses to Learn Apache Kafka in 2025
I Took 7 Apache Kafka Courses Online - Here’s Top 5 Which Actually Helped Me?
I Took 7 Apache Kafka Courses Online - Here’s Top 5 Which Actually Helped Me?
a diagram showing the architecture of a kafka architecture, with different types of products and
a diagram showing the architecture of a kafka architecture, with different types of products and
How does Apache Kafka work? Why is Kafka So fast?
How does Apache Kafka work? Why is Kafka So fast?
How To Work With Apache Kafka In Spring Boot?
How To Work With Apache Kafka In Spring Boot?
the top 5 kafka use cases
the top 5 kafka use cases
Putting Apache Kafka To Use: A Practical Guide To Building an Event Streaming Platform (Part 1) | Confluent
Putting Apache Kafka To Use: A Practical Guide To Building an Event Streaming Platform (Part 1) | Confluent
Apache Kafka Infographic
Apache Kafka Infographic

Properties props = new Properties(); props.put(ProducerConfig.BOOTSTRAP_SERVERS_CONFIG, "localhost:9092"); props.put(ProducerConfig.KEY_SERIALIZER_CLASS_CONFIG, "org.apache.kafka.common.serialization.StringSerializer"); props.put(ProducerConfig.VALUE_SERIALIZER_CLASS_CONFIG, "org.apache.kafka.common.serialization.StringSerializer"); Producer producer = new KafkaProducer<>(props); String topic = "test-topic"; for (int i = 0; i < 100; i++) { producer.send(new ProducerRecord<>(topic, "Message " + i), new Callback() { public void onCompletion(RecordMetadata metadata, Exception exception) { if (exception != null) { System.err.println("Error sending message: " + exception); } else { System.out.println("Message sent: " + "Topic=" + metadata.topic() + ", " + "Partition=" + metadata.partition() + ", " + "Offset=" + metadata.offset() + ", " + "Timestamp=" + new DateTime(metadata.timestamp()).toString()); } } }); } producer.close();

Creating a Kafka Consumer

Here's a simple Kafka consumer reading from the topic created by the producer:

Properties props = new Properties(); props.put(ConsumerConfig.BOOTSTRAP_SERVERS_CONFIG, "localhost:9092"); props.put(ConsumerConfig.GROUP_ID_CONFIG, "test-group"); props.put(ConsumerConfig.KEY_DESERIALIZER_CLASS_CONFIG, "org.apache.kafka.common.serialization.StringDeserializer"); props.put(ConsumerConfig.VALUE_DESERIALIZER_CLASS_CONFIG, "org.apache.kafka.common.serialization.StringDeserializer"); String topic = "test-topic"; Consumer consumer = new KafkaConsumer<>(props); consumer.subscribe(Collections.singleton(topic)); while (true) { ConsumerRecords records = consumer.poll(Duration.ofMillis(100)); for (ConsumerRecord record : records) { System.out.println("Received message: " + "Topic=" + record.topic() + ", " + "Partition=" + record.partition() + ", " + "Offset=" + record.offset() + ", " + "Timestamp=" + new DateTime(record.timestamp()).toString() + ", " + "Key=" + record.key() + ", " + "Value=" + record.value()); } } consumer.close();

There's much more to explore in Apache Kafka, like producers and consumers with avro serialization, Kafka Streams, Confluent Enterprise features, and Kafka Connect. We'll save those for future tutorials. For now, continue practicing, and happy coding!