Учитывая схему arvo и массив Json, как преобразовать их в список Avro GenericRecord? - PullRequest
0 голосов
/ 22 января 2020

Учитывая, что у меня есть действующая схема avro, как показано ниже:

{
 "type": "record",
 "namespace": "com.example",
 "name": "Employee",
 "doc": "Avro Schema for our Employee",     
 "fields": [
   { "name": "first_name", "type": "string", "doc": "First Name of Customer" },
   { "name": "last_name", "type": "string", "doc": "Last Name of Customer" },
   { "name": "age", "type": "int", "doc": "Age at the time of registration" },
 ]

}

и массив Json, как показано ниже:

[
{
    "first_name": "Alex",
    "last_name": "Dan",
    "age": 35
},
{
    "first_name": "Bill",
    "last_name": "Lee",
    "age": 36
},
{
    "first_name": "Charan",
    "last_name": "Vaski",
    "age": 37
}

]

Каков наилучший эффективный способ преобразования массива json в список Avro GenericRecord?

У меня есть следующие коды, которые преобразуют один объект json в один GenericRecord

Schema schema = parser.parse(schemaString);
GenericDatumReader<GenericRecord> reader = new GenericDatumReader<>(schema);
JsonDecoder jsonDecoder = DecoderFactory.get().jsonDecoder(schema, jsonString);
GenericRecord record = reader.read(null, jsonDecoder);
System.out.println(record);

1 Ответ

1 голос
/ 23 января 2020

Вот самый оптимизированный, который я мог получить до сих пор

ObjectMapper objectMapper = new ObjectMapper();
JsonNode jsonNode = objectMapper.readTree(jsonArray);

Schema schema = parser.parse(schemaString);
GenericDatumReader<GenericRecord> reader = new GenericDatumReader<>(schema);
JsonDecoder jsonDecoder = DecoderFactory.get().jsonDecoder(schema, "");

for (int i = 0; i < jsonNode.size(); i++) {
  jsonDecoder.configure(jsonNode.get(i).toString());
  GenericRecord record = reader.read(null, jsonDecoder);
  System.out.println(record);
}
...