-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathload_immigration.py
More file actions
135 lines (122 loc) · 4.28 KB
/
Copy pathload_immigration.py
File metadata and controls
135 lines (122 loc) · 4.28 KB
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
import configparser # Parse configuration file
import psycopg2 # PostgreSQL database adapter for the Python
import sys # Used for exiting python script in case of error
# SQL query definitions
from sql_queries import trunc_immi_table_queries, copy_immi_table_queries
from sql_queries import insert_immi_table_queries, check_zero_count, check_unique_key
"""
Purpose:
- Execute COPY queries listed in copy_immi_table_queries
- copy_immi_table_queries is defined in sql_queries file
Param:
- @cur: Redshift connection cursor
- @conn: Redshift connection
"""
def copy_tables(cur, conn):
for query in copy_immi_table_queries:
cur.execute(query)
conn.commit()
"""
Purpose:
- Execute INSERT STATEMENT queries listed in insert_immi_table_queries
- insert_immi_table_queries is defined in sql_queries file
Param:
- @cur: Redshift connection cursor
- @conn: Redshift connection
"""
def insert_into(cur, conn):
for query in insert_immi_table_queries:
cur.execute(query)
conn.commit
"""
Purpose:
- Execute TRUNCATE queries listed in trunc_immi_table_queries
- trunc_immi_table_queries is defined in sql_queries file
Param:
- @cur: Redshift connection cursor
- @conn: Redshift connection
"""
def trunc_table(cur, conn):
for query in trunc_immi_table_queries:
cur.execute(query)
conn.commit
"""
Purpose:
- Read Redshift cluster connection details from dwh.cfg config file
- Connect to cluster using config details and retrieve Redshift Connection and Cursor handle
- Call trunc_table(), copy_tables() and insert_tables() functions
"""
def main():
config = configparser.ConfigParser()
# Open and read config file to retrieve Redshift and DWH details required to connect
try:
config.read('dwh.cfg')
except Exception as e:
error(f'Error reading config file {e}')
sys.exit()
# Connect using cluster and DWH details
try:
conn = psycopg2.connect("host={} dbname={} user={} password={} port={}".format(*config['CLUSTER'].values()))
except Exception as e:
error(f'Error connecting Data Warehouse {e}')
sys.exit()
# Get conection cursor
try:
cur = conn.cursor()
except Exception as e:
error(f'Error getting connection cursor {e}')
conn.close()
sys.exit()
# Call Truncate function
try:
trunc_table(cur, conn)
except Exception as e:
error(f'Error loading staging table {e}')
conn.close()
sys.exit()
# Call COPY function
try:
copy_tables(cur, conn)
except Exception as e:
error('Error loading staging table', e)
conn.close()
sys.exit()
# Quality Check - Zero Row count check
table_list = ['stage_visitor_analysis','stage_visitors','stage_arrival_date']
for tbl in table_list:
cur.execute(check_zero_count.format(tbl))
result = cur.fetchone()
if result[0] > 0:
continue
else:
error(f'Zero row counts in {tbl}')
conn.close()
sys.exit()
# Quality Check - Unique Key Check
key_table_list = [['admission_id || arrival_date', 'stage_visitor_analysis']
,['admission_id || arrival_date', 'stage_visitors']
,['arrival_date', 'stage_arrival_date']]
for list in key_table_list:
cur.execute(check_unique_key.format(list[0], list[0], list[1]))
result = cur.fetchone()
if result[0] == 0:
continue
else:
error(f"Duplicate values in key, {list[0]}, in table {list[1]}")
conn.close()
sys.exit()
# Call INSERT function
try:
insert_into(cur, conn)
except Exception as e:
error(f'Error inserting into table {e}')
conn.close()
sys.exit()
# Close connection
conn.close()
"""
- Run above code if the file is labled __main__
- Python internally labels files at runtime to differentiate between imported files and main file
"""
if __name__ == "__main__":
main()