验证用户使用默认值提供针对avro架构的json数据

时间:2016-11-09 01:35:15

标签: avro

我有这个avro架构

{
  "type": "record",
  "name": "name",
  "fields": [
    {
      "type": "string",
      "name": "field1",
    },
    {
      "type": ["null","string"],
      "name": "field2",
      "default": null
    }
  ]
}

和相应的json输入

{
        "field1": "value1",
        "field2": "value2"
}

它通过了avro-tools提供的avro验证器

java -jar avro-tools-1.7.4.jar jsontofrag  "`cat schema.avsc`" data.json

但是,如果我省略该字段并输入json值,则field2是可选的 如

{
        "field1": "value1"
}

它抛出

的例外
Exception in thread "main" org.apache.avro.AvroTypeException: Expected field name not found: field2

有什么办法可以解决这个问题吗?我不想为具有可能的默认空值的字段手动添加空值,例如在这种情况下为field2

有什么建议吗?

0 个答案:

没有答案