Kafka Consumer- ClassCastException java

让人想犯罪 __ 提交于 2021-01-29 08:54:44

问题


My Kafka consumer throws an exception when trying to process messages in a batch(i.e process list of messages)

Error Message

is java.lang.ClassCastException: class kafka.psmessage.PMessage cannot be cast to class org.apache.kafka.clients.consumer.ConsumerRecord (kafka.psmessage.pMessage and org.apache.kafka.clients.consumer.ConsumerRecord are in unnamed module of loader 'app'); nested exception is java.lang.ClassCastException: class kafka.psmessage.PMessage cannot be cast to class org.apache.kafka.clients.consumer.ConsumerRecord (kafka.psmessage.PMessage and org.apache.kafka.clients.consumer.ConsumerRecord are in unnamed module of loader 'app')

Code snippet

   public void receive(List<ConsumerRecord<String, PMessage>> records) {
           List<PMessage> msgList = records.stream().map(message -> message.value()).collect(Collectors.toList());

PMessage test = records.get(0).value();
ConsumerRecord<String, PMessage> firstMessage = records.get(0);

All 3 statements above giving ClassCastException

@Override
    protected DefaultKafkaConsumerFactory<String, PMessage> createConsumerFactory(String groupId) {
        Map<String, Object> props = new HashMap<>();
        props.putAll(kafkaProperties.buildConsumerProperties());
        props.put(ConsumerConfig.BOOTSTRAP_SERVERS_CONFIG, serviceInfo.getBrokersAuthUrl());
        props.put(ConsumerConfig.MAX_POLL_RECORDS_CONFIG, 10000);
        props.put(ConsumerConfig.ENABLE_AUTO_COMMIT_CONFIG, false);
        props.put(ConsumerConfig.GROUP_ID_CONFIG, groupId);
        props.putAll(KafkaSaslUtils.getSaslProperties(serviceInfo));

        return new DefaultKafkaConsumerFactory<>(props,
                new StringDeserializer(),
                new MessageDeserializer());
    }

MessageDeserializer:

@Override
    public PMessage deserialize(String topic, byte[] data) {
        if (data == null) {
            throw new SerializationException("Can not deserialize. data is null for topic: '" + topic + "'");
        }

        try {
            SeekableByteArrayInput seekableByteArrayInput = new SeekableByteArrayInput(data);
            GenericDatumReader reader = new GenericDatumReader<GenericRecord>();
            DataFileReader<GenericRecord> dataFileReader = new 
         DataFileReader(seekableByteArrayInput, reader);
            GenericRecord genericRecord = extractGenericRecord(dataFileReader);

Any pointer will be appreciated. Thanks!

来源:https://stackoverflow.com/questions/59869681/kafka-consumer-classcastexception-java

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!