-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathetl.py
More file actions
57 lines (43 loc) · 1.49 KB
/
etl.py
File metadata and controls
57 lines (43 loc) · 1.49 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
import configparser
import psycopg2
from sql_queries import copy_table_queries, insert_table_queries, all_tables_count_rows
def load_staging_tables(cur, conn):
"""
Load data from S3 into staging tables (staging_events, staging_songs)
"""
for query in copy_table_queries:
cur.execute(query)
conn.commit()
def insert_tables(cur, conn):
"""
Transform data: extracting from staging tables into the dimensional tables
using INSERT queries declared in sql_queries
"""
for query in insert_table_queries:
cur.execute(query)
conn.commit()
def count_rows(cur, conn):
"""
Test whether all tables were populated with data successfully
"""
for query in all_tables_count_rows:
cur.execute(query)
res = cur.fetchone()
for rows in res:
print("Query %s returned %s " % (query, rows))
conn.commit()
def main():
config = configparser.ConfigParser()
config.read('dwh.cfg')
conn = psycopg2.connect("host={} dbname={} user={} password={} port={}".format(*config['DB'].values()))
cur = conn.cursor()
print("Issuing copy commands (it may take up to 15 minutes time to load the data from s3). Please wait ...")
load_staging_tables(cur, conn)
print("Inserting tables")
insert_tables(cur, conn)
print("Checking number of rows in tables")
count_rows(cur, conn)
conn.close()
print("Finished successfully")
if __name__ == "__main__":
main()