Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

📝 Sql query tutorial #642

Merged
merged 2 commits into from
Dec 9, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion docs/source/tutorial/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ In this tutorial we will cover (not necessarily in this order):
extract/index.rst
testing.rst
extract/challenge_scenarios/index.rst
study_creator.rst
sourcing_data/index.rst
transform.rst
target_service_plugins/index.rst
load.rst
9 changes: 9 additions & 0 deletions docs/source/tutorial/sourcing_data/index.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
=================================
Sourcing Data
=================================

.. toctree::
:maxdepth: 2

study_creator.rst
query_sql
73 changes: 73 additions & 0 deletions docs/source/tutorial/sourcing_data/query_sql.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
======================================================
Source data stored in a PostgreSQL Database
======================================================

Scenario
========

You would like to extract data directly from a PostgreSQL Database.

nicholasvk marked this conversation as resolved.
Show resolved Hide resolved
.. note::
Before deciding that you'd like to query a SQL database directly, instead,
you may want to consider separately querying your database, saving the
result of that query to a file, and then using that file as source for an
extract config.

By querying a database directly

* It's no longer easy to audit the data used as the source for an
ingest.
* It's much harder, if not impossible, for non-technical users to know
what data is used for an ingest
* Using a database is inherently volatile e.g. rows may be added/
deleted/ updated.

Because of the above points, it's important to consider if your needs
wouldn't be better served by using a static result of your query as
source data for an ingest instead of directly querying the SQL database.


.sql File Solution
======================

A convenient way to load data from a postgres database is by saving your SQL
query in a file, loading that file as source data, then connecting to the
database and running your query inside of source_data_read_function.

``source_data_read_func`` will be used to parse the data from the
``source_data_url``.

As an example, say you save your query in the file at
``my-ingest-package/data/my_query.sql``:

.. code-block:: sql

SELECT *
FROM participant p
WHERE p.study_id = 'SD_ME0WME0W'

Your extract config to pull data from your database could then look like:

.. code-block:: Python

import pandas as pd
import psycopg2

source_data_url = "file://my-ingest-package/data/my_query.sql

db_url = "postgresql://username:[email protected]:5432/postgres"

source_data_read_params = {"connection_url": db_url}


def source_data_read_func(filepath, connection_url, **kwargs):
with open(filepath, "r") as f:
query = f.read().replace("\n", " ")
f.close
conn = psycopg2.connect(connection_url)
df = pd.read_sql(query, conn)
return df

The resulting dataframe would then be the resulting data that comes from your
query. You can then continue on writing your extract config as you normally
would.