You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@avro.apache.org by Pratyush Chandra <ch...@gmail.com> on 2013/01/07 12:46:07 UTC

Serializing json against a schema

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

Re: Serializing json against a schema

Posted by Scott Carey <sc...@apache.org>.
You could use the ReflectDatumWriter to write a simple java data class to
Avro, and you can  create instances of such classes from JSON using a
library like Jackson.   There is a JSON encoding for Avro, if your data
conformed to that format (which would be more verbose than what you have
below) you could use that to decode it, then re-encode it to binary.
Lastly you can use the SpecificDatum API, generate Java classes from your
schema, then set the data from the json with its type-safe builder pattern
APIs instead of the loose Generic API.


On 1/7/13 3:46 AM, "Pratyush Chandra" <ch...@gmail.com> wrote:


>
>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