how to fetch a field in ConsumerRecord
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.
Introduction
To fetch data from a Kafka ConsumerRecord, call its accessor methods such as key(), value(), topic(), partition(), and offset(). The important distinction is whether you mean a field on the record metadata itself or a field inside the record's payload, because those are two different kinds of access.
Access the Built-In Record Fields
Kafka's ConsumerRecord already exposes the main metadata fields through methods.
These methods fetch record metadata and the deserialized key and value, not fields inside a structured payload such as JSON or Avro.
Fetch a Field Inside the Value Payload
If the record value itself contains structured data, you must parse the value after calling record.value().
For a JSON payload:
So the flow becomes:
- fetch the Kafka value with
record.value() - parse that value according to its serialization format
- read the field from the parsed structure
Headers Are a Separate Part of the Record
Kafka records can also carry headers. Those are not the same thing as key or value fields.
If you know the header name:
Headers are often used for tracing, schema hints, or routing metadata.
Know Your Deserializer Types
What you can "fetch" depends heavily on the configured deserializers. With StringDeserializer, record.value() returns a String. With a custom Avro or JSON deserializer, record.value() may already be a domain object.
For example, if you consume typed objects:
In that design, fetching a field is just ordinary Java object access after deserialization.
Handle Nulls Deliberately
Kafka records can have null keys or null values, depending on the producer and topic semantics. That means field access should sometimes be guarded instead of assumed.
This matters especially for tombstone records in compacted topics, where a null value is meaningful rather than accidental.
Other Useful Record Metadata
Besides key and value, ConsumerRecord exposes metadata that is often useful in debugging and processing logic:
- '
record.timestamp()' - '
record.serializedKeySize()' - '
record.serializedValueSize()' - '
record.headers()'
That information can help with audit logging, payload diagnostics, and throughput analysis.
Common Pitfalls
The most common mistake is trying to read a business field directly from ConsumerRecord when the field actually lives inside the serialized value payload. Another is forgetting that the meaning of record.value() depends on the configured deserializer. Developers also treat headers, key, and payload as if they were one flat structure, which makes debugging harder because Kafka keeps them separate by design.
Summary
- Use
record.key(),record.value(),record.topic(),record.partition(), andrecord.offset()for built-inConsumerRecordfields. - Parse
record.value()if the actual business field is inside JSON, Avro, or another payload format. - Use
record.headers()for Kafka headers. - Deserializer choice determines what type
record.value()returns. - Distinguish clearly between Kafka metadata fields and fields inside the message payload.
Related reading
- How to fetch offset id while consuming Kafka from Spark, save it in Cassandra and use it to restart Kafka?
- How to fetch recent messages from Kafka topic
- How to filter messages from Kafka based on headers value in AWS lambda?
- how to find consumer group coordinator in kafka?
- How to find RabbitMQ URL?
- How to find the root cause of high CPU usage of Kafka brokers?
- How to find the schema id from schema registry used for avro records, when reading from kafka consumer
- How to find which consumer is assigned to which partition of a topic in kafka?

System Design Fundamentals
Build a strong foundation in designing scalable, reliable distributed systems.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.