Skip to content

Commit bd93827

Browse files
committed
reafactor
1 parent b03390c commit bd93827

File tree

14 files changed

+81
-121
lines changed

14 files changed

+81
-121
lines changed

README.md

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@ $ pipenv update
2727
$ python3 main.py
2828
```
2929

30-
<<<<<<< HEAD
3130
Installation via [Poetry](https://python-poetry.org/):
3231

3332
```shell
@@ -37,8 +36,6 @@ $ poetry install
3736
$ poetry run
3837
```
3938

40-
=======
41-
>>>>>>> 5ec1fda5600620aa22b8d53ad87226fe689b5151
4239
Alternatively, try installing via `setup.py`:
4340

4441
```shell

biquery_sql_etl/__init__.py

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,18 @@
1-
from biquery_sql_etl.sources.bigquery import bqc
2-
from biquery_sql_etl.sources.sqldatabase import dbc
1+
from biquery_sql_etl.engines import bigquery_engine, rdbms_engine
32
from biquery_sql_etl.queries import sql_queries
3+
from biquery_sql_etl.client import DataClient
44
from loguru import logger
5+
from config import bigquery_table
56

67

78
logger.add('logs/queries.log', format="{time} {message}", level="INFO")
89

910

1011
def main():
1112
"""Move data between sources."""
12-
for k, v in sql_queries.items():
13-
fetched_rows = bqc.fetch_rows(v)
14-
inserted_rows = dbc.insert_rows(fetched_rows, k, replace=True)
13+
bqc = DataClient(bigquery_engine)
14+
dbc = DataClient(rdbms_engine)
15+
for table_name, query in sql_queries.items():
16+
fetched_rows = bqc.fetch_rows(query, table=bigquery_table)
17+
inserted_rows = dbc.insert_rows(fetched_rows, table_name, replace=True)
1518
logger.info(inserted_rows)

biquery_sql_etl/client.py

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
"""Base Data Client."""
2+
from sqlalchemy import MetaData, Table
3+
4+
5+
class DataClient:
6+
7+
def __init__(self, engine):
8+
self.engine = engine
9+
self.metadata = MetaData(bind=self.engine)
10+
self.table_name = None
11+
12+
@property
13+
def table(self):
14+
if self.table_name:
15+
return Table(self.table_name, self.metadata, autoload=True)
16+
return None
17+
18+
def insert_rows(self, rows, table=None, replace=None):
19+
"""Insert rows into table."""
20+
if replace:
21+
self.engine.execute(f'TRUNCATE TABLE {table}')
22+
self.table_name = table
23+
self.engine.execute(self.table.insert(), rows)
24+
return self.construct_response(rows, table)
25+
26+
def fetch_rows(self, query, table=None):
27+
"""Fetch all rows via query."""
28+
rows = self.engine.execute(query).fetchall()
29+
return rows
30+
31+
@staticmethod
32+
def construct_response(rows, table):
33+
"""Summarize results of an executed query."""
34+
columns = rows[0].keys()
35+
column_names = ", ".join(columns)
36+
num_rows = len(rows)
37+
return f'Inserted {num_rows} rows into `{table}` with {len(columns)} columns: {column_names}'
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
from .bigquery import bigquery_engine
2+
from .rdbms import rdbms_engine
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
from sqlalchemy.engine import create_engine
2+
from config import (gcp_credentials,
3+
bigquery_uri)
4+
5+
6+
def create_bigquery_engine(uri, creds):
7+
engine = create_engine(uri, credentials_path=creds)
8+
return engine
9+
10+
11+
bigquery_engine = create_bigquery_engine(bigquery_uri, gcp_credentials)

biquery_sql_etl/engines/rdbms.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
"""SQL Database Client."""
2+
from sqlalchemy.engine import create_engine
3+
from config import rdbms_uri
4+
5+
6+
def create_rdbms_engine(uri):
7+
return create_engine(uri)
8+
9+
10+
rdbms_engine = create_rdbms_engine(rdbms_uri)

biquery_sql_etl/queries.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,12 @@ def read_sql_queries():
2222
return file_contents
2323

2424

25+
def get_table_name(file_names):
26+
"""Assume that table names of queries are [FILE_NAME]_stats."""
27+
tables = [f"{file.split('.')[0]}_stats" for file in file_names]
28+
return tables
29+
30+
2531
def create_query_dict():
2632
"""Create dictionary of queries for logging purposes."""
2733
file_names = listdir(local_sql_folder)
@@ -31,10 +37,4 @@ def create_query_dict():
3137
return files
3238

3339

34-
def get_table_name(file_names):
35-
"""Assume that table names of queries are [FILE_NAME]_stats."""
36-
tables = [f"{file.split('.')[0]}_stats" for file in file_names]
37-
return tables
38-
39-
4040
sql_queries = create_query_dict()

biquery_sql_etl/sources/__init__.py

Lines changed: 0 additions & 1 deletion
This file was deleted.

biquery_sql_etl/sources/bigquery/__init__.py

Lines changed: 0 additions & 10 deletions
This file was deleted.

biquery_sql_etl/sources/bigquery/bigquery_client.py

Lines changed: 0 additions & 14 deletions
This file was deleted.

0 commit comments

Comments
 (0)