-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathetl_utils.py
More file actions
96 lines (68 loc) · 2.24 KB
/
Copy pathetl_utils.py
File metadata and controls
96 lines (68 loc) · 2.24 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
import time
import psycopg2
import configparser
"""
Support module for both `create_tables.py` and `etl.py`.
Currently contains the create_connection() and run_query()
functions used throughout both those scripts.
"""
def create_connection(config_file):
"""
Takes the path to `.cfg` file as an argument, and uses that
information to create a connection to a Redshift (Postgres)
database. Returns the connection and the cursor so other functions
can make use of them.
Paramters:
config_file - path to a `.cfg` file that contains connection
details for the Postgres database.
Returns:
cur (psycopg2.cursor()) - cursor of the (Postgres) db
conn (psycopg2.connect()) - connection to the (Postgres) db
"""
config = configparser.ConfigParser()
config.read(config_file)
try:
connection = psycopg2.connect("""
host={}
dbname={}
user={}
password={}
port={}
""".format(*config['CLUSTER'].values()))
except psycopg2.Error as error:
print("Error: Could not make connection to the Postgres database.")
print(error)
try:
cursor = connection.cursor()
except psycopg2.Error as error:
print("Error: Could not get cursor.")
print(error)
return(cursor, connection)
def run_query(cursor, connection, query):
"""
Uses the Postgres connection and cursor passed in to
run the query that has been passed in as well. Also
prints the query to STDOUT and prints the time taken
for the query to execute as well.
To check execution time, we currently use time.time()
but we may want to implement timeit.timeit() at a later
date.
Paramters:
cur (psycopg2.cursor()) - cursor of the (Postgres) db
conn (psycopg2.connect()) - connection to the (Postgres) db
Returns:
None
"""
time_start = time.time()
cursor.execute(query)
connection.commit()
print("Query:", end=" ")
print(query)
try:
rows = cursor.fetchall()
print("Result:", end=" ")
for row in rows:
print(row)
except psycopg2.Error as _:
print("Query did not return any results.")
print("Execution time:", time.time() - time_start, '\n')