给定arvo模式和Json数组,如何将它们转换为Avro GenericRecord列表?

给定arvo模式和Json数组,如何将它们转换为Avro GenericRecord列表?,json,avro,Json,Avro,鉴于我有一个有效的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_

鉴于我有一个有效的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" },
 ]
[
{
    "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数组,如下所示:

{
 "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" },
 ]
[
{
    "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);

这是迄今为止我能得到的最优化的一个

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);
}

这很好。您必须为数组创建单独的架构