Convert Json to avro

I turn Json into avro. I have json data in a JSONArray . Therefore, converting it to an array of bytes, I ran into a problem.

below is my code:

 static byte [] fromJsonToAvro(JSONArray json, String schemastr) throws Exception { ExcelToJson ejj = new ExcelToJson(); List<String> list = new ArrayList<String>(); if (json != null) { int len = json.length(); for (int i=0;i<len;i++){ list.add(json.get(i).toString()); } } InputStream input = new ByteArrayInputStream(list.getBytes()); //json.toString().getBytes() DataInputStream din = new DataInputStream(input); . . .//rest of the logic 

So how can I do this? How to convert a JsonArray object to bytes (i.e. How to use the getBytes () method for JsonArray objects). The above code gives an error in list.getBytes() and says that getBytes () is not available for the list.

+6
source share
3 answers

For json to avro online converter, check the following URL

http://avro4s-ui.landoop.com

The avro4s library is used , which offers many conversions, including json => avro

+5
source

Avro operates at the record level associated with the circuit. I don’t think there is such a concept as "convert this JSON fragment to bytes for the Avro field regardless of any schema or record."

Assuming the array is part of a larger JSON record, if you start with a write string, you can do

 public static byte[] jsonToAvro(String json, String schemaStr) throws IOException { InputStream input = null; DataFileWriter<GenericRecord> writer = null; Encoder encoder = null; ByteArrayOutputStream output = null; try { Schema schema = new Schema.Parser().parse(schemaStr); DatumReader<GenericRecord> reader = new GenericDatumReader<GenericRecord>(schema); input = new ByteArrayInputStream(json.getBytes()); output = new ByteArrayOutputStream(); DataInputStream din = new DataInputStream(input); writer = new DataFileWriter<GenericRecord>(new GenericDatumWriter<GenericRecord>()); writer.create(schema, output); Decoder decoder = DecoderFactory.get().jsonDecoder(schema, din); GenericRecord datum; while (true) { try { datum = reader.read(null, decoder); } catch (EOFException eofe) { break; } writer.append(datum); } writer.flush(); return output.toByteArray(); } finally { try { input.close(); } catch (Exception e) { } } } 
+2
source

This discussion is probably useful:

http://mail-archives.apache.org/mod_mbox/avro-user/201209.mbox/% 3CCALEq1Z8s1sfaAVB7YE2rpZ=v3q1V_h7Vm39h0HsOzxJ+qfQRSg@mail.gmail .com% 3E

The bottom line is that there is a special Json scheme, and you can use JsonReader / Writer to get to and from it. Here you can use the Json schema:

https://github.com/apache/avro/blob/trunk/share/schemas/org/apache/avro/data/Json.avsc

+2
source

Source: https://habr.com/ru/post/985322/


All Articles