首页 > 解决方案 > Python Avro,如何将数据写入修改后的模式?

问题描述

我是 Avro 的新手,我正在尝试执行基本任务,例如从中读取数据data.avro,现在我想将数据写入data.avro.

我的问题是:...is not an example of the schema...

我不明白我的错误在哪里,感谢您的帮助:

from avro import schema, datafile, io
from avro.datafile import DataFileWriter, DataFileReader
from avro.io import DatumWriter, DatumReader

OUTFILE_NAME = '4_2.avro'
SCHEMA_STR = """{
    "namespace": "advdaba",
    "type": "record",
    "name": "Conference",
    "fields": [
        { "name": "name",       "type": "string" },
        { "name": "time",       "type": "long" },
        { "name": "location",   "type": "string" },
        { "name": "speakers",   "type": {"type":"array","items":"string"} },
        { "name": "participants", "type": {"type": "array", "items": "string"} },
        { "name": "seating", "type": {"type": "map", "values": "int"} }
    ]
}"""

SCHEMA = schema.parse(SCHEMA_STR)

# read data writtent with the old schema
reader = DataFileReader(open("example.avro", "rb"), DatumReader())
#data = []
for example in reader:
    print(example)
    #data.append(example)
reader.close()

# generate data for new schema
data = {
    'name': 'Foo',
    'time': 25612345,
    'location': 'Berne',
    'speakers': ['Jean', 'Elton'],
    'participants': ['John', 'Michel', 'Jacques'],
    'seating': [{'John': 1}, {'Michel': 2}, {'Jacques': 3}]
}

rec_writer = io.DatumWriter(SCHEMA)
df_writer = datafile.DataFileWriter(
    open(OUTFILE_NAME, 'wb'),
    rec_writer,
    writers_schema=SCHEMA,
    codec='deflate'
)

df_writer.append(data)
df_writer.close()

我认为据我了解,甚至可以将新旧数据写入同一个.avro文件中

[编辑]调试后问题来自seating

标签: pythonavro

解决方案


问题出在seating

解决方案是 'seating': {"John": 1, "Michel": 2, "Jacques": 3}


推荐阅读