Home | About | Sematext search-lucene.com search-hadoop.com
 Search Hadoop and all its subprojects:

Switch to Plain View
Avro, mail # user - Direct conversion from Generic Record to Specific Record


+
Roger Hoover 2014-02-05, 18:24
+
Christophe Taton 2014-02-05, 18:59
+
Roger Hoover 2014-02-05, 23:00
Copy link to this message
-
Re: Direct conversion from Generic Record to Specific Record
Mika Ristimaki 2014-02-06, 14:10
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:

> Hi Christophe,
>
> Thanks for your reply.  When you say that we could write a generic conversion function, do you mean we can write one that works for all schemas?  That would be great!
>
> I'd like an API something like this:
>
> MyCustomRecord record = MyCustomRecord.newBuilder(GenericRecord record).build()
>
> Thanks,
>
> Roger
>
>
> On Wed, Feb 5, 2014 at 10:59 AM, Christophe Taton <[EMAIL PROTECTED]> wrote:
> Hi Roger,
> With the change proposed in https://issues.apache.org/jira/browse/AVRO-1443, you would be able to create a specific record using a generic builder API. That means we could write a generic conversion function that creates specific records.
> Would that work for you?
> C.
>
>
> On Wed, Feb 5, 2014 at 10:23 AM, Roger Hoover <[EMAIL PROTECTED]> wrote:
> Hi,
>
> I'm working with an existing API that hands me a GenericRecord.  Is there an easy way I can covert it into a SpecificRecord.  Really I want to get it into the code-generated object.
>
> Thanks,
>
> Roger
>
>
>
>
 
+
Roger Hoover 2014-02-06, 17:07
+
Christophe Taton 2014-02-06, 16:12
+
Roger Hoover 2014-02-06, 17:13
+
Doug Cutting 2014-02-06, 18:35