读取 Avro 文件会出现 AvroTypeException: missing required field 错误(即使新字段在模式中声明为 null)

Reading Avro file gives AvroTypeException: missing required field error (even though the new field is declared null in schema)

我正在尝试 deserialize/read Avro 文件,avro 数据文件没有新字段。即使新字段在模式中声明为 null,它也应该是可选的。但它仍然给我错误作为强制性的。

Exception in thread "main" org.apache.avro.AvroTypeException: Found com.kiran.avro.User, expecting com.kiran.avro.User, missing required field loc

AVRO 架构声明:

{"name": "loc", "type": ["string", "null"]}

正在使用代码读取文件:

DatumReader<User> userDatumReader = new SpecificDatumReader<User>(User.class);
DataFileReader<User> dataFileReader = new DataFileReader<User>(file, userDatumReader);

是否有其他方式声明可选字段?

感谢hints/suggestions !!

"file"的内容是什么?

我可能是错的,但是如果你在模式中定义一个字段为{"name": "loc", "type": ["string", "null"]},你仍然需要定义一个loc字段,即使是null。它应该类似于文件中的 "loc": null

尝试将 "default" 添加到此字段声明中:

{"name" : "loc",
"type" :  ["null","string"] ,
"default" : null}

那么应该可以在文件中省略这个字段。

您还可以查看此问题 以获取更多信息和示例。