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);
}
}
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));
}
Aggregations