Kafka — Hello Producer

In this example, we’ll make the use of Confluent Kafka to publish some messages on Kafka. In this example, we’ll use kafka-clients to talk to Confluent Kafka Server. Once the messages are published we’ll consume those meesage.

Create Topic

`kafka-topics — create — bootstrap-server localhost:9092 — replication-factor 1 — partitions 1 — topic hello-producer-topic`

Check Data —

`kafka-console-consumer — bootstrap-server localhost:9092 — topic hello-producer-topic — from-beginning`

pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>com.kafka.examples</groupId>
<artifactId>hello-producer</artifactId>
<version>2.3.0</version>

<properties>
<java.version>1.8</java.version>
<kafka.version>2.3.0</kafka.version>
</properties>

<build>
<plugins>
<!-- Maven Compiler Plugin-->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.0</version>
<configuration>
<source>${java.version}</source>
<target>${java.version}</target>
</configuration>
</plugin>
</plugins>
</build>

<dependencies>
<!-- Apache Kafka Clients-->
<dependency>
<groupId>org.apache.kafka</groupId>
<artifactId>kafka-clients</artifactId>
<version>${kafka.version}</version>
</dependency>

<!-- Apache Log4J2 binding for SLF4J -->
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-slf4j-impl</artifactId>
<version>2.11.0</version>
</dependency>
</dependencies>
</project>

AppConfig.java

class AppConfigs {
final static String applicationID = "HelloProducer";
final static String bootstrapServers = "localhost:9092";
final static String topicName = "hello-producer-topic";
final static int numEvents = 100;
}

HelloProducer.java

import org.apache.kafka.clients.producer.KafkaProducer;
import org.apache.kafka.clients.producer.ProducerConfig;
import org.apache.kafka.clients.producer.ProducerRecord;
import org.apache.kafka.common.serialization.IntegerSerializer;
import org.apache.kafka.common.serialization.StringSerializer;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import java.util.Properties;

public class HelloProducer {
private static final Logger logger = LogManager.getLogger();

public static void main(String[] args) {

logger.info("Creating Kafka Producer...");
Properties props = new Properties();
props.put(ProducerConfig.CLIENT_ID_CONFIG, AppConfigs.applicationID);
props.put(ProducerConfig.BOOTSTRAP_SERVERS_CONFIG, AppConfigs.bootstrapServers);
props.put(ProducerConfig.KEY_SERIALIZER_CLASS_CONFIG, IntegerSerializer.class.getName());
props.put(ProducerConfig.VALUE_SERIALIZER_CLASS_CONFIG, StringSerializer.class.getName());

KafkaProducer<Integer, String> producer = new KafkaProducer<>(props);

logger.info("Start sending messages...");
for (int i = 1; i <= AppConfigs.numEvents; i++) {
producer.send(new ProducerRecord<>(AppConfigs.topicName, i, "Simple Message-" + i));
}

logger.info("Finished - Closing Kafka Producer.");
producer.close();

}
}

Run the MainApp and then you can check data like below —

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store