春季靴子Kafka Camel Avro消费者

时间:2016-08-03 17:32:29

标签: spring-boot apache-camel apache-kafka avro

我一直试图找到Spring Boot Kafka Camel Avro消费者的示例代码而没有运气。我在以下网址找到了Spring Camel Kafka消费者和制作人样本:

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

但缺少的是Avro部分。我在这里查看avro的Camel文档:

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

我的具体问题是,一旦我的bean是从Avro Schema创建的,我有POJO类,我如何告诉camel上面的Spring示例用户Avro序列化?具体来说,我指的是这行代码:     从( “卡夫卡:本地主机:9092主题=试验&安培; zookeeperHost =本地主机&安培; zookeeperPort = 2181&安培;的groupId =组1&安培; serializerClass = kafka.serializer.StringEncoder?”)豆(kafkaOutputBean.class);

序列化程序是StringEncoder。我如何告诉Camel使用Avro序列化?

1 个答案:

答案 0 :(得分:0)

我找到了自己的答案。因此,我想与您分享。它实际上是serializerClass=org.springframework.integration.kafka.serializer.avro.AvroSerializer。 代码很简单,你几乎可以编写自己的代码。

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