我正在使用Python Avro实现。我用3个可能的实例声明了字段 person - null,男人和女人:
{
"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'}}
然后我在男人的“健身房”字段和女人的“beautySalon”字段中添加NULL:
{
"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}}
哎呀,可空的田野,我们的男人成了女人。悲伤。
有人知道怎么解决吗?