KafkaAvroSerializer encodes Avro records for a Kafka producer and communicates with Schema Registry through its configured HTTP endpoint. The producer supplies the serializer class; KafkaProducer then calls serialize and sends the resulting bytes. section 3.2.9 section 3.7.1
Configure the producer around three related concerns:
- Set VALUE_SERIALIZER_CLASS_CONFIG to KafkaAvroSerializer.class for generated Avro objects. section 3.2.9 section 3.7.1
- Set SCHEMA_REGISTRY_URL_CONFIG to the Schema Registry HTTP endpoint, such as http://localhost:8081. section 3.2.9 section 3.7.1
- Populate only fields and types permitted by the generated Avro class, preserving the producer-consumer contract enforced through schema registration and compatibility. section 3.2.9
Listing 3.40 Required configuration for Avro
// producer properties producerProps.put(ProducerConfig.KEY_SERIALIZER_CLASS_CONFIG, StringSerializer.class); #1 producerProps.put(ProducerConfig.VALUE_SERIALIZER_CLASS_CONFIG, KafkaAvroSerializer.class); #2 producerProps.put(AbstractKafkaSchemaSerDeConfig.SCHEMA_REGISTRY_URL_CONFIG, "http://localhost:8081"); #3 //consumer properties are set separately on the consumer props.put(ConsumerConfig.KEY_DESERIALIZER_CLASS_CONFIG, StringDeserializer.class); #4 props.put(ConsumerConfig.VALUE_DESERIALIZER_CLASS_CONFIG, KafkaAvroDeserializer.class); #5 props.put(KafkaAvroDeserializerConfig.SPECIFIC_AVRO_READER_CONFIG, true); #6 props.put(AbstractKafkaSchemaSerDeConfig.SCHEMA_REGISTRY_URL_CONFIG, "http://localhost:8081"); #7
Use the producer configuration as the minimal working setup; the corresponding consumer uses KafkaAvroDeserializer and can request SpecificRecord instances with SPECIFIC_AVRO_READER_CONFIG=true. section 3.7.1
Producing a generated Avro record
Create a generated Avro object such as AvengerAvro.newBuilder().setName("Black Widow").setRealName("Natasha Romanova").setMovies(List.of("Avengers", "Infinity Wars", "End Game")).build(), then run the Avro producer to publish records to the avro-avengers topic. section 3.2.9
