02: Apache Kafka example with Java Producer & Consumer Tutorial

Apache Kafka with Java getting started tutorial demonstrates how quickly you can get started with Kafka using Docker. This extends Apache Kafka example with Java – getting started tutorial – Part 1.

Apache Kafka Producer & Consumer Example

Apache Kafka Producer & Consumer Example

Step 1: As discussed in part-1, stand-up the Kafka, Zookeeper & Apache Kafka Cluster Visualization (AKHQ) on a Docker container.

Java Producer class ProducerApp.java

Step 2: Create the Java class to produce & send a message to the Kafka broker topic “my-first-topic“.

Run the Consumer class ConsumerApp.java

Step 3: Start the ConsumerApp.java class that was created in part-1.

Run the Producer class ProducerApp.java

Step 4: Run the ProducerApp.java class to send a test message.

Step 5: In the ConsumerApp.java console you see the message printed.

If you double-click on the “my-first-topic” in localhost:9099 you can see the messages published with the offsets.

Apache Kafka messages with offsets

Apache Kafka messages with offsets

View the message on akHQ.io UI

Apache Kafka - akhq.io

Apache Kafka – akhq.io

Step 6: If you run the producer “ProducerApp.java” two more times, you can see the offset count increase in the “ConsumerApp.java” console & in the akhq.io UI.

Go over the different menu items in the akhq.io UI at localhost:9099 and pay attention to the “offset“, “partition” & “Consumer Groups“.

Apache Kafka Interview Q&As

1. 8 Apache Kafka must-know basics interview Q&As


300+ Java Interview FAQs

Tutorials on Java & Big Data