Home | About | Sematext search-lucene.com search-hadoop.com
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
 Search Hadoop and all its subprojects:

Switch to Threaded View
Avro >> mail # user >> Serializing / Deserializing Java Objects


Copy link to this message
-
Re: Serializing / Deserializing Java Objects
Hi Bradford,

I believe you use a SpecificDatumReader.  Something like:

  final static SpecicificDatumReader<QueueItem> QUEUE_ITEM_READER = new
SpecificDatumReader<QueueItem>(QueueItem.class);
  QueueItem q = QUEUE_ITEM_READER.read(null, decoder);

There doesn't seem to be a test that exercises this code path in an
explanatory way, but java/src/java/org/apache/avro/ipc/Requestor.java uses
something quite similar.

-- Philip
On Mon, Jun 14, 2010 at 9:20 PM, Bradford Stephens <
[EMAIL PROTECTED]> wrote:

> Greetings,
>
> I've poked around for examples of this, but I can't find any. I
> imagine it's a fairly common use case.
>
> I'm serializing some simple objects into bytes for placement onto
> RabbitMQ. My java class is pretty simple (but it'll grow in complexity
> in time).:
>
> {
> String[] Columns;
> }
>
>
> So, I made a .json schema containing this:
> {
>      "namespace": "com.dts",
>      "name": "QueueItem",
>      "type": "record",
>      "fields": [
>          {"name": "Columns", "type": ["null", {"type": "array",
> "items":"string"}]}
>      ]
> }
>
>
> And generated a java class ...
>
> Now, I'm writing a test to serialize and deserialize some items. I can
> figure out the serialization, but not deserialization back to objects.
>
>        Schema s = Schema.parse(new File("queuetype.json"));
>
>            ByteArrayOutputStream bao = new ByteArrayOutputStream();
>            GenericDatumWriter w = new GenericDatumWriter(s);
>            Encoder e = new BinaryEncoder(bao);
>            e.init (bao);
>
>
>            QueueItem r = new QueueItem();
>            r.put(0, items);
>            w.write(r, e);
>            e.flush();
>
>            ByteArrayInputStream is = new
> ByteArrayInputStream(bao.toByteArray());
>            DecoderFactory df = new DecoderFactory();
>            Decoder d = df.createBinaryDecoder(is, null);
>
>            QueueItem itemout = (QueueItem)  .....
>
>
> And that's what I can't figure out -- how do I use a decoder method to
> create QueueItems?
>
> Cheers,
> B
>
> radford Stephens,
> Founder, Drawn to Scale
> drawntoscalehq.com
> 727.697.7528
>
> http://www.drawntoscalehq.com --  The intuitive, cloud-scale data
> solution. Process, store, query, search, and serve all your data.
>
> http://www.roadtofailure.com -- The Fringes of Scalability, Social
> Media, and Computer Science
>
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