Created
January 18, 2022 16:31
-
-
Save rupeshtiwari/57b2fa1b91928c1046364d2948c029f3 to your computer and use it in GitHub Desktop.
writing reading avro file using python
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Python 3 with `avro-python3` package available | |
import copy | |
import json | |
import avro | |
from avro.datafile import DataFileWriter, DataFileReader | |
from avro.io import DatumWriter, DatumReader | |
# Note that we combined namespace and name to get "full name" | |
schema = { | |
'name': 'avro.example.User', | |
'type': 'record', | |
'fields': [ | |
{'name': 'name', 'type': 'string'}, | |
{'name': 'age', 'type': 'int'} | |
] | |
} | |
# Parse the schema so we can use it to write the data | |
schema_parsed = avro.schema.Parse(json.dumps(schema)) | |
# Write data to an avro file | |
with open('users.avro', 'wb') as f: | |
writer = DataFileWriter(f, DatumWriter(), schema_parsed) | |
writer.append({'name': 'Pierre-Simon Laplace', 'age': 77}) | |
writer.append({'name': 'John von Neumann', 'age': 53}) | |
writer.close() | |
# Read data from an avro file | |
with open('users.avro', 'rb') as f: | |
reader = DataFileReader(f, DatumReader()) | |
metadata = copy.deepcopy(reader.meta) | |
schema_from_file = json.loads(metadata['avro.schema']) | |
users = [user for user in reader] | |
reader.close() | |
print(f'Schema that we specified:\n {schema}') | |
print(f'Schema that we parsed:\n {schema_parsed}') | |
print(f'Schema from users.avro file:\n {schema_from_file}') | |
print(f'Users:\n {users}') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment