Search in sources :

Example 1 with RecordDeserializationException

use of org.apache.kafka.common.errors.RecordDeserializationException 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, RecordBatch batch, Record record) {
    try {
        long offset = record.offset();
        long timestamp = record.timestamp();
        Optional<Integer> leaderEpoch = maybeLeaderEpoch(batch.partitionLeaderEpoch());
        TimestampType timestampType = batch.timestampType();
        Headers headers = new RecordHeaders(record.headers());
        ByteBuffer keyBytes = record.key();
        byte[] keyByteArray = keyBytes == null ? null : Utils.toArray(keyBytes);
        K key = keyBytes == null ? null : this.keyDeserializer.deserialize(partition.topic(), headers, keyByteArray);
        ByteBuffer valueBytes = record.value();
        byte[] valueByteArray = valueBytes == null ? null : Utils.toArray(valueBytes);
        V value = valueBytes == null ? null : this.valueDeserializer.deserialize(partition.topic(), headers, valueByteArray);
        return new ConsumerRecord<>(partition.topic(), partition.partition(), offset, timestamp, timestampType, keyByteArray == null ? ConsumerRecord.NULL_SIZE : keyByteArray.length, valueByteArray == null ? ConsumerRecord.NULL_SIZE : valueByteArray.length, key, value, headers, leaderEpoch);
    } catch (RuntimeException e) {
        throw new RecordDeserializationException(partition, record.offset(), "Error deserializing key/value for partition " + partition + " at offset " + record.offset() + ". If needed, please seek past the record to continue consumption.", e);
    }
}
Also used : Headers(org.apache.kafka.common.header.Headers) RecordHeaders(org.apache.kafka.common.header.internals.RecordHeaders) ByteBuffer(java.nio.ByteBuffer) ConsumerRecord(org.apache.kafka.clients.consumer.ConsumerRecord) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) RecordHeaders(org.apache.kafka.common.header.internals.RecordHeaders) RecordDeserializationException(org.apache.kafka.common.errors.RecordDeserializationException) TimestampType(org.apache.kafka.common.record.TimestampType)

Example 2 with RecordDeserializationException

use of org.apache.kafka.common.errors.RecordDeserializationException in project kafka by apache.

the class KafkaConsumerTest method testSecondPollWithDeserializationErrorThrowsRecordDeserializationException.

@Test
public void testSecondPollWithDeserializationErrorThrowsRecordDeserializationException() {
    int invalidRecordNumber = 4;
    int invalidRecordOffset = 3;
    StringDeserializer deserializer = mockErrorDeserializer(invalidRecordNumber);
    KafkaConsumer<String, String> consumer = setUpConsumerWithRecordsToPoll(tp0, 5, deserializer);
    ConsumerRecords<String, String> records = consumer.poll(Duration.ZERO);
    assertEquals(invalidRecordNumber - 1, records.count());
    assertEquals(Collections.singleton(tp0), records.partitions());
    assertEquals(invalidRecordNumber - 1, records.records(tp0).size());
    long lastOffset = records.records(tp0).get(records.records(tp0).size() - 1).offset();
    assertEquals(invalidRecordNumber - 2, lastOffset);
    RecordDeserializationException rde = assertThrows(RecordDeserializationException.class, () -> consumer.poll(Duration.ZERO));
    assertEquals(invalidRecordOffset, rde.offset());
    assertEquals(tp0, rde.topicPartition());
    assertEquals(rde.offset(), consumer.position(tp0));
    consumer.close(Duration.ofMillis(0));
}
Also used : RecordDeserializationException(org.apache.kafka.common.errors.RecordDeserializationException) StringDeserializer(org.apache.kafka.common.serialization.StringDeserializer) Test(org.junit.jupiter.api.Test)

Aggregations

RecordDeserializationException (org.apache.kafka.common.errors.RecordDeserializationException)2 ByteBuffer (java.nio.ByteBuffer)1 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1 ConsumerRecord (org.apache.kafka.clients.consumer.ConsumerRecord)1 Headers (org.apache.kafka.common.header.Headers)1 RecordHeaders (org.apache.kafka.common.header.internals.RecordHeaders)1 TimestampType (org.apache.kafka.common.record.TimestampType)1 StringDeserializer (org.apache.kafka.common.serialization.StringDeserializer)1 Test (org.junit.jupiter.api.Test)1