How to covert JSON string to Avro in Python?

旧时模样 提交于 2019-11-26 21:51:19

问题


Is there a way to convert a JSON string to an Avro without a schema definition in Python? Or is this something only Java can handle?


回答1:


Apache Avro™ 1.7.6 Getting Started (Python):

import avro.schema
avro.schema.parse(json_schema_string)



回答2:


I recently had the same problem, and I ended up developing a python package that can take any python data structure, including parsed JSON and store it in Avro without a need for a dedicated schema.

I tested it for python 3.

You can install it as pip3 install rec-avro or see the code and docs at https://github.com/bmizhen/rec-avro

Usage Example:

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()

To convert a JSON string to json objects use json.loads('{"a":"b"}')




回答3:


This should help:

b = BytesIO(b'some message')
reader = DataFileReader(b, DatumReader())

For more information take a look at this Avro Python Guide.



来源:https://stackoverflow.com/questions/22382636/how-to-covert-json-string-to-avro-in-python

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!