-
Notifications
You must be signed in to change notification settings - Fork 4
/
extract_load.py
404 lines (290 loc) · 10.2 KB
/
extract_load.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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
import os
from pathlib import Path
import argparse
import csv
import multiprocessing
from joblib import Parallel, parallel_backend, delayed
import pandas as pd
import yaml
from sqlalchemy import create_engine
from sqlalchemy.types import Integer, String, TIMESTAMP
num_cores = multiprocessing.cpu_count()
N_JOBS = min(num_cores, 8)
FIELD_SEP = ","
def get_parsed_args():
parser = argparse.ArgumentParser()
parser.add_argument(
"-s",
"--sources",
nargs="+",
required=False,
help="List of sources to process, e.g. pbp rosters",
)
parser.add_argument(
"-y",
"--years",
nargs="+",
required=False,
help="Years to process, e.g. 2022 2023",
)
parser.add_argument(
"-np",
"--no_prep",
action="store_true",
required=False,
help="If set, skip data prep",
)
parser.add_argument(
"-nl",
"--no_load",
action="store_true",
required=False,
help="If set, skip data load",
)
args = parser.parse_args()
return args
def read_yaml(yaml_path, storage_model="local"):
try:
with open(Path(yaml_path).resolve(), "r") as f:
yml = yaml.load(f, Loader=yaml.FullLoader)
except FileNotFoundError:
print(f"Could not find {yaml_path}. Please check that {yaml_path} exists.")
raise
return yml
def get_data(file_path):
df = pd.read_csv(file_path, low_memory=False)
return df
def save_df(df, file_path):
# Path(file_path).parents[0].mkdir(parents=True, exist_ok=True)
df.to_csv(
file_path,
index=False,
sep=FIELD_SEP,
quoting=csv.QUOTE_MINIMAL,
compression="gzip",
)
def fix_dtypes(df):
dtypes = {}
for col, _ in df.dtypes.iteritems():
# if df.dtypes[col] == "int64":
# df[col] = df[col].fillna(0)
# dtypes[col] = Integer()
# elif df.dtypes[col] == "float64":
# df[col] = df[col].fillna(0)
# if "game_date" in col:
# dtypes[col] = TIMESTAMP
# # elif df.dtypes[col] == "O":
# else:
# df[col] = df[col].fillna("")
dtypes[col] = String()
if "desc" in df.columns:
df.drop(columns=["desc"], inplace=True)
return df, dtypes
def create_empty_table(engine, df, dtypes, table_name, schema_name, replace=False):
create_sql = pd.io.sql.get_schema(
df, f"{schema_name}.{table_name}", con=engine, dtype=dtypes
)
if not replace:
drop_sql = None
create_sql = create_sql.replace("CREATE TABLE", "CREATE TABLE IF NOT EXISTS")
else:
drop_sql = f"DROP TABLE IF EXISTS {schema_name}.{table_name};"
create_sql = create_sql.replace('"', "")
# create_sql = create_sql.replace("desc ", '"desc" ')
try:
with engine.connect() as con:
if drop_sql:
con.execute(drop_sql)
con.execute(create_sql)
except Exception as ex:
print(f"EXCEPTION: {ex}")
def create_tables(
data_frames,
load_config,
dry_run=False,
replace=False,
):
schema_name = load_config["load_schema"]
for f in data_frames:
table_name = f["table_name"]
df = f["data"]
dtypes = f["dtypes"]
if not dry_run:
engine = get_engine(load_config)
print(f"Creating table {schema_name}.{table_name}...")
create_empty_table(
engine,
df,
dtypes,
table_name,
schema_name,
replace=replace,
)
def prep_data_file(data_file, load_config):
dfs = {}
file_path = data_file.resolve()
print(f"Processing {file_path}...")
if data_file.suffix == "csv":
table_name = data_file.stem
else:
table_name = Path(data_file.stem).stem
print("Table: ", table_name)
data_path = Path(load_config["base_dir"], load_config["data_dir"])
parent_dir = data_file.parents[0].relative_to(data_path)
df = get_data(file_path)
date_col = "game_date"
if date_col in df.columns:
df[date_col] = df[date_col].apply(
lambda x: pd.to_datetime(x) + pd.Timedelta(seconds=1)
)
# elif id_col in df.columns:
# df[date_col] = df[id_col].apply(
# lambda x: pd.to_datetime(str(x)[:8]) + pd.Timedelta(seconds=1)
# )
df, dtypes = fix_dtypes(df)
dfs["table_name"] = table_name
dfs["data"] = df
dfs["dtypes"] = dtypes
save_ext = "gzip" # don't use double extensions like csv.gz
target_file_path = Path(
load_config["base_dir"],
load_config["load_dir"],
parent_dir,
f"{table_name}.{save_ext}",
).resolve()
print(target_file_path)
save_df(df, target_file_path)
return dfs
def prep_data_files(data_files, load_config):
backend = parallel_backend("multiprocessing")
print(f"Spinning up {N_JOBS} jobs on {num_cores} cores...")
with backend:
dfs = Parallel(n_jobs=N_JOBS, verbose=10)(
delayed(prep_data_file)(data_file, load_config) for data_file in data_files
)
return dfs
def data_prep(
load_config,
sources_list,
years_list,
replace,
):
file_filter = "*.csv*"
years_filter = [f"{y}.csv" for y in years_list] if years_list else [".csv"]
for sub_folder in sources_list:
p = Path(load_config["data_dir"], sub_folder)
data_files = sorted(list(p.rglob(file_filter)))
data_files = (
[f for f in data_files if list(filter(f.stem.endswith, years_filter)) != []]
if years_list
else data_files
)
dfs = prep_data_files(data_files, load_config)
create_tables(
dfs,
load_config,
replace=replace,
)
def data_load_pg(
load_path, file_filter, db_host, db_user, db_password, load_database, load_schema
):
for load_file in load_path.rglob(file_filter):
table_name = f"{load_schema}.{load_file.stem}"
raw_file_path = load_file.resolve()
print(f"Loading {raw_file_path}...")
truncate_cmd = f"truncate table {table_name};"
copy_cmd = (
f"\\copy {table_name} from '{raw_file_path}' with delimiter ',' csv header;"
)
psql_cmd = f"PGPASSWORD={db_password} psql --host={db_host} --port=5432 --username={db_user} -w --dbname={load_database}"
cmd = psql_cmd + f' --command="{truncate_cmd}"'
print(cmd)
os.system(cmd)
cmd = psql_cmd + f' --command="{copy_cmd}"'
print(cmd)
os.system(cmd)
def data_load_bigquery(load_config, sources_list, years_list):
for sub_folder in sources_list:
sub_path = Path(load_config["load_path"], sub_folder)
load_schema = load_config["load_schema"]
load_database = load_config["load_database"]
load_files = sub_path.rglob("*.*")
load_files_filtered = (
[f for f in load_files if list(filter(f.stem.endswith, years_list)) != []]
if years_list
else load_files
)
for load_file in load_files_filtered:
table_name = f"{load_schema}.{load_file.stem}"
raw_file_path = load_file.resolve()
print(f"Loading {raw_file_path}...")
bq_cmd = f'bq load --project_id {load_database} --dataset_id {load_schema} --replace --skip_leading_rows 1 --field_delimiter="{FIELD_SEP}" --source_format CSV {table_name} {raw_file_path}'
print(bq_cmd)
os.system(bq_cmd)
def get_load_config():
load_config = {}
load_config["base_dir"] = "data_prep"
load_config["load_dir"] = "data_files_load"
load_config["data_dir"] = "/Users/claus/dev/nflverse-data/data"
dbt_profiles_path = Path(Path.home(), ".dbt", "profiles.yml")
dbt_profiles = read_yaml(dbt_profiles_path)
dbt_profile_name = "nfl"
# dbt_target_name = "pg_local"
dbt_target_name = "bq"
dbt_profile = dbt_profiles[dbt_profile_name]["outputs"][dbt_target_name]
db_type = dbt_profile.get("type")
db_host = dbt_profile.get("host")
db_user = dbt_profile.get("user")
db_password = dbt_profile.get("password")
load_database = "nfl-pbp"
load_schema = "raw"
load_config["db_type"] = db_type
load_config["db_host"] = db_host
load_config["db_user"] = db_user
load_config["key_file_path"] = dbt_profile.get("keyfile")
load_config["db_password"] = db_password
load_config["load_database"] = load_database
load_config["load_schema"] = load_schema
load_config["load_path"] = Path(load_config["base_dir"], load_config["load_dir"])
return load_config
def get_engine(load_config):
if load_config["db_type"] == "postgres":
url = f"postgresql://{load_config['db_user']}:{load_config['db_password']}@{load_config['db_host']}:5432/{load_config['load_database']}"
engine = create_engine(url, echo=True)
elif load_config["db_type"] == "bigquery":
url = f"bigquery://{load_config['load_database']}/{load_config['load_schema']}"
key_file_path = load_config["key_file_path"]
engine = create_engine(url, credentials_path=key_file_path, echo=False)
return engine
def main():
args = get_parsed_args()
do_prep = not args.no_prep
do_load = not args.no_load
# base_dir, load_dir, data_dir, file_filter, db_type, db_host, db_user, db_password, load_database, load_schema, engine
load_config = get_load_config()
Path(load_config["base_dir"]).mkdir(exist_ok=True)
sources_list = args.sources if args.sources else ["pbp", "players", "rosters"]
years_list = args.years
if do_prep:
replace = True
data_prep(
load_config,
sources_list,
years_list,
replace,
)
if do_load:
if load_config["db_type"] == "postgres":
data_load_pg(
load_config,
sources_list,
years_list,
)
elif load_config["db_type"] == "bigquery":
data_load_bigquery(
load_config,
sources_list,
years_list,
)
if __name__ == "__main__":
main()