Search code examples
apache-kafkakafka-producer-api

Kafka Keyless Producer In Java


I am new to the kafka eco system and in my case I'm using a Java producer but have no need for sending a key along with the record value which is serialized Avro. Is there a way to build a Java Producer to will not send keys, or are keys a requirement when sending messages in Kafka?


Solution

  • ProducerRecord has several constructors, one of them don't have value for the key, so you don't have to indicate it.

    Example:

    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("linger.ms", 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");
    
    Producer<String, String> producer = new KafkaProducer<>(props);
    for(int i = 0; i < 100; i++)
        producer.send(new ProducerRecord<>("my-topic", "myValue"));
    
    producer.close();