-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdatabase_utils.py
46 lines (31 loc) · 1.84 KB
/
database_utils.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
import yaml, sqlalchemy
import pandas as pd
class DatabaseConnector:
def read_db_creds (self):
stream = open('db_creds.yaml', 'r')
credentials = yaml.safe_load(stream)
return credentials
def init_db_engine(self):
dictCredentials = self.read_db_creds()
connectionString = "postgresql+psycopg2://" + dictCredentials["RDS_USER"] + ":" + dictCredentials["RDS_PASSWORD"]
connectionString += "@" + dictCredentials["RDS_HOST"] + ":" + str(dictCredentials["RDS_PORT"]) + "/" + str(dictCredentials["RDS_DATABASE"])
engine = sqlalchemy.create_engine(connectionString)
return engine
def list_db_tables (self, engine: sqlalchemy.engine.Engine):
with engine.connect() as connection:
result = connection.execute(sqlalchemy.text("SELECT * FROM pg_catalog.pg_tables WHERE schemaname != 'pg_catalog' AND schemaname != 'information_schema';"))
table_names = list()
for row in result:
table_names.append(row[1])
return table_names
def read_rds_table(self,engine,table_name):
frame = pd.read_sql_table(table_name,con=engine.connect())
return frame
def upload_to_db(self,df: pd.DataFrame,table_name: str):
dictCredentials = self.read_db_creds()
connectionString = "postgresql+psycopg2://" + str(dictCredentials["LOCAL_USER"]) + ":" + str(dictCredentials["LOCAL_PASSWORD"])
connectionString += "@localhost:" + str(dictCredentials["LOCAL_PORT"]) + "/" + str(dictCredentials["LOCAL_DATABASE"])
engine = sqlalchemy.create_engine(connectionString)
# with engine.connect() as connection:
created = df.to_sql(table_name,con=engine,if_exists="replace",index=False)
# print("Created %s lines." % created)