Avro模式.如何将类型设置为“记录"?和“空"立刻 [英] Avro Schema. How to set type to "record" and "null" at once

查看:143
本文介绍了Avro模式.如何将类型设置为“记录"?和“空"立刻的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在架构中将记录"类型与空类型混合使用.

I need to mix "record" type with null type in Schema.

"name":"specShape",
         "type":{
            "type":"record",
            "name":"noSpecShape",
            "fields":[
               {
                  "name":"bpSsc",
                  "type":"null",
                  "default":null,
                  "doc":"SampleValue: null"
               },...

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

For example, for some datas specShape may be null.

因此,如果我将type设置为

So if I set type to

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

No type: {"type":["record","null"]...

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

But if I set whoole type to

"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"数组中(如您的第三个示例).这是可为空的记录的模式:

You had the right idea, you just need to include "null" in the higher level "type" array instead of inside the "fields" array (as in your third example). This is the schema for a nullable record:

[
  "null",
  {
    "type": "record",
    "name": "NoSpecShape",
    "fields": [
      {
        "type": "null",
        "name": "bpSsc",
        "default": null
      }
    ]
  }
]

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

You can also nest it anywhere a type declaration is expected, for example inside another record:

{
  "type": "record",
  "name": "SpecShape",
  "fields": [
    {
      "type": [
        "null",
        {
          "type": "record",
          "name": "NoSpecShape",
          "fields": [
            {
              "type": "null",
              "name": "bpSsc",
              "default": null
            }
          ]
        }
      ],
      "name": "shape"
    }
  ]
}

最后一个模式的

JSON编码实例如下:

JSON-encoded instances of this last schema would look like:

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

这篇关于Avro模式.如何将类型设置为“记录"?和“空"立刻的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆