-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
63 lines (44 loc) · 1.75 KB
/
app.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
import sys
import glob
import os
import json
import re
import pandas as pd
def get_column_names(schemas, ds_name, sorting_key='column_position'):
column_details = schemas[ds_name]
columns = sorted(column_details, key=lambda col: col[sorting_key])
return[col['column_name'] for col in columns]
def read_csv(file, schemas):
file_path_list = re.split('[/\\\]', file)
ds_name = file_path_list[-2]
file_name = file_path_list[-1]
columns = get_column_names(schemas, ds_name)
df = pd.read_csv(file, names=columns)
return df
def to_json(df, tgt_base_dir, ds_name, file_name):
json_file_path = f'{tgt_base_dir}/{ds_name}/{file_name}'
os.makedirs(f'{tgt_base_dir}/{ds_name}', exist_ok=True)
df.to_json(
json_file_path,
orient = 'records',
lines=True
)
def file_converter(src_base_dir, tgt_base_dir, ds_name):
schemas = json.load(open(f'{src_base_dir}/schemas.json'))
files = glob.glob(f'{src_base_dir}/{ds_name}/part-*')
for file in files:
df = read_csv(file, schemas)
file_name = re.split('[/\\\]', file)[-1]
to_json(df, tgt_base_dir, ds_name, file_name)
def process_files(ds_names=None):
src_base_dir = os.environ.get('SRC_BASE_DIR')
tgt_base_dir = os.environ.get('TGT_BASE_DIR')
schemas = json.load(open(f'{src_base_dir}/schemas.json'))
if not ds_names:
ds_names = schemas.keys()
for ds_name in ds_names:
print(f'Processing {ds_name}')
file_converter(src_base_dir, tgt_base_dir, ds_name)
if __name__ == '__main__':
ds_names = json.loads(sys.argv[1])
process_files(ds_names)