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

时间:2022-06-24 19:35:41

I'm trying to get Python to parse Avro schemas such as the following...

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

...and I get the following exception:

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

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

What am I doing wrong?

我究竟做错了什么?

2 个解决方案

#1


30  

According to other sources on the web I would rewrite your second address definition:

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

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"}
                        ]
                    },
        }
    ]
}"""

#2


4  

Every time we provide the type as named type, the field needs to be given as:

每次我们提供类型为命名类型时,该字段需要给出:

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

However, if the named type is union, then we do not need an extra type field and should be usable as:

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

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

Hope this clarifies further!

希望这进一步澄清!

#1


30  

According to other sources on the web I would rewrite your second address definition:

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

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"}
                        ]
                    },
        }
    ]
}"""

#2


4  

Every time we provide the type as named type, the field needs to be given as:

每次我们提供类型为命名类型时,该字段需要给出:

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

However, if the named type is union, then we do not need an extra type field and should be usable as:

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

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

Hope this clarifies further!

希望这进一步澄清!