Avro Schema. How to set type to “record” and “null” at once

南笙酒味 提交于 2020-04-10 11:57:26

问题


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"
               },...

For example, for some datas specShape may be null.

So if I set type to

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

it says

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

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"]**,...

it says

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

How to union these two types?


回答1:


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-encoded instances of this last schema would look like:

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


来源:https://stackoverflow.com/questions/36321616/avro-schema-how-to-set-type-to-record-and-null-at-once

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!