Nate
Nate

Reputation: 67

Given arvo schema and a Json array, how to convert them to a list of Avro GenericRecord?

Given that I have a valid avro schema as below:

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

}

and a Json array as below:

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

]

What is the best effieient way to convert the json array to a list of Avro GenericRecord?

I have got the following codes, that converts one json object to one 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);

Upvotes: 0

Views: 1612

Answers (1)

Nate
Nate

Reputation: 67

Here is the most optimized one I could get so far

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

Upvotes: 1

Related Questions