-
Notifications
You must be signed in to change notification settings - Fork 62
/
csv_transform.py
290 lines (251 loc) · 10.3 KB
/
csv_transform.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
# Copyright 2021 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import datetime
import fnmatch
import json
import logging
import os
import pathlib
import typing
import zipfile as zip
import pandas as pd
import requests
from google.cloud import storage
def main(
source_url: str,
start_year: int,
source_file: pathlib.Path,
target_file: pathlib.Path,
chunksize: str,
target_gcs_bucket: str,
target_gcs_path: str,
data_names: typing.List[str],
data_dtypes: dict,
) -> None:
logging.info("Pipeline process started")
pathlib.Path("./files").mkdir(parents=True, exist_ok=True)
dest_path = os.path.split(source_file)[0]
end_year = datetime.datetime.today().year - 2
download_url_files_from_year_range(
source_url, start_year, end_year, dest_path, True, False
)
st_year = datetime.datetime.today().year - 1
end_year = datetime.datetime.today().year
download_url_files_from_year_range(
source_url, st_year, end_year, dest_path, True, True
)
file_group_wildcard = os.path.split(source_url)[1].replace("_YEAR_ITERATOR.zip", "")
source = concatenate_files(source_file, dest_path, file_group_wildcard, False, ",")
process_source_file(source, target_file, data_names, data_dtypes, int(chunksize))
upload_file_to_gcs(target_file, target_gcs_bucket, target_gcs_path)
logging.info("Pipeline process completed")
def download_url_files_from_year_range(
source_url: str,
start_year: int,
end_year: int,
dest_path: str,
remove_file: bool = False,
continue_on_error: bool = False,
):
for yr in range(start_year, end_year + 1, 1):
src_url = source_url.replace("YEAR_ITERATOR", str(yr))
dest_file = dest_path + "/source_" + os.path.split(src_url)[1]
download_file_http(src_url, dest_file, continue_on_error)
unpack_file(dest_file, dest_path, "zip")
if remove_file:
os.remove(dest_file)
def download_file_http(
source_url: str, source_file: pathlib.Path, continue_on_error: bool = False
) -> None:
logging.info(f"Downloading {source_url} to {source_file}")
try:
src_file = requests.get(source_url, stream=True)
with open(source_file, "wb") as f:
for chunk in src_file:
f.write(chunk)
except requests.exceptions.RequestException as e:
if e == requests.exceptions.HTTPError:
err_msg = "A HTTP error occurred."
elif e == requests.exceptions.Timeout:
err_msg = "A HTTP timeout error occurred."
elif e == requests.exceptions.TooManyRedirects:
err_msg = "Too Many Redirects occurred."
if not continue_on_error:
logging.info(f"{err_msg} Unable to obtain {source_url}")
raise SystemExit(e)
else:
logging.info(
f"{err_msg} Unable to obtain {source_url}. Continuing execution."
)
def unpack_file(infile: str, dest_path: str, compression_type: str = "zip") -> None:
if os.path.exists(infile):
if compression_type == "zip":
logging.info(f"Unpacking {infile} to {dest_path}")
with zip.ZipFile(infile, mode="r") as zipf:
zipf.extractall(dest_path)
zipf.close()
else:
logging.info(
f"{infile} ignored as it is not compressed or is of unknown compression"
)
else:
logging.info(f"{infile} not unpacked because it does not exist.")
def zip_decompress(infile: str, dest_path: str) -> None:
logging.info(f"Unpacking {infile} to {dest_path}")
with zip.ZipFile(infile, mode="r") as zipf:
zipf.extractall(dest_path)
zipf.close()
def concatenate_files(
target_file_path: str,
dest_path: str,
file_group_wildcard: str,
incl_file_source_path: bool = False,
separator: str = ",",
delete_src_file: bool = True,
) -> str:
target_file_dir = os.path.split(str(target_file_path))[0]
target_file_path = str(target_file_path).replace(
".csv", "_" + file_group_wildcard + ".csv"
)
logging.info(f"Concatenating files {target_file_dir}/*{file_group_wildcard}")
if os.path.isfile(target_file_path):
os.unlink(target_file_path)
for src_file_path in sorted(
fnmatch.filter(os.listdir(dest_path), "*" + file_group_wildcard + "*")
):
src_file_path = dest_path + "/" + src_file_path
with open(src_file_path, "r") as src_file:
with open(target_file_path, "a+") as target_file:
next(src_file)
logging.info(
f"Reading from file {src_file_path}, writing to file {target_file_path}"
)
for line in src_file:
if incl_file_source_path:
line = (
'"'
+ os.path.split(src_file_path)[1].strip()
+ '"'
+ separator
+ line
) # include the file source
else:
line = line
target_file.write(line)
if os.path.isfile(src_file_path) and delete_src_file:
os.unlink(src_file_path)
return target_file_path
def process_source_file(
source_file: str, target_file: str, names: list, dtypes: dict, chunksize: int
) -> None:
logging.info(f"Opening batch file {source_file}")
with pd.read_csv(
source_file, # path to main source file to load in batches
engine="python",
encoding="utf-8",
quotechar='"', # string separator, typically double-quotes
chunksize=chunksize, # size of batch data, in no. of records
sep=",", # data column separator, typically ","
header=None, # use when the data file does not contain a header
names=names,
dtype=dtypes,
keep_default_na=True,
na_values=[" "],
) as reader:
for chunk_number, chunk in enumerate(reader):
target_file_batch = str(target_file).replace(
".csv", "-" + str(chunk_number) + ".csv"
)
df = pd.DataFrame()
df = pd.concat([df, chunk])
process_chunk(df, target_file_batch, target_file, (not chunk_number == 0))
def process_chunk(
df: pd.DataFrame,
target_file_batch: str,
target_file: str,
skip_header: bool,
) -> None:
df = resolve_date_format(df, "%Y-%m-%d %H:%M")
save_to_new_file(df, file_path=str(target_file_batch), sep=",")
append_batch_file(target_file_batch, target_file, skip_header, not (skip_header))
def resolve_date_format(df: pd.DataFrame, from_format: str) -> pd.DataFrame:
logging.info("Resolving Date Format")
for col in df.columns:
if df[col].dtype == "datetime64[ns]":
logging.info(f"Resolving datetime on {col}")
df[col] = df[col].apply(lambda x: convert_dt_format(str(x), from_format))
return df
def convert_dt_format(dt_str: str, from_format: str) -> str:
if not dt_str or str(dt_str).lower() == "nan" or str(dt_str).lower() == "nat":
rtnval = ""
elif len(dt_str.strip()) == 10:
# if there is no time format
rtnval = dt_str + " 00:00:00"
elif len(dt_str.strip().split(" ")[1]) == 8:
# if format of time portion is 00:00:00 then use 00:00 format
dt_str = dt_str[:-3]
rtnval = datetime.datetime.strptime(dt_str, from_format).strftime(
"%Y-%m-%d %H:%M:%S"
)
elif (len(dt_str.strip().split("-")[0]) == 4) and (
len(from_format.strip().split("/")[0]) == 2
):
# if the format of the date portion of the data is in YYYY-MM-DD format
# and from_format is in MM-DD-YYYY then resolve this by modifying the from_format
# to use the YYYY-MM-DD. This resolves mixed date formats in files
from_format = "%Y-%m-%d " + from_format.strip().split(" ")[1]
else:
dt_str = ""
return rtnval
def save_to_new_file(df, file_path, sep="|") -> None:
logging.info(f"Saving to file {file_path} separator='{sep}'")
df.to_csv(file_path, sep=sep, index=False)
def append_batch_file(
batch_file_path: str, target_file_path: str, skip_header: bool, truncate_file: bool
) -> None:
with open(batch_file_path, "r") as data_file:
if truncate_file:
target_file = open(target_file_path, "w+").close()
with open(target_file_path, "a+") as target_file:
if skip_header:
logging.info(
f"Appending batch file {batch_file_path} to {target_file_path} with skip header"
)
next(data_file)
else:
logging.info(
f"Appending batch file {batch_file_path} to {target_file_path}"
)
target_file.write(data_file.read())
if os.path.exists(batch_file_path):
os.remove(batch_file_path)
def upload_file_to_gcs(file_path: pathlib.Path, gcs_bucket: str, gcs_path: str) -> None:
logging.info(f"Uploading to GCS {gcs_bucket} in {gcs_path}")
storage_client = storage.Client()
bucket = storage_client.bucket(gcs_bucket)
blob = bucket.blob(gcs_path)
blob.upload_from_filename(file_path)
if __name__ == "__main__":
logging.getLogger().setLevel(logging.INFO)
main(
source_url=os.environ["SOURCE_URL"],
source_file=pathlib.Path(os.environ["SOURCE_FILE"]).expanduser(),
target_file=pathlib.Path(os.environ["TARGET_FILE"]).expanduser(),
start_year=int(os.environ["START_YEAR"]),
chunksize=os.environ["CHUNKSIZE"],
target_gcs_bucket=os.environ["TARGET_GCS_BUCKET"],
target_gcs_path=os.environ["TARGET_GCS_PATH"],
data_names=json.loads(os.environ["DATA_NAMES"]),
data_dtypes=json.loads(os.environ["DATA_DTYPES"]),
)