【问题标题】:How to nest records in an Avro schema?如何在 Avro 模式中嵌套记录?
【发布时间】:2012-07-30 15:31:12
【问题描述】:

我正在尝试让 Python 解析 Avro 架构,例如以下...

from avro import schema

mySchema = """
{
    "name": "person",
    "type": "record",
    "fields": [
        {"name": "firstname", "type": "string"},
        {"name": "lastname", "type": "string"},
        {
            "name": "address",
            "type": "record",
            "fields": [
                {"name": "streetaddress", "type": "string"},
                {"name": "city", "type": "string"}
            ]
        }
    ]
}"""

parsedSchema = schema.parse(mySchema)

...我得到以下异常:

avro.schema.SchemaParseException: Type property "record" not a valid Avro schema: Could not make an Avro Schema object from record.

我做错了什么?

【问题讨论】:

    标签: python avro


    【解决方案1】:

    根据网络上的其他来源,我将重写您的第二个地址定义:

    mySchema = """
    {
        "name": "person",
        "type": "record",
        "fields": [
            {"name": "firstname", "type": "string"},
            {"name": "lastname", "type": "string"},
            {
                "name": "address",
                "type": {
                            "type" : "record",
                            "name" : "AddressUSRecord",
                            "fields" : [
                                {"name": "streetaddress", "type": "string"},
                                {"name": "city", "type": "string"}
                            ]
                        }
            }
        ]
    }"""
    

    【讨论】:

    • 谢谢,马可,这很有效。地址名称的第二个声明(您编写“AddressUSRecord”的那个)似乎是解析架构所必需的,但在处理符合架构的数据时会被忽略。
    • 这没什么意义。为什么person 可以有typerecord,而address 不能?
    • 在 avro 规范中,它允许type 像这样扩展吗?
    • 查看规范的 Parsing Canonical Form 部分。:avro.apache.org/docs/current/… 据我了解,所有类型都被扩展,甚至是原语,我们通常看到的一个词是 Parsed Canonical Form of架构。所以当我们写: {"type": "string"} 和写一样, {"type": {"type": "string"}}
    • 如果我早点找到这个答案,本可以节省我 1 天的调试时间。
    【解决方案2】:

    每次我们将类型提供为命名类型时,需要将字段指定为:

    "name":"some_name",
    "type": {
              "name":"CodeClassName",
               "type":"record/enum/array"
     } 
    

    但是,如果命名类型是 union,那么我们不需要额外的类型字段并且应该可以用作:

    "name":"some_name",
    "type": [{
              "name":"CodeClassName1",
               "type":"record",
               "fields": ...
              },
              {
               "name":"CodeClassName2",
                "type":"record",
                "fields": ...
    }]
    

    希望这进一步澄清!

    【讨论】:

      猜你喜欢
      • 2018-10-11
      • 1970-01-01
      • 1970-01-01
      • 2017-04-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多