SpecificRecord is the common Java interface implemented by generated Avro classes. It lets a Kafka producer or consumer handle several concrete event types when an Avro union is the topic value schema. section 4.6.1
Use the interface at the Kafka boundary, then resolve each record’s concrete type before processing it:
- Declare the producer as Producer<String, SpecificRecord> when values may be TruckEvent, PlaneEvent, or DeliveryEvent. section 4.6.1
- Configure KafkaAvroDeserializer with SPECIFIC_AVRO_READER_CONFIG=true and consume as Consumer<SpecificRecord>. section 4.6.2 section 3.7.1
- Use instanceof checks, then cast to PlaneEvent, TruckEvent, or DeliveryEvent to apply event-specific logic. section 4.6.2
Producing an Avro union
Set KafkaAvroSerializer as the value serializer, disable schema autoregistration, enable the latest schema version, and instantiate KafkaProducer<String, SpecificRecord>. A consumer can then retrieve the generated concrete class and branch on its type. section 4.6.1
Listing 4.17 Determining the concrete type of a record using Avro union schemas
SpecificRecord avroRecord = record.value();
if (avroRecord instanceof PlaneEvent) {
PlaneEvent planeEvent = (PlaneEvent) avroRecord;
....
} else if (avroRecord instanceof TruckEvent) {
TruckEvent truckEvent = (TruckEvent) avroRecord;
....
} else if (avroRecord instanceof DeliveryEvent) {
DeliveryEvent deliveryEvent = (DeliveryEvent) avroRecord;
....
}
Inspect the concrete-type dispatch pattern for records returned through SpecificRecord. section 4.6.2
