avro-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Pratyush Chandra <chandra.praty...@gmail.com>
Subject Serializing json against a schema
Date Mon, 07 Jan 2013 11:46:07 GMT
Hi,

I am new to Avro. I was going through examples and figured out that
GenericRecord can be appended to DataFileWriter and then serialized.

Example:
record.avsc is
{
    "namespace": "example.proto",
    "name": "Message", "type": "record",
    "fields": [
          {"name": "to",   "type": ["string","null"]}
    ]
}

and my code snippet is :
        DatumWriter<GenericRecord> datumWriter = new
GenericDatumWriter<GenericRecord>(schema);
        DataFileWriter<GenericRecord> dataFileWriter = new
DataFileWriter<GenericRecord>(datumWriter);
        dataFileWriter.create(schema, file);
        GenericRecord message1= new GenericData.Record(schema);
        message1.put("to", "Alyssa");
        dataFileWriter.append(message1);
        dataFileWriter.close();

My question is : Suppose I am receiving a json from server, and based on
schema I would like to serialize it directly, without parsing it.
For example :
Input received is *{"to": "Alyssa"}*
Is there a way, I can serialize above json with record.avsc schema instead
of appending GenericRecord ?

-- 
Pratyush Chandra

Mime
View raw message