Class ConsumerRecord<K,V>
java.lang.Object
org.apache.kafka.clients.consumer.ConsumerRecord<K,V>
A key/value pair to be received from Kafka. This also consists of a topic name and a partition number from which the record is being received, an offset that points to the record in a Kafka partition, and a timestamp as marked by the corresponding ProducerRecord.
Thread Safety
This consumer record is not thread-safe. Concurrent access to aConsumerRecord instance by multiple threads may result in undefined behavior, including but not limited to the following: - Throwing
ConcurrentModificationException(e.g., when concurrently modifyingheaders()). - Data corruption or logical errors (e.g., inconsistent state of
headersorvalue). - Visibility issues (e.g., modifications by one thread not being visible to another thread).
In particular, the headers() method returns a mutable collection of headers. If multiple threads access or modify these headers concurrently, it may lead to race conditions or inconsistent states. It is the responsibility of the user to ensure that multi-threaded access is properly synchronized.
Refer to the KafkaConsumer documentation for more details on multi-threaded consumption and processing strategies.
Field Summary
FieldsConstructor Summary
ConstructorsConstructorDescriptionConsumerRecord(String topic, int partition, long offset, long timestamp, org.apache.kafka.common.record.TimestampType timestampType, int serializedKeySize, int serializedValueSize, K key, V value, Headers headers, Optional<Integer> leaderEpoch) Creates a record to be received from a specified topic and partition.ConsumerRecord(String topic, int partition, long offset, long timestamp, org.apache.kafka.common.record.TimestampType timestampType, int serializedKeySize, int serializedValueSize, K key, V value, Headers headers, Optional<Integer> leaderEpoch, Optional<Short> deliveryCount) Creates a record to be received from a specified topic and partition.ConsumerRecord(String topic, int partition, long offset, K key, V value) Creates a record to be received from a specified topic and partition (provided for compatibility with Kafka 0.9 before the message format supported timestamps and before serialized metadata were exposed).Method Summary
Modifier and TypeMethodDescriptionGet the delivery count for the record if available.headers()The headers (never null)key()The key (or null if no key is specified)Get the leader epoch for the record if availablelongoffset()The position of this record in the corresponding Kafka partition.intThe partition from which this record is receivedintThe size of the serialized, uncompressed key in bytes.intThe size of the serialized, uncompressed value in bytes.longThe timestamp of this record, in milliseconds elapsed since unix epoch.org.apache.kafka.common.record.TimestampTypeThe timestamp type of this recordtopic()The topic this record is received from (never null)toString()value()The value
Field Details
NO_TIMESTAMP
public static final long NO_TIMESTAMP- See Also:
NULL_SIZE
public static final int NULL_SIZE- See Also:
Constructor Details
ConsumerRecord
Creates a record to be received from a specified topic and partition (provided for compatibility with Kafka 0.9 before the message format supported timestamps and before serialized metadata were exposed).- Parameters:
topic- The topic this record is received frompartition- The partition of the topic this record is received fromoffset- The offset of this record in the corresponding Kafka partitionkey- The key of the record, if one exists (null is allowed)value- The record contents
ConsumerRecord
public ConsumerRecord(String topic, int partition, long offset, long timestamp, org.apache.kafka.common.record.TimestampType timestampType, int serializedKeySize, int serializedValueSize, K key, V value, Headers headers, Optional<Integer> leaderEpoch) Creates a record to be received from a specified topic and partition.- Parameters:
topic- The topic this record is received frompartition- The partition of the topic this record is received fromoffset- The offset of this record in the corresponding Kafka partitiontimestamp- The timestamp of the record.timestampType- The timestamp typeserializedKeySize- The length of the serialized keyserializedValueSize- The length of the serialized valuekey- The key of the record, if one exists (null is allowed)value- The record contentsheaders- The headers of the recordleaderEpoch- Optional leader epoch of the record (may be empty for legacy record formats)
ConsumerRecord
public ConsumerRecord(String topic, int partition, long offset, long timestamp, org.apache.kafka.common.record.TimestampType timestampType, int serializedKeySize, int serializedValueSize, K key, V value, Headers headers, Optional<Integer> leaderEpoch, Optional<Short> deliveryCount) Creates a record to be received from a specified topic and partition.- Parameters:
topic- The topic this record is received frompartition- The partition of the topic this record is received fromoffset- The offset of this record in the corresponding Kafka partitiontimestamp- The timestamp of the record.timestampType- The timestamp typeserializedKeySize- The length of the serialized keyserializedValueSize- The length of the serialized valuekey- The key of the record, if one exists (null is allowed)value- The record contentsheaders- The headers of the recordleaderEpoch- Optional leader epoch of the record (may be empty for legacy record formats)deliveryCount- Optional delivery count of the record (may be empty when deliveries not counted)
Method Details
topic
The topic this record is received from (never null)partition
public int partition()The partition from which this record is receivedheaders
The headers (never null)key
The key (or null if no key is specified)value
The valueoffset
public long offset()The position of this record in the corresponding Kafka partition.timestamp
public long timestamp()The timestamp of this record, in milliseconds elapsed since unix epoch.timestampType
public org.apache.kafka.common.record.TimestampType timestampType()The timestamp type of this recordserializedKeySize
public int serializedKeySize()The size of the serialized, uncompressed key in bytes. If key is null, the returned size is -1.serializedValueSize
public int serializedValueSize()The size of the serialized, uncompressed value in bytes. If value is null, the returned size is -1.leaderEpoch
deliveryCount
toString