简体   繁体   中英

Deserialize Avro Object In Python Without Having AVRO Schema On Hand

I would like to see an example of how I would deserialize an AVRO object in Python with out having an AVRO schema for it on hand.

I found an example below for deserializing but avro.io.DatumReader uses schema as input. Is it possible to extract the AVRO schema from raw_bytes?

bytes_reader = io.BytesIO(raw_bytes)
decoder = avro.io.BinaryDecoder(bytes_reader)
reader = avro.io.DatumReader(schema)
decoded_data = reader.read(decoder)

The schema passed into DatumReader is optional. If you want to read without having to supply the schema, you can create a DatumReader() without giving it a schema.

DataFileReader should work:

from avro.io import DatumReader
from avro.datafile import DataFileReader
import io

raw_bytes = ... # contents of avro file
elements = DataFileReader(io.BytesIO(raw_bytes), DatumReader())
for e in elements:
    print(e)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM