Class ConsumerRecord<K,​V>


  • public class ConsumerRecord<K,​V>
    extends java.lang.Object
    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.
    • Constructor Summary

      Constructors 
      Constructor Description
      ConsumerRecord​(java.lang.String topic, int partition, long offset, long timestamp, org.apache.kafka.common.record.TimestampType timestampType, long checksum, int serializedKeySize, int serializedValueSize, K key, V value)
      Creates a record to be received from a specified topic and partition (provided for compatibility with Kafka 0.10 before the message format supported headers).
      ConsumerRecord​(java.lang.String topic, int partition, long offset, long timestamp, org.apache.kafka.common.record.TimestampType timestampType, java.lang.Long checksum, int serializedKeySize, int serializedValueSize, K key, V value, Headers headers)
      Creates a record to be received from a specified topic and partition
      ConsumerRecord​(java.lang.String topic, int partition, long offset, long timestamp, org.apache.kafka.common.record.TimestampType timestampType, java.lang.Long checksum, int serializedKeySize, int serializedValueSize, K key, V value, Headers headers, java.lang.String producer)
      The constructor is added for backward compatibility
      ConsumerRecord​(java.lang.String topic, int partition, long offset, long timestamp, org.apache.kafka.common.record.TimestampType timestampType, java.lang.Long checksum, int serializedKeySize, int serializedValueSize, K key, V value, Headers headers, java.util.Optional<java.lang.Integer> leaderEpoch)  
      ConsumerRecord​(java.lang.String topic, int partition, long offset, long timestamp, org.apache.kafka.common.record.TimestampType timestampType, java.lang.Long checksum, int serializedKeySize, int serializedValueSize, K key, V value, Headers headers, java.util.Optional<java.lang.Integer> leaderEpoch, java.lang.String producer)
      Creates a record to be received from a specified topic and partition
      ConsumerRecord​(java.lang.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).
      ConsumerRecord​(java.lang.String topic, int partition, long offset, K key, V value, long timestamp, java.lang.String producer)
      Creates a record to be received from a specified topic and partition
    • Method Summary

      All Methods Instance Methods Concrete Methods Deprecated Methods 
      Modifier and Type Method Description
      long checksum()
      Deprecated.
      As of Kafka 0.11.0.
      Headers headers()
      The headers (never null)
      K key()
      The key (or null if no key is specified)
      java.util.Optional<java.lang.Integer> leaderEpoch()
      Get the leader epoch for the record if available
      long offset()
      The position of this record in the corresponding Kafka partition.
      int partition()
      The partition from which this record is received
      java.lang.String producer()
      The producer for this record.
      int serializedKeySize()
      The size of the serialized, uncompressed key in bytes.
      int serializedValueSize()
      The size of the serialized, uncompressed value in bytes.
      long timestamp()
      The timestamp of this record
      org.apache.kafka.common.record.TimestampType timestampType()
      The timestamp type of this record
      java.lang.String topic()
      The topic this record is received from (never null)
      java.lang.String toString()  
      V value()
      The value
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
    • Constructor Detail

      • ConsumerRecord

        public ConsumerRecord​(java.lang.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).
        Parameters:
        topic - The topic this record is received from
        partition - The partition of the topic this record is received from
        offset - The offset of this record in the corresponding Kafka partition
        key - The key of the record, if one exists (null is allowed)
        value - The record contents
      • ConsumerRecord

        public ConsumerRecord​(java.lang.String topic,
                              int partition,
                              long offset,
                              long timestamp,
                              org.apache.kafka.common.record.TimestampType timestampType,
                              long checksum,
                              int serializedKeySize,
                              int serializedValueSize,
                              K key,
                              V value)
        Creates a record to be received from a specified topic and partition (provided for compatibility with Kafka 0.10 before the message format supported headers).
        Parameters:
        topic - The topic this record is received from
        partition - The partition of the topic this record is received from
        offset - The offset of this record in the corresponding Kafka partition
        timestamp - The timestamp of the record.
        timestampType - The timestamp type
        checksum - The checksum (CRC32) of the full record
        serializedKeySize - The length of the serialized key
        serializedValueSize - The length of the serialized value
        key - The key of the record, if one exists (null is allowed)
        value - The record contents
      • ConsumerRecord

        public ConsumerRecord​(java.lang.String topic,
                              int partition,
                              long offset,
                              long timestamp,
                              org.apache.kafka.common.record.TimestampType timestampType,
                              java.lang.Long checksum,
                              int serializedKeySize,
                              int serializedValueSize,
                              K key,
                              V value,
                              Headers headers)
        Creates a record to be received from a specified topic and partition
        Parameters:
        topic - The topic this record is received from
        partition - The partition of the topic this record is received from
        offset - The offset of this record in the corresponding Kafka partition
        timestamp - The timestamp of the record.
        timestampType - The timestamp type
        checksum - The checksum (CRC32) of the full record
        serializedKeySize - The length of the serialized key
        serializedValueSize - The length of the serialized value
        key - The key of the record, if one exists (null is allowed)
        value - The record contents
        headers - The headers of the record.
      • ConsumerRecord

        public ConsumerRecord​(java.lang.String topic,
                              int partition,
                              long offset,
                              long timestamp,
                              org.apache.kafka.common.record.TimestampType timestampType,
                              java.lang.Long checksum,
                              int serializedKeySize,
                              int serializedValueSize,
                              K key,
                              V value,
                              Headers headers,
                              java.util.Optional<java.lang.Integer> leaderEpoch,
                              java.lang.String producer)
        Creates a record to be received from a specified topic and partition
        Parameters:
        topic - The topic this record is received from
        partition - The partition of the topic this record is received from
        offset - The offset of this record in the corresponding Kafka partition
        timestamp - The timestamp of the record.
        timestampType - The timestamp type
        checksum - The checksum (CRC32) of the full record
        serializedKeySize - The length of the serialized key
        serializedValueSize - The length of the serialized value
        key - The key of the record, if one exists (null is allowed)
        value - The record contents
        headers - The headers of the record.
        producer - The producer for this record
      • ConsumerRecord

        public ConsumerRecord​(java.lang.String topic,
                              int partition,
                              long offset,
                              long timestamp,
                              org.apache.kafka.common.record.TimestampType timestampType,
                              java.lang.Long checksum,
                              int serializedKeySize,
                              int serializedValueSize,
                              K key,
                              V value,
                              Headers headers,
                              java.lang.String producer)
        The constructor is added for backward compatibility
      • ConsumerRecord

        public ConsumerRecord​(java.lang.String topic,
                              int partition,
                              long offset,
                              long timestamp,
                              org.apache.kafka.common.record.TimestampType timestampType,
                              java.lang.Long checksum,
                              int serializedKeySize,
                              int serializedValueSize,
                              K key,
                              V value,
                              Headers headers,
                              java.util.Optional<java.lang.Integer> leaderEpoch)
      • ConsumerRecord

        public ConsumerRecord​(java.lang.String topic,
                              int partition,
                              long offset,
                              K key,
                              V value,
                              long timestamp,
                              java.lang.String producer)
        Creates a record to be received from a specified topic and partition
        Parameters:
        topic - The topic this record is received from
        partition - The partition of the topic this record is received from
        offset - The offset of this record in the corresponding Kafka partition
        key - The key of the record, if one exists (null is allowed)
        value - The record contents
        timestamp - The timestamp at which the record was produced
        producer - The producer for this record
    • Method Detail

      • topic

        public java.lang.String topic()
        The topic this record is received from (never null)
      • partition

        public int partition()
        The partition from which this record is received
      • headers

        public Headers headers()
        The headers (never null)
      • key

        public K key()
        The key (or null if no key is specified)
      • value

        public V value()
        The value
      • offset

        public long offset()
        The position of this record in the corresponding Kafka partition.
      • timestamp

        public long timestamp()
        The timestamp of this record
      • timestampType

        public org.apache.kafka.common.record.TimestampType timestampType()
        The timestamp type of this record
      • checksum

        @Deprecated
        public long checksum()
        Deprecated.
        As of Kafka 0.11.0. Because of the potential for message format conversion on the broker, the checksum returned by the broker may not match what was computed by the producer. It is therefore unsafe to depend on this checksum for end-to-end delivery guarantees. Additionally, message format v2 does not include a record-level checksum (for performance, the record checksum was replaced with a batch checksum). To maintain compatibility, a partial checksum computed from the record timestamp, serialized key size, and serialized value size is returned instead, but this should not be depended on for end-to-end reliability.
        The checksum (CRC32) of the record.
      • serializedKeySize

        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

        public java.util.Optional<java.lang.Integer> leaderEpoch()
        Get the leader epoch for the record if available
        Returns:
        the leader epoch or empty for legacy record formats
      • producer

        public java.lang.String producer()
        The producer for this record.
      • toString

        public java.lang.String toString()
        Overrides:
        toString in class java.lang.Object