支持存储任意嵌套python数据结构的avro模式和数据转换器。
rec-avro的Python项目详细描述
记录avro:
avro支持json和其他嵌套数据结构。
rec avro提供了一个通用的avro模式和转换函数,允许在avro中存储嵌套的python数据结构。
仅在Python3中测试。
安装:
$ pip3 install rec-avro
用法:
使用Fastavro:
fromfastavroimportwriter,reader,schemafromrec_avroimportto_rec_avro_destructive,from_rec_avro_destructive,rec_avro_schemadefjson_objects():return[{'a':'a'},{'b':'b'}]# For efficiency, to_rec_avro_destructive() destroys rec, and reuses it's# data structures to construct avro_objects avro_objects=(to_rec_avro_destructive(rec)forrecinjson_objects())# store records in avrowithopen('json_in_avro.avro','wb')asf_out:writer(f_out,schema.parse_schema(rec_avro_schema()),avro_objects)#load records from avrowithopen('json_in_avro.avro','rb')asf_in:# For efficiency, from_rec_avro_destructive(rec) destroys rec, and # reuses it's data structures to construct it's outputloaded_json=[from_rec_avro_destructive(rec)forrecinreader(f_in)]assertloaded_json==json_objects()
开发:
# Running all tests: $ python setup.py pytest # Running tests manually $ pip3 install fastavro pytest $ python setup.py develop $ pytest tests/test_rec_avro.py