On Tue, Oct 21, 2014 at 10:11 AM, umahida <[email protected]> wrote:
> I have complicated nested POJOs. I am able to generate the schema using
> ReflectData tool.
> I want to convert them to Generic Record preferably or Indexed Records.
To convert from a reflect representation to generic you can serialize
instances with ReflectDatumWriter then read them with
GenericDatumReader. For example, something like:
public Object reflectToGeneric(Object object, Schema s) {
ReflectDatumWriter<Object> writer = new ReflectDatumWriter<Object>(s);
ByteArrayOutputStream out = new ByteArrayOutputStream();
writer.write(object, EncoderFactory.get().directBinaryEncoder(out, null));
GenericDatumReader<Object> reader = new GenericDatumReader<Object>(s);
return reader.read(null,
DecoderFactory.get().binaryDecoder(out.toByteArray(), null));
}
If you're converting multiple objects it would be faster to reuse the
writer, reader, encoder, decoder, etc.
Doug