- Lab
-
Libraries: If you want this lab, consider one of these libraries.
- Cloud

Connecting to Kafka Programmatically in Java
The real power of Kafka comes from the ability to write applications that utilize it. Kafka's Java APIs make it easy to build applications that work with Kafka data. Through this lab, we will have the opportunity to build a simple Java program that consumes data from a Kafka topic. Tackling this challenge will provide hands-on experience with the Kafka Java APIs and consuming data from a real cluster. After completing this introductory lab, we will be ready to move on to more complex scenarios involving both Kafka and Java code.

Lab Info
Table of Contents
-
Challenge
Clone the Starter Project from GitHub and Perform a Test Run
- Clone the starter project from GitHub.
cd ~/ git clone https://github.com/linuxacademy/content-ccdak-kafka-simple-consumer.git
- Perform a test to make sure the code is able to compile and run.
cd content-ccdak-kafka-simple-consumer/ ./gradlew run
The output should contain the message printed by the Main class:
Hello, world!
. -
Challenge
Implement a Consumer in the Main Class and Run It
- Add the Kafka Client Libraries as a project dependency in
build.gradle
with:
vi build.gradle
- In the
dependencies { ... }
block, add the following line:
implementation 'org.apache.kafka:kafka-clients:2.2.1'
- Edit the
Main
class:
vi src/main/java/com/linuxacademy/ccdak/kafkaSimpleConsumer/Main.java
- Implement a basic consumer that consumes messages from the topic and prints them to the screen:
package com.linuxacademy.ccdak.kafkaSimpleConsumer; import org.apache.kafka.clients.consumer.*; import java.util.Properties; import java.util.Arrays; import java.time.Duration; public class Main { public static void main(String[] args) { Properties props = new Properties(); props.setProperty("bootstrap.servers", "localhost:9092"); props.setProperty("group.id", "my-group"); props.setProperty("enable.auto.commit", "true"); props.setProperty("auto.commit.interval.ms", "1000"); props.setProperty("key.deserializer", "org.apache.kafka.common.serialization.StringDeserializer"); props.setProperty("value.deserializer", "org.apache.kafka.common.serialization.StringDeserializer"); KafkaConsumer<String, String> consumer = new KafkaConsumer<String, String>(props); consumer.subscribe(Arrays.asList("inventory_purchases")); while (true) { ConsumerRecords<String, String> records = consumer.poll(Duration.ofMillis(100)); for (ConsumerRecord<String, String> record : records) { System.out.printf("offset = %d, key = %s, value = %s%n", record.offset(), record.key(), record.value()); } } } }
- Run the code:
./gradlew run
The program should print a series of messages from the Kafka topic containing information about item purchases.
- Add the Kafka Client Libraries as a project dependency in
About the author
Real skill practice before real-world application
Hands-on Labs are real environments created by industry experts to help you learn. These environments help you gain knowledge and experience, practice without compromising your system, test without risk, destroy without fear, and let you learn from your mistakes. Hands-on Labs: practice your skills before delivering in the real world.
Learn by doing
Engage hands-on with the tools and technologies you’re learning. You pick the skill, we provide the credentials and environment.
Follow your guide
All labs have detailed instructions and objectives, guiding you through the learning process and ensuring you understand every step.
Turn time into mastery
On average, you retain 75% more of your learning if you take time to practice. Hands-on labs set you up for success to make those skills stick.