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 json against a schema


Copy link to this message
-
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
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