Create the Target Topic
Before a Kafka application can publish messages, the target topic must be provisioned on the cluster. In this step, you will create a topic named count-topic that your Java application will connect to and publish metrics to.
Step 1: Create the count-topic Topic
Execute the following command in the terminal to create the topic on the Kafka broker running locally at localhost:9092. Note that since this is a single-broker sandbox, we will configure a replication factor of 1.
# Create a topic with 3 partitions and a replication factor of 1
kafka-topics --create --bootstrap-server localhost:9092 --partitions 3 --replication-factor 1 --topic count-topic
Step 2: Verify the Topic
Confirm the topic exists and has been configured with the correct number of partitions:
# List all topics on the broker
kafka-topics --list --bootstrap-server localhost:9092
# Describe the count-topic details
kafka-topics --describe --bootstrap-server localhost:9092 --topic count-topic
Once the topic is created and verified, click Verify step below to move on to implementing your Java application!
Hint
Use `kafka-topics --create --bootstrap-server localhost:9092 --partitions 3 --replication-factor 1 --topic count-topic`.
Implement the Java Producer
Now, you will write a Java application that connects to your Kafka broker using the official Apache Kafka client libraries.
You will implement a simple command-line application that acts as a Producer, publishing 100 messages containing sequence numbers to count-topic.
Step 1: Open the Main class in the Editor
Locate the file path /home/sandbox/kafka-java-connect/src/main/java/com/linuxacademy/ccdak/kafkaJavaConnect/Main.java in the Editor tab on the right side of the screen. Open it to find the Java class skeleton.
Step 2: Implement the Producer Logic
Replace the skeleton code in Main.java with the following implementation. This code configures the producer properties, initializes a KafkaProducer, sends 100 consecutive records to the topic count-topic, and safely closes the producer resources.
package com.linuxacademy.ccdak.kafkaJavaConnect;
import org.apache.kafka.clients.producer.*;
import java.util.Properties;
public class Main {
public static void main(String[] args) {
// 1. Configure the Producer properties
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");
// 2. Instantiate the Producer
Producer<String, String> producer = new KafkaProducer<>(props);
// 3. Send 100 messages sequentially
for (int i = 0; i < 100; i++) {
String key = "count";
String value = Integer.toString(i);
// Create a ProducerRecord specifying topic, key, and value
ProducerRecord<String, String> record = new ProducerRecord<>("count-topic", key, value);
// Publish record asynchronously
producer.send(record);
}
// 4. Close the producer to flush all records and release resources
producer.close();
System.out.println("Finished producing 100 records!");
}
}
Make sure to Save the file inside your editor after editing.
Click Verify step below to check if your Java source code is correctly structured.
Hint
Open `/home/sandbox/kafka-java-connect/src/main/java/com/linuxacademy/ccdak/kafkaJavaConnect/Main.java` in the Editor and implement the producer logic.
Run and Verify the Java Producer
With your producer code fully implemented and saved, it's time to build and run the application, then consume the records from Kafka to verify they arrived.
Step 1: Navigate and Run Gradle
In your active terminal tab, change to the project directory and run the Gradle run task to execute your Java program:
# Navigate to the project folder
cd /home/sandbox/kafka-java-connect
# Run the Gradle application
gradle run
This compiles your Main.java class, downloads any missing dependencies, and runs the application. You should see Finished producing 100 records! printed at the end.
Step 2: Read Messages with a CLI Consumer
To verify the messages were successfully published, use the standard console consumer tool to read them from the topic count-topic starting from the beginning:
# Read all records from the beginning from count-topic
kafka-console-consumer --bootstrap-server localhost:9092 --topic count-topic --from-beginning --max-messages 100
You should see 100 numbers (from 0 to 99) scroll rapidly down the screen.
Click Verify step below to complete the lab!
Hint
Run `gradle run` in the terminal inside `/home/sandbox/kafka-java-connect` to execute your producer, then verify that 100 messages exist in count-topic.