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`


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



<!-- Maven Compiler Plugin-->

<!-- Apache Kafka Clients-->

<!-- Apache Log4J2 binding for SLF4J -->


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;


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.");


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




Java Developer and enthusiast

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Change Model/Layer name in TensorFlow Keras

Things I learnt as Product Manager in Mobile App Development

FMOD Unity: Beat-Mapping

How to use arduino control board LY-F2 and STONE serial display to implement piano key project

Strapi: Backing up data in a Strapi installation

Unity: Creating restart message

Git and GitHub (Part-1)

AWS Lambda Functions and Layers

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


Java Developer and enthusiast

More from Medium

Kafka and Its Components

Kafka connect overview and build a data pipeline using Mysql and Kafka Connect

Apache Kafka’s 3 Main Functions

Kafka — Message Filter