如何在avro中定义一个在数组中有联合的模式?

时间:2016-08-09 20:56:47

标签: avro

我想将我的数组元素定义为联合。可能吗?如果是这样,请分享示例架构。

2 个答案:

答案 0 :(得分:0)

您可以将以下代码添加为记录字段类型

{
  "name": "some_type",
  "type": {
    "type": "array",
    "items": {
      "name": "SomeType",
      "type": "record",
      "fields": [
        {
          "name": "name",
          "type": ["null", "string"]
        },
        {
          "name": "text",
          "type": "string"
        },
        {
          "name": "type",
          "type": {
            "name": "NamedTextType",
            "type": "enum",
            "symbols": [ "named_text", "common_named_text" ]
          }
        }
      ]
    }
  }
}

希望这有帮助!

答案 1 :(得分:0)

I think this is what you are looking for:
Avro Schema:
{
  "type": "record",
  "namespace": "example.avro",
  "name": "array_union",
  "fields": [
    {
      "name": "body",
      "type": {
        "name": "body",
        "type": "record",
        "fields": [
          {
            "name": "abc",
            "type": [
              "null",
              {
                "type": "array",
                "name": "abc_name_0",
                "items": {
                  "name": "_name_0",
                  "type": "record",
                  "fields": [
                    {
                      "name": "app_id",
                      "type": [
                        "null",
                        "string"
                      ]
                    },
                    {
                      "name": "app_name",
                      "type": [
                        "null",
                        "string"
                      ]
                    },
                    {
                      "name": "app_key",
                      "type": [
                        "null",
                        "string"
                      ]
                    }
                  ]
                }
              }
            ]
          }
        ]
      }
    }
  ]
}

Valid Json that schema can accept:
{
  "body": {
    "abc": {
      "array": [
        {
          "app_id": {
            "string": "abc"
          },
          "app_name": {
            "string": "bcd"
          },
          "app_key": {
            "string": "cde"
          }
        }
      ]
    }
  }
}

Or, 

{
  "body": {
    "abc": null
  }
}
相关问题