Skip to content

Commit 5059366

Browse files
committed
minor tweks
1 parent 045e7f6 commit 5059366

13 files changed

Lines changed: 38 additions & 159 deletions

File tree

Pipfile

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,7 @@ verify_ssl = true
99
SQLAlchemy = "*"
1010
PyBigQuery = "*"
1111
PyMySQL = "*"
12-
Psycopg2-Binary = "*"
1312
Loguru = "*"
1413

1514
[requires]
16-
python_version = "3.7"
15+
python_version = "3.8"

Pipfile.lock

Lines changed: 2 additions & 40 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

README.md

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
11
# BigQuery SQLAlchemy Tutorial
22

3-
![Python](https://img.shields.io/badge/Python-v3.7-blue.svg?logo=python&longCache=true&logoColor=white&colorB=5e81ac&style=flat-square&colorA=4c566a)
4-
![Google Cloud BigQuery](https://img.shields.io/badge/Google--Cloud--BigQuery-v1.11.2-blue.svg?logo=Google&longCache=true&logoColor=white&colorB=5e81ac&style=flat-square&colorA=4c566a)
5-
![PyBigQuery](https://img.shields.io/badge/PyBigQuery-v0.4.11-blue.svg?logo=Google&longCache=true&logoColor=white&colorB=5e81ac&style=flat-square&colorA=4c566a)
6-
![SQLAlchemy](https://img.shields.io/badge/SQLAlchemy-v1.3.1-red.svg?longCache=true&style=flat-square&logo=scala&logoColor=white&colorA=4c566a&colorB=bf616a)
3+
![Python](https://img.shields.io/badge/Python-v^3.7-blue.svg?logo=python&longCache=true&logoColor=white&colorB=5e81ac&style=flat-square&colorA=4c566a)
4+
![Google Cloud BigQuery](https://img.shields.io/badge/Google--Cloud--BigQuery-v1.24.0-blue.svg?logo=Google&longCache=true&logoColor=white&colorB=5e81ac&style=flat-square&colorA=4c566a)
5+
![PyBigQuery](https://img.shields.io/badge/PyBigQuery-v0.4.13-blue.svg?logo=Google&longCache=true&logoColor=white&colorB=5e81ac&style=flat-square&colorA=4c566a)
6+
![SQLAlchemy](https://img.shields.io/badge/SQLAlchemy-v1.3.13-red.svg?longCache=true&style=flat-square&logo=scala&logoColor=white&colorA=4c566a&colorB=bf616a)
77
![PyMySQL](https://img.shields.io/badge/PyMySQL-v0.9.3-red.svg?longCache=true&style=flat-square&logo=mysql&logoColor=white&colorA=4c566a&colorB=bf616a)
8-
![Psycopg2-Binary](https://img.shields.io/badge/Psycopg2--Binary-v2.8.4-red.svg?longCache=true&style=flat-square&logo=postgresql&logoColor=white&colorA=4c566a&colorB=bf616a)
98
![GitHub Last Commit](https://img.shields.io/github/last-commit/google/skia.svg?style=flat-square&colorA=4c566a&colorB=a3be8c)
109
[![GitHub Issues](https://img.shields.io/github/issues/hackersandslackers/bigquery-sqlalchemy-tutorial.svg?style=flat-square&colorA=4c566a&colorB=ebcb8b)](https://github.com/hackersandslackers/bigquery-python-tutorial/issues)
1110
[![GitHub Stars](https://img.shields.io/github/stars/hackersandslackers/bigquery-sqlalchemy-tutorial.svg?style=flat-square&colorB=ebcb8b&colorA=4c566a)](https://github.com/hackersandslackers/bigquery-python-tutorial/stargazers)

biquery_sql_etl/__init__.py

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,27 @@
1+
from loguru import logger
12
from biquery_sql_etl.engines import bigquery_engine, rdbms_engine
23
from biquery_sql_etl.queries import sql_queries
34
from biquery_sql_etl.client import DataClient
4-
from loguru import logger
55
from config import bigquery_table
66

77

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

1010

11-
def main():
12-
"""Move data between sources."""
13-
bqc = DataClient(bigquery_engine)
14-
dbc = DataClient(rdbms_engine)
11+
def init_pipeline():
12+
"""Move data between Bigquery and MySQL."""
13+
total_rows_affected = 0
14+
bqc, dbc = data_sources()
1515
for table_name, query in sql_queries.items():
1616
fetched_rows = bqc.fetch_rows(query, table=bigquery_table)
1717
inserted_rows = dbc.insert_rows(fetched_rows, table_name, replace=True)
1818
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."""
25+
bqc = DataClient(bigquery_engine)
26+
dbc = DataClient(rdbms_engine)
27+
return bqc, dbc

biquery_sql_etl/client.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
"""Base Data Client."""
1+
"""Generic Client for interacting with data sources."""
22
from sqlalchemy import MetaData, Table
33

44

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
"""SQL Database Engine."""
2-
1+
"""BigQuery Engine."""
32
from sqlalchemy.engine import create_engine
43
from config import (gcp_credentials,
54
bigquery_uri)
65

76

8-
bigquery_engine = create_engine(bigquery_uri, credentials_path=gcp_credentials)
7+
bigquery_engine = create_engine(bigquery_uri,
8+
credentials_path=gcp_credentials)

biquery_sql_etl/queries.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66

77
def get_local_sql_files():
8-
"""Fetch all SQL query files in folder."""
8+
"""Fetch all .sql files from local folder."""
99
files = [local_sql_folder + '/' + f for f in listdir(local_sql_folder) if isfile(join(local_sql_folder, f)) if '.sql' in f]
1010
return files
1111

config.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,5 +15,5 @@
1515
rdbms_name = environ.get('DATABASE_NAME')
1616
rdbms_uri = f'mysql+pymysql://{rdbms_user}:{rdbms_pass}@{rdbms_host}:{rdbms_port}/{rdbms_name}'
1717

18-
# Local
18+
# Locally stored queries
1919
local_sql_folder = 'sql'

main.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
"""Application entry point."""
2-
from biquery_sql_etl import main
2+
from biquery_sql_etl import init_pipeline
33

44
if __name__ == '__main__':
5-
main()
5+
init_pipeline()

poetry.lock

Lines changed: 2 additions & 90 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)