Basic Kafka Producer, How to write records in Java

Basic Kafka Producer, How to write records in Java

You will learn

  • How to configure Kafka Consumer properties
  • How to create Kafka consumer
  • How to send records with Kafka Consumer

The first example shows how to print out records from Kafka to the console.

We will have to set the properties for a Kafka Consumer Object and create it.

Then we subscribe to the topics of our choice

Setting Properties

Properties props = new Properties();
props.put(“bootstrap.servers”, “localhost:9092”);
props.put(“acks”, “all”);
props.put(“retries”, 0);
props.put(“batch.size”, 16384);
props.put(“”, 1);
props.put(“buffer.memory”, 33554432);
props.put(“key.serializer”, “org.apache.kafka.common.serialization.StringSerializer”);
props.put(“value.serializer”, “org.apache.kafka.common.serialization.StringSerializer”);

Create an Instance of a Kafka Producer, with the properties we just set

Producer producer = new KafkaProducer<>(props);

Cal the send() function on the producer object to send a record to the Kafka topic wetter

for (int i = 0; i < 100; i++) { producer.send(new ProducerRecord(“wetter”, Integer.toString(x), Integer.toString(i)));


Leave a Reply

Your email address will not be published. Required fields are marked *