Last active
May 26, 2020 19:38
-
-
Save talfco/59c5b6374c82d67b316dd5bf9c0e54e6 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
private String convertAvroBinaryToJSON(byte[] msg) throws IOException { | |
long msgFingerprint = getAvroFingerprint(msg); | |
Schema mgsSchema = registry.getSchema(msgFingerprint); | |
byte[] payload = extractPayload(msg); | |
DatumReader<Object> reader = null; | |
if (msgFingerprint == this.backendComponentSchemaFingerprint) | |
reader = new GenericDatumReader<>(mgsSchema); | |
else | |
reader = new GenericDatumReader<>(mgsSchema,this.backendComponentSchema); | |
DatumWriter<Object> writer = new GenericDatumWriter<>(mgsSchema); | |
BinaryDecoder binaryDecoder = DecoderFactory.get().binaryDecoder(new ByteArrayInputStream(payload), null); | |
ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); | |
JsonEncoder jsonEncoder = EncoderFactory.get().jsonEncoder(mgsSchema, outputStream, true); | |
Object datum = null; | |
while (!binaryDecoder.isEnd()) { | |
datum = reader.read(datum, binaryDecoder); | |
writer.write(datum, jsonEncoder); | |
jsonEncoder.flush(); | |
} | |
outputStream.flush(); | |
return outputStream.toString(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment