python - 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
解决方案
问题出在seating
解决方案是
'seating': {"John": 1, "Michel": 2, "Jacques": 3}
推荐阅读
- javascript - 我尝试验证一个不应允许任何字符串的文本框
- windows - 用于 32 个 IP 的并行跟踪路由的 windows cmd 批处理文件
- python - 如何对多个值应用正则表达式以在熊猫中创建新列?
- hyperlink - 激活粘贴的链接
- .net - .NET 表单:更改 DataSource 后标记更改的 DataGridView 单元格
- flutter - 在颤动中读取带有标题的csv文件映射
- java - 您可以将 MatOfRect 转换为 MatOfRect2d 吗?
- multithreading - Perl线程:哈希问题数组
- azure - Azure Kubernetes 服务:如何自动将 Pod 从 Spot 节点池移动到常规节点池?
- google-cloud-platform - 使用 istio 和 GKE 的多集群入口