2
votes

I have been trying to find sample code for Spring Boot Kafka Camel Avro consumer with no luck. I have found Spring Camel Kafka Consumer and producer sample at the following url:

https://thysmichels.com/2015/09/04/apache-camel-kafka-spring-integration/

But what is missing is the Avro part. I am looking at Camel documentation for avro here:

http://camel.apache.org/avro.html

My specific question is once my bean is created from the Avro Schema and I have the POJO classes, how do I tell camel for spring example above to user Avro serialization? Specifically I am referring this line of code: from("kafka:localhost:9092?topic=test&zookeeperHost=localhost&zookeeperPort=2181&groupId=group1&serializerClass=kafka.serializer.StringEncoder").bean(kafkaOutputBean.class);

Where the serializer is StringEncoder. How do I tell Camel to use Avro serialization?

1
Would not serializerClass=... be where you put it? e.g. &serializerClass=avro.serializer.StringEncoderjny

1 Answers

0
votes

I found my own answer. Thus I'd like to share it with you. It is actually serializerClass=org.springframework.integration.kafka.serializer.avro.AvroSerializer. The code is very simple and you can pretty much write your own.

    public class AvroSerializer<T> {

        public T deserialize(final byte[] bytes, final DatumReader<T> reader) throws IOException {
            final Decoder decoder = DecoderFactory.get().binaryDecoder(bytes, null);
            return reader.read(null, decoder);
        }

        public byte[] serialize(final T input, final DatumWriter<T> writer) throws IOException {
            final ByteArrayOutputStream stream = new ByteArrayOutputStream();

            final Encoder encoder = EncoderFactory.get().binaryEncoder(stream, null);
            writer.write(input, encoder);
            encoder.flush();

            return stream.toByteArray();
        }
    }