【问题标题】:Avro schema getting undefined type name when using Record type使用记录类型时,Avro 模式获取未定义的类型名称
【发布时间】:2020-07-23 08:02:29
【问题描述】:

所以我试图用这个 avro 模式解析一个对象。

对象是这样的:

myInfo: {size: 'XL'}

但它的行为就像记录类型实际上并不存在,我得到了一个undefined type name: data.platform_data.test_service.result.record at Function.Type.forSchema

架构看起来像:

  "avro": {
    "metadata": {
      "loadType": "full",
      "version": "0.1"
    },
    "schema": {
      "name": "data.platform_data.test_service.result",
      "type": "record",
      "fields": [
        {
          "name": "myInfo",
          "type": "record",
          "fields": [{
            "name": "size",
            "type": {"name":"size", "type": "string"}
          }]
      }
      ]
    }
  }

我应该提到我也为此使用avsc。有人有什么想法吗?我已经尝试了几乎所有的组合,但 afaik 解析像这样的对象的唯一方法是使用 record

【问题讨论】:

    标签: avro avsc


    【解决方案1】:

    玩弄架构,我发现"type": "record" 是个问题。我将其移至嵌套定义。它奏效了。似乎描述 here 有点混乱。

    改变 之前:

    {
      "name": "myInfo",
      "type": "record",
      "fields": [{
        "name": "size",
        "type": {"name":"size", "type": "string"}
      }]
    }
    

    之后:

    {
      "name": "myInfo",
      "type": {
        "type": "record",
        "name": "myInfo",
        "fields": [
          {
            "name": "size",
            "type": {"name":"size", "type": "string"}
          }
        ]
      }
    }
    

    更新的架构正在运行:

    {
      "name": "data.platform_data.test_service.result",
      "type": "record",
      "fields": [
        {
          "name": "myInfo",
          "type": {
            "type": "record",
            "name": "myInfo",
            "fields": [
              {
                "name": "size",
                "type": {"name":"size", "type": "string"}
              }
            ]
          }
        }
      ]
    }
    

    要使记录属性为空,过程与任何其他属性相同。您需要与"null" 联合(如下图所示):

    {
      "name": "data.platform_data.test_service.result",
      "type": "record",
      "fields": [
        {
          "name": "myInfo",
          "type": [
            "null",
            {
              "type": "record",
              "name": "myInfo",
              "fields": [
                {
                  "name": "size",
                  "type": {
                    "name": "size",
                    "type": "string"
                  }
                }
              ]
            }
          ]
        }
      ]
    }
    

    【讨论】:

    • 这项工作非常感谢!在这种情况下,您如何使 myInfo 字段可以为空?我尝试将类型转换为数组,第一个是当前对象,第二个位置是null,但它没有用。
    • @Strobe_,更新了包含可为空记录属性的答案。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-10-12
    • 1970-01-01
    • 2016-02-14
    • 2021-07-03
    • 1970-01-01
    相关资源
    最近更新 更多