官网示例文档:http://avro.apache.org/docs/current/gettingstartedpython.html#download_install
需要注意的是,官网给出的是py2.x的示例代码。
py3 需要做一些改动:
- 首先你需要下载avro_python3 而不是avro
- 然后对代码做以下调整(黄底部分)
import avro.schema from avro.datafile import DataFileReader, DataFileWriter from avro.io import DatumReader, DatumWriter schema = avro.schema.Parse(open("user.avsc", "rb").read()) writer = DataFileWriter(open("users.avro", "wb"), DatumWriter(), schema) writer.append({"name": "Alyssa", "favorite_number": 256}) writer.append({"name": "Ben", "favorite_number": 7, "favorite_color": "red"}) writer.close() reader = DataFileReader(open("users.avro", "rb"), DatumReader()) for user in reader: print(user) reader.close()
-
user.avsc 文件哪来?
直接将文章中的以下内容存到文件中就行了。
{"namespace": "example.avro", "type": "record", "name": "User", "fields": [ {"name": "name", "type": "string"}, {"name": "favorite_number", "type": ["int", "null"]}, {"name": "favorite_color", "type": ["string", "null"]} ] }