我正在使用 Python Avro 实现。我用 3 个可能的实例声明了现场人员- 空、男人和女人:
{
"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
}
]
}
]
}
]
}
然后我假设它是一个男人写记录:
writer = DataFileWriter(open('result.avro', 'w'), DatumWriter(), SCHEMA)
writer.append({
'person': {
'gym': 'Sport 4 Live'
}
})
writer.close()
reader = DataFileReader(open('result.avro', 'r'), DatumReader())
for row in reader:
print row
reader.close()
结果是可以预见的:
{u'person': {u'gym': u'Sport 4 Live'}}
然后我将 NULL 添加到男人的“gym”字段和女人的“beautySalon”字段:
{
"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}}
糟糕,有了可为空的字段,我们的男人变成了女人。伤心。
有人知道如何解决吗?