简体   繁体   中英

Avro to json converter in java

I am reading Avro from kafka as a string and I am trying to convert the String avro to Json using java code.

    @KafkaListener(topics = "#{'${kafka.consumer.topics}'.split(',')}", containerFactory = "kafkaListenerContainerFactory")
    void listener(String message, Acknowledgment acknowledgment, @Header(KafkaHeaders.RECEIVED_TOPIC) String topic) throws CacheServiceException, JsonProcessingException {

//some code here
byte[] data = message.getBytes(); //This seems to be the issue

avroToJson(schema,data)


}

//Code to convert avro to json
public String avroToJson(Schema schema, byte[] avroBinary) throws IOException {
        DatumReader<Object> datumReader = new GenericDatumReader<>(schema);
        Decoder decoder = DecoderFactory.get().binaryDecoder(avroBinary, null);
        Object avroDatum = datumReader.read(null, decoder);
        System.out.println("Initiating loop");
        try (ByteArrayOutputStream baos = new ByteArrayOutputStream()) {
            DatumWriter<Object> writer = new GenericDatumWriter<>(schema);
            JsonEncoder encoder = EncoderFactory.get().jsonEncoder(schema, baos, false);
            writer.write(avroDatum, encoder);
            encoder.flush();
            baos.flush();
            return new String(baos.toByteArray(), StandardCharsets.UTF_8);
        }
    }

I want to avoid reading data as AVRO from kafka as I am reading data from different topics with different Schemas in same kafka.

Avro is used to validate your data against the schema. if you don't want this, simply remove Avro and use only Kafka. From the producer, side send json data instead of avro serialized data.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM