Avro, mail # user - Re: Direct conversion from Generic Record to Specific Record - 2014-02-06, 14:10
 Search Hadoop and all its subprojects:

Switch to Threaded View
Copy link to this message
-
Re: Direct conversion from Generic Record to Specific Record
Hi,

If you don't mind the overhead you can make the conversion through the avro binary data. So something like this should work (assuming both records are using the same schema)

GenericRecord myRecord = ...
GenericDatumWriter<GenericRecord> writer = new GenericDatumWriter<GenericRecord>(MyCustomRecord.getClassSchema());
ByteArrayOutputStream out = new ByteArrayOutputStream();
Encoder encoder = EncoderFactory.get().binaryEncoder(out, null);
writer.write(myRecord, encoder);
encoder.flush();

byte[] avroData = out.toByteArray();
out.close();

SpecificDatumReader<MyCustomRecord> reader = new SpecificDatumReader<MyCustomRecord>(MyCustomRecord.class);
Decoder decoder = DecoderFactory.get().binaryDecoder(avroData, null);
MyCustomRecord myCustomRecord = reader.read(null, decoder);

-Mika

On Feb 6, 2014, at 12:59 AM, Roger Hoover <[EMAIL PROTECTED]> wrote:

 
NEW: Monitor These Apps!
elasticsearch, apache solr, apache hbase, hadoop, redis, casssandra, amazon cloudwatch, mysql, memcached, apache kafka, apache zookeeper, apache storm, ubuntu, centOS, red hat, debian, puppet labs, java, senseiDB