Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix .json.gz files #1

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 50 additions & 0 deletions convert_avro2jsonl.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import gzip
import json

from fastavro import reader
from tqdm import tqdm
from pathlib import Path


def validate_file(file_path):
try:
with gzip.open(file_path) as f:
docs = []
for line in f:
docs.append(json.loads(line))
return True
except json.decoder.JSONDecodeError:
print(f"json.decoder.JSONDecodeError for file {file_path}")
return False


def avro_to_jsonl(file, overwrite=False):
with open(file, "rb") as in_file:
avro_reader = reader(in_file)
out_file_path = f"{file.split('.')[0]}.jsonl.gz"
if Path(out_file_path).exists() and not overwrite:
print(f"File {out_file_path} already exists. Skipping.")
else:
with gzip.open(out_file_path, "wt") as out_file:
for record in tqdm(avro_reader, desc=f"Processing {file}"):
out_file.write(json.dumps(record) + "\n")
assert validate_file(out_file_path), f"Validation failed for file {out_file_path}"
print(f"Validation passed for file {out_file_path}")


def main():
for file in [
"v1_20180119/test.avro",
"v1_20180119/train.avro",
"v2_20190802/train.avro",
"v2_20190802/dev.avro",
"v2_20190802/test.avro",
"v3_20200302/train.avro",
"v3_20200302/dev.avro",
"v3_20200302/test.avro",
]:
avro_to_jsonl(file, overwrite=True)


if __name__ == "__main__":
main()
Binary file removed v1_20180119/test.json.gz
Binary file not shown.
Binary file added v1_20180119/test.jsonl.gz
Binary file not shown.
Binary file removed v1_20180119/train.json.gz
Binary file not shown.
Binary file added v1_20180119/train.jsonl.gz
Binary file not shown.
Binary file removed v2_20190802/dev.json.gz
Binary file not shown.
Binary file added v2_20190802/dev.jsonl.gz
Binary file not shown.
Binary file removed v2_20190802/test.json.gz
Binary file not shown.
Binary file added v2_20190802/test.jsonl.gz
Binary file not shown.
Binary file removed v2_20190802/train.json.gz
Binary file not shown.
Binary file added v2_20190802/train.jsonl.gz
Binary file not shown.
Binary file removed v3_20200302/dev.json.gz
Binary file not shown.
Binary file added v3_20200302/dev.jsonl.gz
Binary file not shown.
Binary file removed v3_20200302/test.json.gz
Binary file not shown.
Binary file added v3_20200302/test.jsonl.gz
Binary file not shown.
Binary file not shown.