You can take a look at the org.apache.avro.tool.DataFileWriteTool class in the avro tools source. It looks similar to what you have, but I only glanced at it. -a
On Sun, Feb 23, 2014 at 6:08 PM, Check Peck <[email protected]> wrote: > I am trying to avro binary encode my JSON String. Below is my JSON String > and I have created a simple method which will do the conversion but I am > not sure whether the way I am doing is correct or not? > > public static void main(String args[]) throws Exception{ > try{ > Schema schema = new > Parser().parse((TestExample.class.getResourceAsStream("/3233.avsc"))); > String json="{"+ > " \"location\" : {"+ > " \"devices\":["+ > " {"+ > " \"did\":\"9abd09-439bcd-629a8f\","+ > " \"dt\":\"browser\","+ > " \"usl\":{"+ > " \"pos\":{"+ > " \"source\":\"GPS\","+ > " \"lat\":90.0,"+ > " \"long\":101.0,"+ > " \"acc\":100"+ > " },"+ > " \"addSource\":\"LL\","+ > " \"add\":["+ > " {"+ > " \"val\":\"2123\","+ > " \"type\" : \"NUM\""+ > " },"+ > " {"+ > " \"val\":\"Harris ST\","+ > " \"type\" : \"ST\""+ > " }"+ > " ],"+ > " \"ei\":{"+ > " \"ibm\":true,"+ > " \"sr\":10,"+ > " \"ienz\":true,"+ > " \"enz\":100,"+ > " \"enr\":10"+ > " },"+ > " \"lm\":1390598086120"+ > " }"+ > " }"+ > " ],"+ > " \"ver\" : \"1.0\""+ > " }"+ > "}"; > > byte[] avroByteArray = fromJsonToAvro(json,schema); > > } catch (Exception ex) { > // log an exception > } > > > Below method will convert my JSON String to Avro Binary encoded - > > private static byte[] fromJsonToAvro(String json, Schema schema) > throws Exception { > > InputStream input = new ByteArrayInputStream(json.getBytes()); > DataInputStream din = new DataInputStream(input); > > Decoder decoder = DecoderFactory.get().jsonDecoder(schema, din); > > DatumReader<Object> reader = new > GenericDatumReader<Object>(schema); > Object datum = reader.read(null, decoder); > > > GenericDatumWriter<Object> w = new > GenericDatumWriter<Object>(schema); > ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); > > Encoder e = EncoderFactory.get().binaryEncoder(outputStream, null); > > w.write(datum, e); > e.flush(); > > return outputStream.toByteArray(); > } > > > Can anyone take a look and let me know whether the way I am trying to avro > binary my JSON String is correct or not? >
