Building a Kafka Consumer in Java

45 minutes
  • 2 Learning Objectives

About this Hands-on Lab

Kafka consumers provide the ability to process data that is stored in Kafka topics. Since you can write consumer code using the Consumer API, it is possible to build consumers that can do practically anything with your Kafka data. In this lab, you will have the opportunity to build a simple consumer that reads from a Kafka topic and writes data to a file on the disk. This lab will give you some hands-on experience with Kafka consumers. Hopefully, it will also spark your imagination about other tasks you might be able to accomplish with Kafka consumers!

Learning Objectives

Successfully complete this lab by achieving the following learning objectives:

Clone the Starter Project and Run It to Make Sure Everything Is Working
  1. Clone the starter project into the home directory:

    cd ~/
    git clone https://github.com/linuxacademy/content-ccdak-kafka-consumer-lab.git
  2. Run the code to ensure it works before modifying it:

    cd content-ccdak-kafka-consumer-lab/
    ./gradlew run

    Note: We should see a Hello, World! message in the output.

Implement the Consumer and Run It to Verify That It Works as Expected
  1. Edit the main class:

    vi src/main/java/com/linuxacademy/ccdak/consumer/Main.java
  2. Implement the consumer according to the provided specification:

    package com.linuxacademy.ccdak.consumer;
    
    import java.io.BufferedWriter;
    import java.io.FileWriter;
    import java.io.IOException;
    import java.time.Duration;
    import java.util.Arrays;
    import java.util.Properties;
    import org.apache.kafka.clients.consumer.ConsumerRecord;
    import org.apache.kafka.clients.consumer.ConsumerRecords;
    import org.apache.kafka.clients.consumer.KafkaConsumer;
    
    public class Main {
    
      public static void main(String[] args) {
          Properties props = new Properties();
          props.setProperty("bootstrap.servers", "localhost:9092");
          props.setProperty("group.id", "group1");
          props.setProperty("enable.auto.commit", "true");
          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<>(props);
          consumer.subscribe(Arrays.asList("inventory_purchases"));
          try {
              BufferedWriter writer = new BufferedWriter(new FileWriter("/home/cloud_user/output/output.dat", true));
              while (true) {
                  ConsumerRecords<String, String> records = consumer.poll(Duration.ofMillis(100));
                  for (ConsumerRecord<String, String> record : records) {
                      String recordString = "key=" + record.key() + ", value=" + record.value() + ", topic=" + record.topic() + ", partition=" + record.partition() + ", offset=" + record.offset();
                      System.out.println(recordString);
                      writer.write(recordString + "n");
                  }
                  consumer.commitSync();
                  writer.flush();
              }
          } catch (IOException e) {
              throw new RuntimeException(e);
          }
      }
    
    }
  3. Execute the program:

    ./gradlew run
  4. Verify that data is appearing in the output file:

    cat /home/cloud_user/output/output.dat

Additional Resources

Your supermarket company is using Kafka to process inventory data. They have a topic called inventory_purchases which is receiving data about the items being purchased and the quantity. However, there is still a legacy system which must ingest this data in the form of a data file.

You have been asked to create a consumer that will read the data from the topic and output to a data file. Each record should be on its own line, and should have the following format:

key=<key>, value=<value>, topic=<topic>, partition=<partition>, offset=<offset>

There is a starter project located in GitHub which you can use to implement your producer: https://github.com/linuxacademy/content-ccdak-kafka-consumer-lab.git. Clone this project and implement the consumer in its Main class. You can execute the main class from the project directory with the ./gradlew run command.

The output data should go into the following file: /home/cloud_user/output/output.dat.

If you get stuck, feel free to check out the solution video, or the detailed instructions under each objective. Good luck!

What are Hands-on Labs

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.

Get Started
Who’s going to be learning?

How many seats do you need?

  • $499 USD per seat per year
  • Billed Annually
  • Renews in 12 months

Ready to accelerate learning?

For over 25 licenses, a member of our sales team will walk you through a custom tailored solution for your business.


$2,495.00

Checkout
Sign In
Welcome Back!

Psst…this one if you’ve been moved to ACG!