我正在尝试使用Apache Avro将从Elastic Search导出的数据的模式强制执行到HDFS中的许多Avro文档中(使用Drill进行查询)。我在使用Avro默认设置时遇到了一些问题
鉴于此架构:
{
"namespace" : "avrotest",
"type" : "record",
"name" : "people",
"fields" : [
{"name" : "firstname", "type" : "string"},
{"name" : "age", "type" :"int", "default": -1}
]
}
我希望{"firstname" : "Jane"}
这样的json文档会使用默认值-1
为年龄字段序列化。
默认值:此字段的默认值,用于读取实例 缺少此字段(可选)。
然而,这似乎不会发生
java -jar avro-tools-1.8.0.jar fromjson --schema-file p2.avsc jane.json > jane.avro
Exception in thread "main" org.apache.avro.AvroTypeException: Expected int. Got END_OBJECT
at org.apache.avro.io.JsonDecoder.error(JsonDecoder.java:697)
at org.apache.avro.io.JsonDecoder.readInt(JsonDecoder.java:172)
at org.apache.avro.io.ValidatingDecoder.readInt(ValidatingDecoder.java:83)
at org.apache.avro.generic.GenericDatumReader.readInt(GenericDatumReader.java:511)
at org.apache.avro.generic.GenericDatumReader.readWithoutConversion(GenericDatumReader.java:182)
at org.apache.avro.generic.GenericDatumReader.read(GenericDatumReader.java:152)
at org.apache.avro.generic.GenericDatumReader.readField(GenericDatumReader.java:240)
at org.apache.avro.generic.GenericDatumReader.readRecord(GenericDatumReader.java:230)
at org.apache.avro.generic.GenericDatumReader.readWithoutConversion(GenericDatumReader.java:174)
at org.apache.avro.generic.GenericDatumReader.read(GenericDatumReader.java:152)
at org.apache.avro.generic.GenericDatumReader.read(GenericDatumReader.java:144)
at org.apache.avro.tool.DataFileWriteTool.run(DataFileWriteTool.java:99)
at org.apache.avro.tool.Main.run(Main.java:87)
at org.apache.avro.tool.Main.main(Main.java:76)
这可能,或者我错过了什么?
答案 0 :(得分:0)
关键是,如果您在模式中声明您的字段,请执行以下操作:
{"name": "fieldName", "type": ["int", "null"], default: null }
使用像optional这样的字段是不够的,尝试这样声明:
{"name": "fieldName", "type": ["null", "int"], default: null }