Spring cloud stream kafka avro deserialization

时间:2018-09-18 20:34:45

标签: avro spring-cloud-stream spring-kafka

I'm writing a spring cloud stream sink application which consumes Avro messages. I'm trying to get the application consume the messages however I'm getting the following error.

org.springframework.messaging.converter.MessageConversionException: Cannot convert from [[B] to [org.apache.avro.generic.GenericRecord] for GenericMessage [payload=byte[304], headers={kafka_offset=42898134, kafka_consumer=org.apache.kafka.clients.consumer.KafkaConsumer@74f810ba, deliveryAttempt=3, kafka_timestampType=NO_TIMESTAMP_TYPE, kafka_receivedMessageKey=null, kafka_receivedPartitionId=0, kafka_receivedTopic=bam_hrapi, kafka_receivedTimestamp=-1, contentType=avro/bytes}], failedMessage=GenericMessage [payload=byte[304], headers={kafka_offset=42898134, kafka_consumer=org.apache.kafka.clients.consumer.KafkaConsumer@74f810ba, deliveryAttempt=3, kafka_timestampType=NO_TIMESTAMP_TYPE, kafka_receivedMessageKey=null, kafka_receivedPartitionId=0, kafka_receivedTopic=*****, kafka_receivedTimestamp=-1, contentType=avro/bytes}]

I tried reading the spring reference documentation and configured the application accordingly. I tried various configurations however I still get the error.

Below is my application config

@SpringBootApplication
@EnableBinding(Processor.class)
public class UserApp {

    public static void main(String[] args) {
        SpringApplication.run(UserApp.class, args);
    }

    @Bean
    public MessageConverter userMessageConverter() {
        AvroSchemaMessageConverter converter = new AvroSchemaMessageConverter(MimeType.valueOf("avro/bytes"));
        converter.setSchemaLocation(new ClassPathResource("schemas/MapsEvent.avsc"));
        return converter;
    }
}

and my stream listener as below

@Component
@Slf4j
public class UserAdditionEventListener {
    @StreamListener(Processor.INPUT)
    @SendTo(Processor.OUTPUT)
    public String handleEvents(GenericRecord input) {
        System.out.println(input);
        return "hello";
    }
}

Below is my application.properties file

spring.cloud.stream.bindings.input.destination=user_topic
spring.cloud.stream.bindings.output.destination=user_output_topic
spring.cloud.stream.kafka.binder.brokers=dd-kafa-100
spring.cloud.stream.default.consumer.headerMode=raw
spring.cloud.stream.bindings.input.content-type=avro/bytes