Union 类型为 null 或 UUID 的正确 Avro 模式是什么

What is the correct Avro schema for Union type of null or UUID

我正在尝试为可选的可为 null 的 GUID 定义 Avro 模式。像这样将 logicalType 作为联合类型的一部分是否正确?

looked in the docs 找不到此用例的示例

{
  "name": "MyMessageType",
  "type": "record",
  "namespace": "com.mycompany.kafka.records",
  "fields": [
    {
      "name": "userId",
      "type": [
        "null",
        {
          "type": "string",
          "logicalType": "uuid"
        }
      ],
      "default": null,
    },
    { ... more fields ... }
  ]
}

正确的方法是如你所写,逻辑类型是类型定义的一部分而不是联合定义的一部分。

例如 正确的方法:

  "type": [
    "null",
    {
      "type": "string",
      "logicalType": "uuid" //Logical type is part of the 'string' type definition
    }
  ],

并且不作为联合定义的一部分。

例如走错路

  "logicalType": "uuid" //Wrong way - Logical type is NOT part of the 'union' type definition
  "type": [
    "null",
    {
      "type": "string",
    }
  ],

顺便说一句,你可以有定义几个不同逻辑类型的联合,这可能是逻辑类型是联合分支的一部分而不是整个联合的一部分的原因。