python3 读取avro文件

官网示例文档:http://avro.apache.org/docs/current/gettingstartedpython.html#download_installhtml

须要注意的是,官网给出的是py2.x的示例代码。python

py3 须要作一些改动:apache

  1. 首先你须要下载avro_python3 而不是avro
  2. 而后对代码作如下调整(黄底部分)
    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()
  3. 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"]}
 ]
}
相关文章
相关标签/搜索