当以追加模式写入avro文件时,改变该文件的模式



我正在寻找一种在python中修改avro文件的模式的方法。以以下示例为例,使用fastavro包,首先用相应的模式写出一些初始记录:

from fastavro import writer, parse_schema
schema = {
'name': 'test',
'type': 'record',
'fields': [
{'name': 'id', 'type': 'int'},
{'name': 'val', 'type': 'long'},
],
}
records = [
{u'id': 1, u'val': 0.2},
{u'id': 2, u'val': 3.1},
]
with open('test.avro', 'wb') as f:
writer(f, parse_schema(schema), records)

哦,我有一些更多的记录,但它们包含None值。我想将这些记录附加到avro文件中,并相应地修改我的模式:

more_records = [
{u'id': 3, u'val': 1.5},
{u'id': 2, u'val': None},
]
schema['fields'][1]['type'] = ['long', 'null']
with open('test.avro', 'a+b') as f:
writer(f, parse_schema(schema), more_records)

这将导致错误,而不是覆盖模式:

ValueError: Provided schema {'type': 'record', 'name': 'test', 'fields': [{'name': 'id', 'type': 'int'}, {'name': 'val', 'type': ['long', 'null']}], '__fastavro_parsed': True, '__named_schemas': {'test': {'type': 'record', 'name': 'test', 'fields': [{'name': 'id', 'type': 'int'}, {'name': 'val', 'type': ['long', 'null']}]}}} does not match file writer_schema {'type': 'record', 'name': 'test', 'fields': [{'name': 'id', 'type': 'int'}, {'name': 'val', 'type': 'long'}], '__fastavro_parsed': True, '__named_schemas': {'test': {'type': 'record', 'name': 'test', 'fields': [{'name': 'id', 'type': 'int'}, {'name': 'val', 'type': 'long'}]}}}

是否有解决这个问题的方法?fastavro医生对此建议这是不可能的,但我希望有人知道一种方法!

欢呼

fastavro中的追加API目前不支持此功能。您可以在该存储库中打开一个问题,并讨论类似这样的内容是否有意义。

最新更新