avro-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Gaurav <gaurav...@gmail.com>
Subject Re: Records inside records
Date Mon, 05 Dec 2011 15:27:27 GMT
I figured it out.

	private static ByteArrayOutputStream EncodeData() throws IOException {
		// TODO Auto-generated method stub
		Schema schema = createMetaData();
		
		GenericDatumWriter<GenericData.Record> datum = new
GenericDatumWriter<GenericData.Record>(schema);
		
		GenericData.Record inner_record = new
GenericData.Record(schema.getField("trade").schema());
		inner_record.put("inner_spn", new Long(23490843));
		
		GenericData.Record record = new GenericData.Record(schema);
		record.put("xyz", 1050324);		
		record.put("trade", inner_record);
		
		ByteArrayOutputStream out = new ByteArrayOutputStream();
		BinaryEncoder encoder = ENCODER_FACTORY.binaryEncoder(out, null);
		
		datum.write(record, encoder);
		
		encoder.flush();
		out.close();

		return out;
	}

	private static Schema createMetaData() {
		// TODO Auto-generated method stub
		String jsonSchema = "{ \"type\" : \"record\"," +
					"\"name\" : \"update\", "+
					"\"fields\" : [ "+
					            "{\"name\": \"xyz\", \"type\" : \"int\"}," +	
					            "{\"name\": \"trade\", \"type\" : {\"type\" : \"record\",
\"name\" : \"subfield\"," +
					            "							       \"fields\" : [{\"name\" : \"inner_spn\",
\"type\" : \"long\"}]}}" +
					"]}";
		Schema schema = new Schema.Parser().parse(jsonSchema);
		return schema;
	}

--
View this message in context: http://apache-avro.679487.n3.nabble.com/Records-inside-records-tp3561179p3561705.html
Sent from the Avro - Users mailing list archive at Nabble.com.

Mime
View raw message