python读写模式_使用python avro库时读写模式

avro规范允许使用不同的写入和读取模式,只要它们匹配即可.该规范还允许别名来满足读取和写入模式之间的差异.以下

python 2.7试图说明这一点.

import uuid

import avro.schema

import json

from avro.datafile import DataFileReader, DataFileWriter

from avro.io import DatumReader, DatumWriter

write_schema = {

"namespace": "example.avro",

"type": "record",

"name": "User",

"fields": [

{"name": "name", "type": "string"},

{"name": "favorite_number", "type": ["int", "null"]},

{"name": "favorite_color", "type": ["string", "null"]}

]

}

writer = DataFileWriter(open("users.avro", "wb"), DatumWriter(write_schema))

writer.append({"name": "Alyssa", "favorite_number": 256})

writer.append({"name": "Ben", "favorite_number": 7, "favorite_color": "red"})

writer.close()

read_schema = {

"namespace": "example.avro",

"type": "record",

"name": "User",

"fields": [

{"name": "first_name", "type": "string", "aliases": ["name"]},

{"name": "favorite_number", "type": ["int", "null"]},

{"name": "favorite_color", "type": ["string", "null"]}

]

}

# 1. open avro and extract passport + data

reader = DataFileReader(open("users.avro", "rb"), DatumReader(write_schema, read_schema))

reader.close()

此代码包含以下错误消息:

/Library/Frameworks/Python.framework/Versions/2.7/bin/python2.7 /Users/simonshapiro/python_beam/src/avrov_test.py

Traceback (most recent call last):

File "/Users/simonshapiro/python_beam/src/avrov_test.py", line 67, in

writer.append({"name": "Alyssa", "favorite_number": 256})

File "/Library/Python/2.7/site-packages/avro/datafile.py", line 196, in append

self.datum_writer.write(datum, self.buffer_encoder)

File "/Library/Python/2.7/site-packages/avro/io.py", line 768, in write

if not validate(self.writers_schema, datum):

File "/Library/Python/2.7/site-packages/avro/io.py", line 103, in validate

schema_type = expected_schema.type

AttributeError: 'dict' object has no attribute 'type'

Process finished with exit code 1

使用此行在没有不同模式的情况下运行时

reader = DataFileReader(open("users.avro", "rb"), DatumReader())

它工作正常.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值