Created
August 22, 2021 07:15
-
-
Save adkdev/80b9a90e123b8747989c9f0c6028e359 to your computer and use it in GitHub Desktop.
Convert Avro document to Json in Python using fastavro
This file contains 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
import fastavro | |
import json | |
import os | |
ymd = "2021-08-21" | |
s3_path = "path/to/avro/fils/with/date={}" | |
data = [] | |
for i in range(0, 24): | |
ymdh = f"{ymd}-{i:02d}" | |
target_dir = s3_path.format(ymdh) | |
files = sorted([os.path.join(target_dir, f) for f in os.listdir(target_dir) if f.endswith(".avro")]) | |
for f in files: | |
with open(f, "rb") as fo: | |
for record in fastavro.reader(fo): | |
data.append(record) | |
# print(record) | |
with open('out.json', 'w') as out: | |
json.dump(data, out, sort_keys=True, indent=4) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment