liveBook · Manning

KafkaAvroSerializer

As explained in Kafka Streams in Action, Second Edition

KafkaAvroSerializer is a Kafka serializer that encodes Avro records and communicates with Schema Registry to obtain their schemas.

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

How Kafka Streams in Action, Second Edition covers it

How deep it goes
Explains required serializer and endpoint properties, then stops short of broader producer configuration deferred to later treatment.
Examples
Uses generated AvengerAvro records, Black Widow data, the avro-avengers topic, and Gradle producer execution.
What it assumes
Assumes generated Avro classes and a running Kafka broker and Schema Registry, while clarifying the producer's serializer boundary.
What you can do after
Operate/configure a Kafka producer that publishes generated Avro records through Schema Registry.
How it connects
Connects Avro records with KafkaProducer, Schema Registry, KafkaAvroDeserializer, SpecificRecord, and GenericRecord.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Kafka Streams in Action, Second Edition. The section links point to the parts of the book it draws on.