Writing to Kafka in Java

To write records to a Kafka cluster using the producer API in Java, you will need to follow these steps (written by OpenAI ChatGPT).

Add the following dependency to your pom.xml file.

<dependency>
    <groupId>org.apache.kafka</groupId>
    <artifactId>kafka-clients</artifactId>
    <version>2.7.0</version>
</dependency>

Import the necessary Kafka classes in your Java file.

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.StringSerializer;

Create a KafkaProducer object by setting the necessary configuration properties.

Properties props = new Properties();
props.put(ProducerConfig.BOOTSTRAP_SERVERS_CONFIG, "localhost:9092");
props.put(ProducerConfig.KEY_SERIALIZER_CLASS_CONFIG, StringSerializer.class);
props.put(ProducerConfig.VALUE_SERIALIZER_CLASS_CONFIG, StringSerializer.class);

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

Create a ProducerRecord object with the topic, key, and value you want to send.

String topic = "my-topic";
String key = "key";
String value = "value";

ProducerRecord<String, String> record = new ProducerRecord<>(topic, key, value);

Send the record to Kafka using the send() method of the KafkaProducer object.

producer.send(record);

Close the producer when you are finished.

producer.close();

That’s it! That’s how you write a record to a Kafka cluster using the producer API in Java.

Leave a reply

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