有没有办法在不使用Python的模式定义的情况下将JSON字符串转换为Avro?还是只有Java才能处理的事情?
小智 7
我最近遇到了同样的问题,我最终开发了一个 python 包,它可以采用任何 python 数据结构,包括解析的 JSON 并将其存储在 Avro 中,而无需专用模式。
我为python 3测试了它。
您可以将其安装为pip3 install rec-avro或在https://github.com/bmizhen/rec-avro 上查看代码和文档
用法示例:
from fastavro import writer, reader, schema
from rec_avro import to_rec_avro_destructive, from_rec_avro_destructive, rec_avro_schema
def json_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) for rec in json_objects())
# store records in avro
with open('json_in_avro.avro', 'wb') as f_out:
writer(f_out, schema.parse_schema(rec_avro_schema()), avro_objects)
#load records from avro
with open('json_in_avro.avro', 'rb') as f_in:
# For efficiency, from_rec_avro_destructive(rec) destroys rec, and
# reuses it's data structures to construct it's output
loaded_json = [from_rec_avro_destructive(rec) for rec in reader(f_in)]
assert loaded_json == json_objects()
Run Code Online (Sandbox Code Playgroud)
要将 JSON 字符串转换为 json 对象,请使用 json.loads('{"a":"b"}')
sds*_*sds -5
Apache Avro\xe2\x84\xa2 1.7.6 入门 (Python):
\n\nimport avro.schema\navro.schema.parse(json_schema_string)\nRun Code Online (Sandbox Code Playgroud)\n