38

我正在尝试让 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.

我究竟做错了什么?

4

2 回答 2

56

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

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"}
                        ]
                    }
        }
    ]
}"""
于 2012-08-01T18:10:18.390 回答
7

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

"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": ...
}]

希望这进一步澄清!

于 2016-07-30T14:44:31.190 回答