Search in sources :

Example 1 with Record

use of org.apache.kafka.common.record.Record in project kafka by apache.

the class Fetcher method parseRecord.

/**
     * Parse the record entry, deserializing the key / value fields if necessary
     */
private ConsumerRecord<K, V> parseRecord(TopicPartition partition, LogEntry logEntry) {
    Record record = logEntry.record();
    if (this.checkCrcs) {
        try {
            record.ensureValid();
        } catch (InvalidRecordException e) {
            throw new KafkaException("Record for partition " + partition + " at offset " + logEntry.offset() + " is invalid, cause: " + e.getMessage());
        }
    }
    try {
        long offset = logEntry.offset();
        long timestamp = record.timestamp();
        TimestampType timestampType = record.timestampType();
        ByteBuffer keyBytes = record.key();
        byte[] keyByteArray = keyBytes == null ? null : Utils.toArray(keyBytes);
        K key = keyBytes == null ? null : this.keyDeserializer.deserialize(partition.topic(), keyByteArray);
        ByteBuffer valueBytes = record.value();
        byte[] valueByteArray = valueBytes == null ? null : Utils.toArray(valueBytes);
        V value = valueBytes == null ? null : this.valueDeserializer.deserialize(partition.topic(), valueByteArray);
        return new ConsumerRecord<>(partition.topic(), partition.partition(), offset, timestamp, timestampType, record.checksum(), keyByteArray == null ? ConsumerRecord.NULL_SIZE : keyByteArray.length, valueByteArray == null ? ConsumerRecord.NULL_SIZE : valueByteArray.length, key, value);
    } catch (RuntimeException e) {
        throw new SerializationException("Error deserializing key/value for partition " + partition + " at offset " + logEntry.offset(), e);
    }
}
Also used : SerializationException(org.apache.kafka.common.errors.SerializationException) TimestampType(org.apache.kafka.common.record.TimestampType) Record(org.apache.kafka.common.record.Record) ConsumerRecord(org.apache.kafka.clients.consumer.ConsumerRecord) KafkaException(org.apache.kafka.common.KafkaException) InvalidRecordException(org.apache.kafka.common.record.InvalidRecordException) ByteBuffer(java.nio.ByteBuffer) ConsumerRecord(org.apache.kafka.clients.consumer.ConsumerRecord)

Aggregations

ByteBuffer (java.nio.ByteBuffer)1 ConsumerRecord (org.apache.kafka.clients.consumer.ConsumerRecord)1 KafkaException (org.apache.kafka.common.KafkaException)1 SerializationException (org.apache.kafka.common.errors.SerializationException)1 InvalidRecordException (org.apache.kafka.common.record.InvalidRecordException)1 Record (org.apache.kafka.common.record.Record)1 TimestampType (org.apache.kafka.common.record.TimestampType)1