Producer API Usage in Kafka

This guide covers the basics of using the Kafka Producer API, which is responsible for sending records to Kafka topics. Understanding how to configure and use the Producer API is crucial for writing data into Kafka.

1. What is a Kafka Producer?

A Kafka producer is an application or component that writes records to Kafka topics. Producers are responsible for sending data to Kafka and are typically configured with various properties to control their behavior.

2. Basic Configuration Options

Kafka producers can be configured using various properties to control their behavior. Key configuration options include:

3. Example: Basic Kafka Producer Configuration

Below is a Java example demonstrating how to configure and use a Kafka producer to send messages to a Kafka topic.


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.clients.producer.RecordMetadata;
import org.apache.kafka.common.serialization.StringSerializer;

import java.util.Properties;
import java.util.concurrent.Future;

public class KafkaProducerExample {
    public static void main(String[] args) {
        // Producer configuration
        Properties props = new Properties();
        props.put(ProducerConfig.BOOTSTRAP_SERVERS_CONFIG, "localhost:9092");
        props.put(ProducerConfig.KEY_SERIALIZER_CLASS_CONFIG, StringSerializer.class.getName());
        props.put(ProducerConfig.VALUE_SERIALIZER_CLASS_CONFIG, StringSerializer.class.getName());
        props.put(ProducerConfig.ACKS_CONFIG, "all");
        props.put(ProducerConfig.RETRIES_CONFIG, 3);
        props.put(ProducerConfig.LINGER_MS_CONFIG, 100);
        
        // Create KafkaProducer
        KafkaProducer producer = new KafkaProducer<>(props);
        
        // Create a ProducerRecord
        ProducerRecord record = new ProducerRecord<>("my-topic", "key", "value");
        
        // Send the record asynchronously
        Future future = producer.send(record);
        
        // Wait for the record to be sent and print metadata
        try {
            RecordMetadata metadata = future.get();
            System.out.printf("Sent record to topic %s partition %d with offset %d%n",
                              metadata.topic(), metadata.partition(), metadata.offset());
        } catch (Exception e) {
            e.printStackTrace();
        }
        
        // Close the producer
        producer.close();
    }
}
    

4. Kafka Producer Configuration Diagram

The following diagram illustrates the key components involved in Kafka producer configuration, including the Kafka broker connection, serialization, and acknowledgment settings.

Kafka Producer Configuration Diagram

Diagram: Kafka Producer Configuration Components

5. Conclusion

The Kafka Producer API is essential for writing data into Kafka topics. By understanding and applying the various configuration options, you can optimize your Kafka producers for efficient and reliable data production.