Skip to content

Commit c2902e6

Browse files
committed
minor tweks
1 parent 5059366 commit c2902e6

File tree

3 files changed

+11
-15
lines changed

3 files changed

+11
-15
lines changed

biquery_sql_etl/__init__.py

+7-14
Original file line numberDiff line numberDiff line change
@@ -2,26 +2,19 @@
22
from biquery_sql_etl.engines import bigquery_engine, rdbms_engine
33
from biquery_sql_etl.queries import sql_queries
44
from biquery_sql_etl.client import DataClient
5-
from config import bigquery_table
65

76

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

109

1110
def init_pipeline():
1211
"""Move data between Bigquery and MySQL."""
13-
total_rows_affected = 0
14-
bqc, dbc = data_sources()
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)
18-
logger.info(inserted_rows)
19-
total_rows_affected += len(fetched_rows)
20-
logger.info(f"Completed migration of {total_rows_affected} rows from BigQuery to MySQL.")
21-
22-
23-
def data_sources():
24-
"""Construct datasources."""
12+
num_rows = 0
2513
bqc = DataClient(bigquery_engine)
2614
dbc = DataClient(rdbms_engine)
27-
return bqc, dbc
15+
for table_name, query in sql_queries.items():
16+
rows = bqc.fetch_rows(query)
17+
insert = dbc.insert_rows(rows, table_name, replace=True)
18+
logger.info(insert)
19+
num_rows += len(rows)
20+
logger.info(f"Completed migration of {num_rows} rows from BigQuery to MySQL.")

biquery_sql_etl/client.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ def insert_rows(self, rows, table=None, replace=None):
2323
self.engine.execute(self.table.insert(), rows)
2424
return self.construct_response(rows, table)
2525

26-
def fetch_rows(self, query, table=None):
26+
def fetch_rows(self, query):
2727
"""Fetch all rows via query."""
2828
rows = self.engine.execute(query).fetchall()
2929
return rows

logs/queries.log

+3
Original file line numberDiff line numberDiff line change
@@ -1 +1,4 @@
11
# QUERY HISTORY
2+
2020-02-11T07:29:26.499248-0500 Inserted 100 rows into `weekly_stats` with 4 columns: title, url, slug, views
3+
2020-02-11T07:29:28.552129-0500 Inserted 500 rows into `monthly_stats` with 4 columns: title, url, slug, views
4+
2020-02-11T07:29:28.552409-0500 Completed migration of 600 rows from BigQuery to MySQL.

0 commit comments

Comments
 (0)