如何在Avro架构中嵌套记录?

Jor*_*nda 27 python avro

我正在尝试让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)
Run Code Online (Sandbox Code Playgroud)

......我得到以下异常:

avro.schema.SchemaParseException: Type property "record" not a valid Avro schema: Could not make an Avro Schema object from record.
Run Code Online (Sandbox Code Playgroud)

我究竟做错了什么?

Mar*_*Wit 41

根据网络上的其他消息来源,我会重写你的第二个地址定义:

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"}
                        ]
                    }
        }
    ]
}"""
Run Code Online (Sandbox Code Playgroud)

  • 谢谢,马可,那行得通。地址名称的第二个声明(您在其中编写“AddressUSRecord”的那个)似乎是解析架构所必需的,但在处理符合架构的数据时被忽略。 (2认同)
  • 这没什么意义。为什么`person`可以有`record`的`type`,而`address`不能? (2认同)

Ani*_*Ani 6

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

"name":"some_name",
"type": {
          "name":"CodeClassName",
           "type":"record/enum/array"
 } 
Run Code Online (Sandbox Code Playgroud)

但是,如果命名的类型是并集,则我们不需要额外的类型字段,并且可以将其用作:

"name":"some_name",
"type": [{
          "name":"CodeClassName1",
           "type":"record",
           "fields": ...
          },
          {
           "name":"CodeClassName2",
            "type":"record",
            "fields": ...
}]
Run Code Online (Sandbox Code Playgroud)

希望这进一步澄清!