尝试使用apache avro反序列化json
时出现以下异常:
Exception in thread "main" org.apache.avro.AvroTypeException: Unknown union branch sampleArrayRecordString
at org.apache.avro.io.JsonDecoder.readIndex(JsonDecoder.java:445)
以下是代码:
final Schema schema = Schema.parse(new File(
"<schema_file_path>"));
byte[] payload = Files.readAllBytes(Paths.get(
"<json_file_path>"));
GenericRecord record = new GenericDatumReader<GenericRecord>(schema).read(null,
DecoderFactory.get().jsonDecoder(schema, new String(payload)));
System.out.println(record);
以下是架构:
{
"type":"record",
"name":"SampleEvent",
"namespace":"a.b",
"doc":"Avro definition for sample data.",
"fields":[
{
"name":"eventBody",
"type":[
{
"type":"record",
"name":"SampleBody",
"doc":"Avro definition",
"fields":[
{
"name":"sampleArray",
"type":{
"type":"array",
"items":[
{
"name":"sampleArrayRecord",
"type":"record",
"fields":[
{
"name":"sampleArrayRecordString",
"type":"string",
"doc":"Sample array record string",
"omdFieldSecurity":"internal",
"omdLogicalDataType":"string"
}
],
"doc":"Sample array record",
"omdFileSecurity":"internal",
"omdRepositoryTag":"1.4.0"
}
]
},
"doc":"Sample array",
"omdFieldSecurity":"internal",
"omdLogicalDataType":"record"
}
],
"omdSecurityClassification":"Internal",
"omdRepositoryTag":"1.0.0"
}
]
}
]
}
最后,下面是json:
{
"eventBody": {
"a.b.SampleBody": {
"sampleArray": [{
"sampleArrayRecordString": "KLMNO"
}]
}
}
}
我是否需要指定任何元数据才能使其生效?