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

Building a Kafka Producer in Java

Kafka producers allow you to write data to Kafka topics easily. Since you can build your own producers, you will be capable of developing logic to handle messages in ways that serve a variety of use cases. In this lab, you will have the opportunity to work with your own Kafka producer written in Java. You will solve a particular use case by building your producer logic — writing messages to multiple Kafka topics. This will give you some hands-on experience with building your own Kafka producers in Java.

Google Cloud Platform icon
Labs

Path Info

Level
Clock icon Intermediate
Duration
Clock icon 45m
Published
Clock icon Oct 18, 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 and Run It to Verify That It Works

    1. Clone the starter project into the home directory:
    cd ~/
    git clone https://github.com/linuxacademy/content-ccdak-kafka-producer-lab.git
    
    1. Run the code to ensure it works before modifying it:
    cd content-ccdak-kafka-producer-lab/
    ./gradlew run
    

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

  2. Challenge

    Implement the Producer and Run It to Verify That It Works

    1. Edit the main class:
    vi src/main/java/com/linuxacademy/ccdak/producer/Main.java
    
    1. Implement the producer according to the provided specification:
      package com.linuxacademy.ccdak.producer;
      
      import java.io.BufferedReader;
      import java.io.File;
      import java.io.FileReader;
      import java.io.IOException;
      import java.util.Properties;
      import org.apache.kafka.clients.producer.KafkaProducer;
      import org.apache.kafka.clients.producer.Producer;
      import org.apache.kafka.clients.producer.ProducerRecord;
      
      public class Main {
      
          public static void main(String[] args) {
              Properties props = new Properties();
              props.put("bootstrap.servers", "localhost:9092");
              props.put("key.serializer", "org.apache.kafka.common.serialization.StringSerializer");
              props.put("value.serializer", "org.apache.kafka.common.serialization.StringSerializer");
      
              props.put("acks", "all");
      
              Producer<String, String> producer = new KafkaProducer<>(props);
      
              try {
                  File file = new File(Main.class.getClassLoader().getResource("sample_transaction_log.txt").getFile());
                  BufferedReader br = new BufferedReader(new FileReader(file));
                  String line;
                  while ((line = br.readLine()) != null) {
                      String[] lineArray = line.split(":");
                      String key = lineArray[0];
                      String value = lineArray[1];
                      producer.send(new ProducerRecord<>("inventory_purchases", key, value));
                      if (key.equals("apples")) {
                          producer.send(new ProducerRecord<>("apple_purchases", key, value));
                      }
                  }
                  br.close();
              } catch (IOException e) {
                  throw new RuntimeException(e);
              }
      
              producer.close();
          }
      
      }
    
    1. Execute the program:
    ./gradlew run
    
    1. Consume the records from the inventory_purchases topic and verify that we can see the new records created by the producer:
     kafka-console-consumer --bootstrap-server localhost:9092 --topic inventory_purchases --property print.key=true --from-beginning
    
    1. Consume the records from the apple_purchases topic to verify that we can see the new records created by the producer:
    kafka-console-consumer --bootstrap-server localhost:9092 --topic apple_purchases --property print.key=true --from-beginning
    

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