Avro架构.如何一次将类型设置为"记录"和"空"

Nad*_*zov 5 union null schema record avro

我需要在Schema中将"record"类型与null类型混合使用.

"name":"specShape",
         "type":{
            "type":"record",
            "name":"noSpecShape",
            "fields":[
               {
                  "name":"bpSsc",
                  "type":"null",
                  "default":null,
                  "doc":"SampleValue: null"
               },...
Run Code Online (Sandbox Code Playgroud)

例如,对于某些数据,specShape可能为null.

所以,如果我将类型设置为

"name":"specShape",
         "type":{
            **"type":["record", "null"],**
            "name":"noSpecShape",
            "fields":[
               {
                  "name":"bpSsc",
                  "type":"null",
                  "default":null,
                  "doc":"SampleValue: null"
               },...
Run Code Online (Sandbox Code Playgroud)

它说

No type: {"type":["record","null"]...
Run Code Online (Sandbox Code Playgroud)

但是如果我将whoole类型设置为

"name":"specShape",
         **"type":[{
            "type":"record",
            "name":"noSpecShape",
            "fields":[
               {
                  "name":"bpSsc",
                  "type":"null",
                  "default":null,
                  "doc":"SampleValue: null"
               }, "null"]**,...
Run Code Online (Sandbox Code Playgroud)

它说

Not in union [{"type":"record"
Run Code Online (Sandbox Code Playgroud)

如何结合这两种类型?

mtt*_*tth 9

你有正确的想法,你只需要包含"null"在更高级别的"type"数组中而不是在"fields"数组中(如第三个例子中所示).这是可空记录的架构:

[
  "null",
  {
    "type": "record",
    "name": "NoSpecShape",
    "fields": [
      {
        "type": "null",
        "name": "bpSsc",
        "default": null
      }
    ]
  }
]
Run Code Online (Sandbox Code Playgroud)

您还可以将其嵌套在预期类型声明的任何位置,例如在另一个记录中:

{
  "type": "record",
  "name": "SpecShape",
  "fields": [
    {
      "type": [
        "null",
        {
          "type": "record",
          "name": "NoSpecShape",
          "fields": [
            {
              "type": "null",
              "name": "bpSsc",
              "default": null
            }
          ]
        }
      ],
      "name": "shape"
    }
  ]
}
Run Code Online (Sandbox Code Playgroud)

最后一个模式的JSON编码实例如下所示:

  • {"shape": null}
  • {"shape": {"NoSpecShape": {"bpSsc": null}}}