-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #201 from BritishGeologicalSurvey/update-docs-code…
…-demos Update docs code demos
- Loading branch information
Showing
44 changed files
with
658 additions
and
550 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
"""ETL Helper script to demonstrate DbParams.""" | ||
import etlhelper as etl | ||
|
||
ORACLEDB = etl.DbParams( | ||
dbtype="ORACLE", | ||
host="localhost", | ||
port=1521, | ||
dbname="mydata", | ||
user="oracle_user", | ||
) | ||
|
||
POSTGRESDB = etl.DbParams( | ||
dbtype="PG", | ||
host="localhost", | ||
port=5432, | ||
dbname="mydata", | ||
user="postgres_user", | ||
) | ||
|
||
SQLITEDB = etl.DbParams( | ||
dbtype="SQLITE", | ||
filename="/path/to/file.db", | ||
) | ||
|
||
MSSQLDB = etl.DbParams( | ||
dbtype="MSSQL", | ||
host="localhost", | ||
port=1433, | ||
dbname="mydata", | ||
user="mssql_user", | ||
odbc_driver="ODBC Driver 17 for SQL Server", | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
import os | ||
os.environ["ORACLE_PASSWORD"] = "some-secret-password" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
"""ETL Helper script to demonstrate oracle handling Large Objects (LOBs).""" | ||
import etlhelper as etl | ||
import oracledb | ||
from my_databases import ORACLEDB | ||
|
||
select_sql = "SELECT my_clob, my_blob FROM my_table" | ||
|
||
with ORACLEDB.connect("ORA_PASSWORD") as conn: | ||
# By default, ETL Helper returns native types | ||
result_as_native = etl.fetchall(select_sql, conn) | ||
|
||
# Update oracledb settings to return LOBs | ||
oracledb.defaults.fetch_lobs = True | ||
result_as_lobs = etl.fetchall(select_sql, conn) |
3 changes: 3 additions & 0 deletions
3
docs/code_demos/connecting_to_databases/standalone_connect.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
import etlhelper as etl | ||
from my_databases import ORACLEDB | ||
oracle_conn = etl.connect(ORACLEDB, "ORACLE_PASSWORD") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
"""ETL Helper script to demonstrate copying records between databases with iter_rows and load.""" | ||
import etlhelper as etl | ||
from my_databases import POSTGRESDB, ORACLEDB | ||
|
||
select_sql = """ | ||
SELECT id, name, value FROM my_table | ||
WHERE value > :min_value | ||
""" | ||
|
||
with ORACLEDB.connect("ORA_PASSWORD") as src_conn: | ||
with POSTGRESDB.connect("PG_PASSWORD") as dest_conn: | ||
rows = etl.iter_rows(select_sql, src_conn, parameters={"min_value": 99}) | ||
etl.load("my_table", dest_conn, rows) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
"""ETL Helper script to demonstrate copy_rows.""" | ||
import etlhelper as etl | ||
from etlhelper.row_factories import namedtuple_row_factory | ||
from my_databases import POSTGRESDB, ORACLEDB | ||
|
||
select_sql = """ | ||
SELECT | ||
customer_id, | ||
SUM (amount) AS total_amount | ||
FROM payment | ||
WHERE id > 1000 | ||
GROUP BY customer_id | ||
""" | ||
|
||
# This insert query uses positional parameters, so a namedtuple_row_factory | ||
# is used. | ||
insert_sql = """ | ||
INSERT INTO dest ( | ||
customer_id, | ||
total_amount, | ||
loaded_by, | ||
load_time | ||
) | ||
VALUES ( | ||
%s, | ||
%s, | ||
current_user, | ||
now() | ||
) | ||
""" | ||
|
||
with ORACLEDB.connect("ORA_PASSWORD") as src_conn: | ||
with POSTGRESDB.connect("PG_PASSWORD") as dest_conn: | ||
etl.copy_rows( | ||
select_sql, | ||
src_conn, | ||
insert_sql, | ||
dest_conn, | ||
row_factory=namedtuple_row_factory, | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
"""ETL Helper script to demonstrate copy_table_rows.""" | ||
import etlhelper as etl | ||
from my_databases import POSTGRESDB, ORACLEDB | ||
|
||
with ORACLEDB.connect("ORA_PASSWORD") as src_conn: | ||
with POSTGRESDB.connect("PG_PASSWORD") as dest_conn: | ||
etl.copy_table_rows("my_table", src_conn, dest_conn) |
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
"""ETL Helper script to demonstrate logging errors.""" | ||
import logging | ||
import sqlite3 | ||
import etlhelper as etl | ||
|
||
etl.log_to_console() | ||
logger = logging.getLogger("etlhelper") | ||
|
||
db_file = "igneous_rocks.db" | ||
|
||
rows = [ | ||
{"name": "basalt", "grain_size": "fine"}, | ||
{"name": "granite", "grain_size": "coarse"} | ||
] | ||
|
||
|
||
def log_errors(failed_rows: list[tuple[dict, Exception]]) -> None: | ||
for row, exception in failed_rows: | ||
logger.error(exception) | ||
|
||
|
||
with sqlite3.connect(db_file) as conn: | ||
etl.load("igneous_rock", conn, rows, on_error=log_errors) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
"""ETL Helper script to demonstrate using fetch functions.""" | ||
import sqlite3 | ||
import etlhelper as etl | ||
|
||
db_file = "igneous_rocks.db" | ||
|
||
with sqlite3.connect(db_file) as conn: | ||
first_row = etl.fetchone("SELECT * FROM igneous_rock", conn) | ||
all_rows = etl.fetchall("SELECT * FROM igneous_rock", conn) | ||
|
||
print(first_row) | ||
print(all_rows) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
"""ETL Helper script to demonstrate iter_rows.""" | ||
import sqlite3 | ||
import etlhelper as etl | ||
|
||
db_file = "igneous_rocks.db" | ||
|
||
with sqlite3.connect(db_file) as conn: | ||
for row in etl.iter_rows("SELECT * FROM igneous_rock", conn): | ||
print(row) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
"""ETL Helper script to demonstrate using fetch functions with a given row factory.""" | ||
import sqlite3 | ||
import etlhelper as etl | ||
from etlhelper.row_factories import namedtuple_row_factory | ||
|
||
db_file = "igneous_rocks.db" | ||
|
||
with sqlite3.connect(db_file) as conn: | ||
row = etl.fetchone( | ||
"SELECT * FROM igneous_rock", | ||
conn, | ||
row_factory=namedtuple_row_factory, | ||
) | ||
|
||
print(row) | ||
print(row.name) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
"""ETL Helper script to demonstrate using executemany with a named placeholder query.""" | ||
import sqlite3 | ||
import etlhelper as etl | ||
|
||
db_file = "igneous_rocks.db" | ||
|
||
# Insert query changes case and adds update_at column | ||
insert_sql = """ | ||
INSERT INTO igneous_rocks (name, grain_size, updated_at) | ||
VALUES (:name, UPPER(:grainsize), datetime('now')) | ||
""" | ||
|
||
rows = [ | ||
{"name": "basalt", "grain_size": "fine"}, | ||
{"name": "granite", "grain_size": "coarse"} | ||
] | ||
|
||
with sqlite3.connect(db_file) as conn: | ||
# Note that table must already exist | ||
processed, failed = etl.executemany(insert_sql, conn, rows) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
"""ETL Helper script to demonstrate using executemany with a positional placeholder query.""" | ||
import sqlite3 | ||
import etlhelper as etl | ||
|
||
db_file = "igneous_rocks.db" | ||
|
||
# Positional placeholders for data in tuple format | ||
insert_sql = """ | ||
INSERT INTO igneous_rocks (name, grain_size, updated_at) | ||
VALUES (?, UPPER(?), datetime('now')) | ||
""" | ||
|
||
rows = [("basalt", "fine"), ("granite", "coarse")] | ||
|
||
with sqlite3.connect(db_file) as conn: | ||
# Note that table must already exist | ||
processed, failed = etl.executemany(insert_sql, conn, rows) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
"""ETL Helper script to demonstrate load.""" | ||
import sqlite3 | ||
import etlhelper as etl | ||
|
||
db_file = "igneous_rocks.db" | ||
|
||
rows = [ | ||
{"name": "basalt", "grain_size": "fine"}, | ||
{"name": "granite", "grain_size": "coarse"} | ||
] | ||
|
||
with sqlite3.connect(db_file) as conn: | ||
# Note that table must already exist | ||
processed, failed = etl.load("igneous_rock", conn, rows) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
"""ETL Helper script to demonstrate using Apache Airflow to schedule tasks.""" | ||
import datetime as dt | ||
from airflow import DAG | ||
from airflow.operators.python_operator import PythonOperator | ||
from database_to_database import copy_readings | ||
|
||
|
||
def copy_readings_with_args(**kwargs) -> None: | ||
# Set arguments for copy_readings from context | ||
start = kwargs.get("prev_execution_date") | ||
end = kwargs.get("execution_date") | ||
copy_readings(start, end) | ||
|
||
|
||
dag = DAG( | ||
"readings", | ||
schedule_interval=dt.timedelta(days=1), | ||
start_date=dt.datetime(2019, 8, 1), | ||
catchup=True, | ||
) | ||
|
||
t1 = PythonOperator( | ||
task_id="copy_readings", | ||
python_callable=copy_readings_with_args, | ||
provide_context=True, | ||
dag=dag, | ||
) |
Oops, something went wrong.