- Lab
- A Cloud Guru
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.
Path Info
Table of Contents
-
Challenge
Clone the Producer Source Code and Run it to Ensure that Everything Works
- Clone the producer source code into the home directory:
cd ~/ git clone https://github.com/linuxacademy/content-ccdak-kafka-client-config-lab.git
- Run the code to ensure it works before modifying it:
cd content-ccdak-kafka-client-config-lab ./gradlew run
- 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
-
Challenge
Implement the Required Configuration Changes in the Producer and Run the Program to Test Them
- Edit the
Main
class of the producer source code:
vi src/main/java/com/linuxacademy/ccdak/client/config/Main.java
- 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(); } }
- Execute the program:
./gradlew run
- 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
- Edit the
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.