3. Kafka Producers: Writing Messages to Kafka
3.2 Constructing a producer
Built in: ByteArraySerializer ("which doesn't do much"), StringSerializer, IntegerSerializer, and much more.
Three mandatory properties
| Property | Purpose | Notes |
|---|---|---|
bootstrap.servers | host:port pairs for the initial connection | Doesn't need all brokers — the producer gets more info after connecting. Include at least two, so if one broker is down the producer can still reach the cluster |
key.serializer | Class name implementing org.apache.kafka.common.serialization.Serializer, used to turn the key object into bytes | Required even if you only send values — use type Void with VoidSerializer |
value.serializer | Same, for the value object |
Why serializers exist at all: "Kafka brokers expect byte arrays as keys and values. However, the producer interface allows, using parameterized types, any Java object to be sent as a key and value. This makes for very readable code, but it also means that the producer has to know how to convert these objects to byte arrays."
Built in: ByteArraySerializer ("which doesn't do much"), StringSerializer, IntegerSerializer, and much more.
Properties kafkaProps = new Properties();
kafkaProps.put("bootstrap.servers", "broker1:9092,broker2:9092");
kafkaProps.put("key.serializer",
"org.apache.kafka.common.serialization.StringSerializer");
kafkaProps.put("value.serializer",
"org.apache.kafka.common.serialization.StringSerializer");
producer = new KafkaProducer<String, String>(kafkaProps);"With such a simple interface, it is clear that most of the control over producer behavior is done by setting the correct configuration properties."