Skip to content

Instantly share code, notes, and snippets.

@vepo
Last active December 10, 2021 12:56
Show Gist options
  • Save vepo/b1b84d1f3723ab1b867ebc02043c4912 to your computer and use it in GitHub Desktop.
Save vepo/b1b84d1f3723ab1b867ebc02043c4912 to your computer and use it in GitHub Desktop.
Convert POJO/Map to AVRO GenericRecord
import org.apache.avro.generic.GenericData;
import org.apache.avro.Schema;
import org.apache.avro.io.DecoderFactory;
import org.apache.avro.io.Decoder;
import org.apache.avro.io.DatumReader;
import org.apache.avro.generic.GenericDatumReader;
public class AvroConverter {
public static GenericData.Record parseJson(String data, Schema schema) throws IOException {
Decoder decoder = DecoderFactory.get().jsonDecoder(schema, data);
DatumReader<GenericData.Record> reader = new GenericDatumReader<>(schema);
return reader.read(null, decoder);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment