-
Notifications
You must be signed in to change notification settings - Fork 0
/
etl.py
45 lines (33 loc) · 1.2 KB
/
etl.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
import psycopg2
from configparser import ConfigParser, ExtendedInterpolation
from sql_queries import copy_table_queries, insert_table_queries
def load_staging_tables(cur, conn):
"""
execute data insertion jobs into fact tables as defined in predefined queries
"""
for query in copy_table_queries:
cur.execute(query)
conn.commit()
def insert_tables(cur, conn):
"""
execute data insertion jobs into dimension tables as defined in predefined queries
"""
for query in insert_table_queries:
cur.execute(query)
conn.commit()
def main():
parser = ConfigParser(interpolation=ExtendedInterpolation())
parser.read('dwh.cfg')
db_host = parser.get("cluster", "db_host")
db_name = parser.get("cluster", "db_name")
db_user = parser.get("cluster", "db_user")
db_password = parser.get("cluster", "db_password")
db_port = parser.get("cluster", "db_port")
db_info = f"host={db_host} dbname={db_name} user={db_user} password={db_password} port={db_port}"
conn = psycopg2.connect(db_info)
cur = conn.cursor()
load_staging_tables(cur, conn)
insert_tables(cur, conn)
conn.close()
if __name__ == "__main__":
main()