json文档缺少字段的avro序列化问题

whitzsjs  于 2021-06-02  发布在  Hadoop
关注(0)|答案(1)|浏览(485)

我正在尝试使用apacheavro对从elasticsearch导出到hdfs中的许多avro文档中的数据实施模式(使用drill进行查询)。我对avro的默认值有点问题
鉴于此模式:

{    
  "namespace" : "avrotest",    
  "type" : "record",    
  "name" : "people",                                                                                                   
  "fields" : [                                                                                                         
    {"name" : "firstname", "type" : "string"},                                                                        
    {"name" : "age", "type" :"int", "default": -1}                                                                     
  ]                                                                                                                    
}

我希望json文档 {"firstname" : "Jane"} 将使用的默认值序列化 -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)

这是可能的,还是我遗漏了什么?

iyfamqjs

iyfamqjs1#

关键是,如果您在模式中这样声明字段:

{"name": "fieldName", "type": ["int", "null"], default: null }

仅使用像optional这样的字段是不够的,请尝试这样声明:

{"name": "fieldName", "type": ["null", "int"], default: null }

相关问题