Null Avro模式。如何将类型设置为";“记录”;及;空";马上

Null Avro模式。如何将类型设置为";“记录”;及;空";马上,null,schema,union,record,avro,Null,Schema,Union,Record,Avro,我需要在模式中混合使用“记录”类型和空类型 "name":"specShape", "type":{ "type":"record", "name":"noSpecShape", "fields":[ { "name":"bpSsc", "type":"null", "d

我需要在模式中混合使用“记录”类型和空类型

"name":"specShape",
         "type":{
            "type":"record",
            "name":"noSpecShape",
            "fields":[
               {
                  "name":"bpSsc",
                  "type":"null",
                  "default":null,
                  "doc":"SampleValue: null"
               },...
例如,对于某些数据,specShape可能为null

所以如果我把type设置为

"name":"specShape",
         "type":{
            **"type":["record", "null"],**
            "name":"noSpecShape",
            "fields":[
               {
                  "name":"bpSsc",
                  "type":"null",
                  "default":null,
                  "doc":"SampleValue: null"
               },...
上面说

No type: {"type":["record","null"]...
但是如果我把whoole类型设置为

"name":"specShape",
         **"type":[{
            "type":"record",
            "name":"noSpecShape",
            "fields":[
               {
                  "name":"bpSsc",
                  "type":"null",
                  "default":null,
                  "doc":"SampleValue: null"
               }, "null"]**,...
上面说

Not in union [{"type":"record"

如何合并这两种类型?

您的想法是正确的,只需在更高级别的
“type”
数组中包含
“null”
,而不是在
“fields”
数组中(如第三个示例)。这是可为空记录的架构:

[
  "null",
  {
    "type": "record",
    "name": "NoSpecShape",
    "fields": [
      {
        "type": "null",
        "name": "bpSsc",
        "default": null
      }
    ]
  }
]
您还可以将其嵌套在需要类型声明的任何位置,例如在另一条记录中:

{
  "type": "record",
  "name": "SpecShape",
  "fields": [
    {
      "type": [
        "null",
        {
          "type": "record",
          "name": "NoSpecShape",
          "fields": [
            {
              "type": "null",
              "name": "bpSsc",
              "default": null
            }
          ]
        }
      ],
      "name": "shape"
    }
  ]
}
最后一个模式的JSON编码实例如下所示:

  • {“shape”:null}
  • {“shape”:{“NoSpecShape”:{“bpSsc”:null}}}

但现在我在添加记录时遇到“不在联盟”异常,您能帮忙吗?在答案中添加了一些示例记录,希望能有所帮助。(不要忘记,当JSON编码时,必须“包装”联合实例。)