使用Python生成的Avro记录类型不明确

2024-10-02 08:29:27 发布

您现在位置:Python中文网/ 问答频道 /正文

我使用的是Python Avro实现。我用3个可能的实例声明了字段person,分别是null、Man和Woman:

{
    "type":"record",
    "name":"Example",
    "namespace":"marboni",
    "fields":[
        {
            "name":"person",
            "type":[
                "null",
                {
                    "type":"record",
                    "name":"Man",
                    "fields":[
                        {
                            "name":"gym",
                            "type": "string",
                            "default":null
                        }
                    ]
                },
                {
                    "type":"record",
                    "name":"Woman",
                    "fields":[
                        {
                            "name":"beautySalon",
                            "type": "string",
                            "default":null
                        }
                    ]
                }
            ]
        }
    ]
}

然后我假设是个男人写记录:

^{pr2}$

结果是可预测的:

{u'person': {u'gym': u'Sport 4 Live'}}

然后我将空值添加到男性的“健身房”字段和女性的“美容沙龙”字段:

{
    "type":"record",
    "name":"Example",
    "namespace":"marboni",
    "fields":[
        {
            "name":"person",
            "type":[
                "null",
                {
                    "type":"record",
                    "name":"Man",
                    "fields":[
                        {
                            "name":"gym",
                            "type": ["null", "string"],
                            "default":null
                        }
                    ]
                },
                {
                    "type":"record",
                    "name":"Woman",
                    "fields":[
                        {
                            "name":"beautySalon",
                            "type": ["null", "string"],
                            "default":null
                        }
                    ]
                }
            ]
        }
    ]
}

运行相同的代码并获得:

{u'person': {u'beautySalon': None}}

哎呀,有了空字段,我们的男人变成了女人。可悲。在

有人知道怎么解决吗?在


Tags: namedefaultfieldsstringexampletyperecordnamespace

热门问题