내 JSON 문자열을 바이너리 인코딩하려고합니다. 아래는 JSON 문자열이며 변환을 수행하는 간단한 방법을 만들었지 만 내가하고있는 방식이 올바른지 아닌지 확실하지 않습니다. 아 브로 진 내 JSON 문자열 인코딩 변환합니다 방법 아래Avro를 사용하는 방법 Apache Avro를 사용하여 JSON 문자열을 바이너리 인코딩 하시겠습니까?
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
}
-
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();
}
사람이 살펴보고 제가 브로 진에 노력하고 길을 내 JSON 문자열이 정확한지 여부를 알려하거나 아니?
의 [아파치 아 브로 사양 ] (http://avro.apache.org/docs/1.7.6/spec.html). –
Avro 표기법이 JSON 문자열의 형식에 대한 제약 조건의 특정 사양이기 때문에 "Avro"에 대한 JSON 변환이 무엇을 의미하는지 분명하지 않습니다. –
어쨌든, 아파치는 [유틸리티 세트] (http : //avro.apache.org/docs/current/api/java/index.html), 왜 자신 만의 글을 써야하는지 명확하지 않다. –