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

2020-07-10 06:38发布

问题:

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}}}