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

Configuring a Kafka Client

Kafka clients such as producers and consumers can be configured much like brokers and topics. In this hands-on lab, you will have the opportunity to explore the basics of configuring Kafka clients by making some configuration changes to a simple producer written in Java. After completing this lab, you will have some experience with the process of configuring Kafka clients programmatically.

Google Cloud Platform icon
Labs

Path Info

Level
Clock icon Intermediate
Duration
Clock icon 30m
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 Producer Source Code and Run it to Ensure that Everything Works

    1. Clone the producer source code into the home directory:
    cd ~/
    git clone https://github.com/linuxacademy/content-ccdak-kafka-client-config-lab.git
    
    1. Run the code to ensure it works before modifying it:
    cd content-ccdak-kafka-client-config-lab
    ./gradlew run
    
    1. To view the output, consume the records from the inventory_purchases topic:
    kafka-console-consumer --bootstrap-server localhost:9092 --topic inventory_purchases --property print.key=true --from-beginning
    
  2. Challenge

    Implement the Required Configuration Changes in the Producer and Run the Program to Test Them

    1. Edit the Main class of the producer source code:
    vi src/main/java/com/linuxacademy/ccdak/client/config/Main.java
    
    1. Add the necessary configurations to the props object before the producer is instantiated. The final code should look something like this:
    package com.linuxacademy.ccdak.client.config;
    
    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");
            props.put("buffer.memory", "12582912");
            props.put("connections.max.idle.ms", "300000");
    
            Producer<String, String> producer = new KafkaProducer<>(props);
            producer.send(new ProducerRecord<>("inventory_purchases", "apples", "1"));
            producer.send(new ProducerRecord<>("inventory_purchases", "apples", "3"));
            producer.send(new ProducerRecord<>("inventory_purchases", "oranges", "12"));
            producer.send(new ProducerRecord<>("inventory_purchases", "bananas", "25"));
            producer.send(new ProducerRecord<>("inventory_purchases", "pears", "15"));
            producer.send(new ProducerRecord<>("inventory_purchases", "apples", "6"));
            producer.send(new ProducerRecord<>("inventory_purchases", "pears", "7"));
            producer.send(new ProducerRecord<>("inventory_purchases", "oranges", "1"));
            producer.send(new ProducerRecord<>("inventory_purchases", "grapes", "56"));
            producer.send(new ProducerRecord<>("inventory_purchases", "oranges", "11"));
            producer.close();
        }
    
    }
    
    1. Execute the program:
    ./gradlew run
    
    1. Consume the records from the inventory_purchases topic to 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
    

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