liveBook · Manning

SpecificRecord

As explained in Kafka Streams in Action, Second Edition

SpecificRecord is an Avro Java interface implemented by generated classes for records with a known schema.

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

How Kafka Streams in Action, Second Edition covers it

How deep it goes
Explains generic declarations, serializer settings, deserializer settings, and concrete-type checks without detailing generated-class internals.
Examples
Uses TruckEvent, PlaneEvent, and DeliveryEvent with producer and consumer configuration listings.
What it assumes
Assumes Java Kafka client and Avro-generated classes, while showing the required serializer, deserializer, and schema settings.
What you can do after
Build it from scratch by producing and consuming multiple generated Avro event classes through one Kafka value type.
How it connects
Connects Avro union schemas with KafkaProducer, KafkaConsumer, KafkaAvroSerializer, KafkaAvroDeserializer, 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.