首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Avro模式。如何同时设置类型为“记录”和“空”

Avro模式。如何同时设置类型为“记录”和“空”
EN

Stack Overflow用户
提问于 2016-03-31 07:33:39
回答 1查看 9.7K关注 0票数 10

我需要在Schema中混合使用"record“类型和null类型。

代码语言:javascript
复制
"name":"specShape",
         "type":{
            "type":"record",
            "name":"noSpecShape",
            "fields":[
               {
                  "name":"bpSsc",
                  "type":"null",
                  "default":null,
                  "doc":"SampleValue: null"
               },...

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

因此,如果我将type设置为

代码语言:javascript
复制
"name":"specShape",
         "type":{
            **"type":["record", "null"],**
            "name":"noSpecShape",
            "fields":[
               {
                  "name":"bpSsc",
                  "type":"null",
                  "default":null,
                  "doc":"SampleValue: null"
               },...

上面写着

代码语言:javascript
复制
No type: {"type":["record","null"]...

但如果我将呼叫器类型设置为

代码语言:javascript
复制
"name":"specShape",
         **"type":[{
            "type":"record",
            "name":"noSpecShape",
            "fields":[
               {
                  "name":"bpSsc",
                  "type":"null",
                  "default":null,
                  "doc":"SampleValue: null"
               }, "null"]**,...

上面写着

代码语言:javascript
复制
Not in union [{"type":"record"

如何将这两种类型结合起来?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-03-31 09:36:16

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

代码语言:javascript
复制
[
  "null",
  {
    "type": "record",
    "name": "NoSpecShape",
    "fields": [
      {
        "type": "null",
        "name": "bpSsc",
        "default": null
      }
    ]
  }
]

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

代码语言:javascript
复制
{
  "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}}}
票数 17
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/36321616

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档