-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpipeline.py
155 lines (113 loc) · 4.5 KB
/
pipeline.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
# Workarrorund import modules in VSCODE
import importlib
importlib.invalidate_caches()
import contextlib
import sys
import os
sys.path.append("./spark_test")
# --- END Workarround -----------------
from pyspark.sql.types import StringType
from pyspark.sql import *
from pyspark.sql import DataFrame
import config as cfg
import file_check as fchk
import quality_check as qchk
import input
import datetime
import data_base as data
import output as out
import logging
import time
# Reload imports
importlib.reload(cfg)
importlib.reload(fchk)
importlib.reload(qchk)
importlib.reload(input)
importlib.reload(data)
importlib.reload(out)
def main(argv):
logging.basicConfig(filename='pipe_line.log', level=logging.INFO, format='%(asctime)s %(message)s', datefmt='%Y-%m-%d %I:%M:%S %p')
conf_file=""
if len(argv)==1 or argv[1]=="-h":
print('\nUsage: pipeline.py <config-file>\n')
sys.exit(2)
else:
conf_file=argv[1]
with contextlib.redirect_stdout(None):
pass
## Main Loop
while True:
# Read Configuration
config = cfg.Config(conf_file)
input_signal = config.get_signal()
input_path = config.get_input_path()
if input_signal != 'RUN':
break
# Data Base
conn = data.connect(config.get_dbpath(), config.get_dbname())
data.create_support_database(conn)
# Process only once a day
actual_date = datetime.date.today().strftime("%Y-%m-%d")
prev_date = data.get_last_date(conn)
while prev_date!=None and (prev_date == actual_date):
time.sleep(600) # 10 min check
actual_date = datetime.date.today().strftime("%Y-%m-%d")
if prev_date==None:
data.insert_process_date(conn,actual_date)
else:
data.update_process_date(conn,prev_date, actual_date)
prev_date = actual_date
logging.info("Start process " + actual_date + "." )
# Init Spark session
#spark_session.sparkContext.addPyFile(os.path.abspath("./spark_test") + "/quality_check.py")
spark_session = data.init_spark()
spark_session.sparkContext.addPyFile(os.path.abspath(".") + "/quality_check.py")
# Create spark areas dataframe
df_areas = input.read_areas(spark_session, config.get_area_path(),config.get_area_filename())
# Files to process
file_list = fchk.get_files_from_location(input_path, config.get_extension())
for file in file_list:
# Perform file check
status, error = fchk.check_file_constraints(input_path, file ,config.get_prefix(),config.get_extension())
if status==False:
logging.error("Error [" + error + "] processing: " + file )
continue
# File is ok to procdess
if status and data.can_process(conn, file):
logging.info("Start process: " + file )
start_time = datetime.datetime.strftime(datetime.datetime.now(),"%Y-%m-%d %H:%M:%S")
# Insert start process time
data.insert_start_processed(conn, start_time, file)
# Read input file
df = input.read(input_path, file, spark_session)
# Clean
df = qchk.clean(df)
# Validate location
df = qchk.validate_location(df, df_areas)
# Write bad & metadata
df_bad = out.write_bad(spark_session, config, df, file)
# Write clean
out.write_clean(spark_session, config, df, df_bad, file)
# Escribir fin de proceso
end_time = datetime.datetime.strftime(datetime.datetime.now(),"%Y-%m-%d %H:%M:%S")
data.update_end_processed(spark_session,end_time, file)
logging.info("End process: " + file )
else:
logging.error("Error [ processed ] : " + file )
# Free resources
conn.close()
data.stop_spark(spark_session)
logging.info("Finish process " + actual_date + "." )
logging.info("Signal to finish process..." )
try:
logging.info("Close Database connection..." )
conn.close()
except:
pass
try:
logging.info("Close Spark connection..." )
data.stop_spark(spark_session)
except:
pass
if __name__ == "__main__":
main( sys.argv )