liveBook · Manning

GenericRecord

As explained in Kafka Streams in Action, Second Edition

A generic record is a schema-aware data container whose fields are accessed dynamically without a generated type-specific class.

GenericRecord is a schema-aware, dynamically accessed container for Avro fields. Unlike a generated specific record, it has no type-specific structure, so consumers inspect fields before reading them. section 3.2.9

A KafkaAvroDeserializer returns GenericRecord when SPECIFIC_AVRO_READER_CONFIG is false. The record still follows the schema and contains the same data, but callers work with it more like a HashMap than a generated Java class. section 3.2.9 section 3.7.1

Read and update fields dynamically:

  • Call hasField with the exact schema field name before attempting access. section 3.2.9
  • Call get with the schema spelling, such as name, real_name, or movies. section 3.2.9
  • Call put to update or set a value, such as genericRecord.put("real_name", "updated name"). section 3.2.9
  • Enumerate GenericRecord.getSchema().getFields() when the available structure is unknown. section 3.2.9

Choosing generic versus specific Avro records

A consumer configured with SPECIFIC_AVRO_READER_CONFIG=true can call avenger.getName(), avenger.getRealName(), and avenger.getMovies() on a generated AvroAvenger object. With the setting false, it receives GenericRecord and checks each field before calling get. This trades direct, structure-aware access for flexibility across arbitrary schema-shaped records. section 3.2.9

How Kafka Streams in Action, Second Edition covers it

How deep it goes
Explains deserialization choices, schema-exact access, field discovery, and mutation, but defers broader consumer configuration.
Examples
Demonstrates name, real_name, and movies fields alongside Avenger-specific getters and GenericRecord checks.
What it assumes
Assumes an Avro schema and Kafka serialization context, while clarifying specific-reader configuration and generic field access.
What you can do after
Apply it via KafkaAvroDeserializer by checking, reading, enumerating, and updating schema-defined fields dynamically.
How it connects
Connects GenericRecord to Avro, Schema Registry, KafkaAvroDeserializer, and generated specific records.

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.