Skip to content

Contact sales

By filling out this form and clicking submit, you acknowledge our privacy policy.
  • Labs icon Lab
  • A Cloud Guru
Google Cloud Platform icon
Labs

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.

Google Cloud Platform icon
Labs

Path Info

Level
Clock icon Intermediate
Duration
Clock icon 1h 15m
Published
Clock icon Oct 04, 2019

Contact sales

By filling out this form and clicking submit, you acknowledge our privacy policy.

Table of Contents

  1. Challenge

    Clone the Starter Project from GitHub and Perform a Test Run

    1. Clone the starter project from GitHub.
    cd ~/
    git clone https://github.com/linuxacademy/content-ccdak-kafka-simple-consumer.git
    
    1. 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!.

  2. Challenge

    Implement a Consumer in the Main Class and Run It

    1. Add the Kafka Client Libraries as a project dependency in build.gradle with:
    vi build.gradle
    
    1. In the dependencies { ... } block, add the following line:
    implementation 'org.apache.kafka:kafka-clients:2.2.1'
    
    1. Edit the Main class:
    vi src/main/java/com/linuxacademy/ccdak/kafkaSimpleConsumer/Main.java
    
    1. 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());
                }
            }
        }
    
    }
    
    1. Run the code:
    ./gradlew run
    

    The program should print a series of messages from the Kafka topic containing information about item purchases.

The Cloud Content team comprises subject matter experts hyper focused on services offered by the leading cloud vendors (AWS, GCP, and Azure), as well as cloud-related technologies such as Linux and DevOps. The team is thrilled to share their knowledge to help you build modern tech solutions from the ground up, secure and optimize your environments, and so much more!

What's a lab?

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.

Provided environment for hands-on practice

We will provide the credentials and environment necessary for you to practice right within your browser.

Guided walkthrough

Follow along with the author’s guided walkthrough and build something new in your provided environment!

Did you know?

On average, you retain 75% more of your learning if you get time for practice.

Start learning by doing today

View Plans