Python Guide: Loading SQL Database Data to BigQuery with dlt Library
Join our Slack community or book a call with our support engineer Adrian.
This page provides technical documentation on how to load data from sql_database
to bigquery
using the open-source Python library, dlt
. Sql_database
is a Database Management System (DBMS) that structures data for effective retrieval, supporting over 30 databases via SQLAlchemy. On the other hand, bigquery
is an economical, serverless enterprise data warehouse that operates across multiple clouds and scales with your data.
dlt
Key Features
- Easy to get started:
dlt
is a Python library that is easy to use and understand. It is designed to be simple to use and easy to understand. Typepip install dlt
and you are ready to go. - User friendly interface:
dlt
is designed to be user friendly. It has a simple and intuitive declarative interface that makes it easy to use and understand. Low code but not no code. - Extensible & Open Source:
dlt
is designed to be extensible. You can add new sources and destinations todlt
by writing a few lines of Python code, or use our verified sources and extend them. - Community Support & Documentation:
dlt
is fully open source and has a growing community of users and contributors. We have a community slack and documentation to help you get started.
Getting started with your pipeline locally
1. Install dlt
First you need to install the dlt
library with the correct extras for bigquery
:
pip install "dlt[bigquery]"
The dlt
cli has a useful command to get you started with any combination of source and destination. You can run the following command to see all available sources:
dlt init --list
For this example, we want to load data from sql_database
to bigquery
. You can run the following commands to create a starting point for loading data from sql_database
to bigquery
:
# create a new directory
mkdir my-sql_database-pipeline
cd my-sql_database-pipeline
# initialize a new pipeline with your source and destination
dlt init sql_database bigquery
# install the required dependencies
pip install -r requirements.txt
The last command will install the required dependencies for your pipeline. The dependencies are listed in the requirements.txt
:
sqlalchemy>=1.4
dlt[bigquery]>=0.3.5
2. Configuring your source and destination credentials
The dlt
cli will have created a .dlt
directory in your project folder. This directory contains a config.toml
file and a secrets.toml
file that you can use to configure your pipeline:
config.toml
# put your configuration values here
[runtime]
log_level="WARNING" # the system log level of dlt
# use the dlthub_telemetry setting to enable/disable anonymous usage data reporting, see https://dlthub.com/docs/telemetry
dlthub_telemetry = true
secrets.toml
# put your secret values and credentials here. do not share this file and do not push it to github
[sources.sql_database.credentials]
drivername = "drivername" # please set me up!
database = "database" # please set me up!
password = "password" # please set me up!
username = "username" # please set me up!
host = "host" # please set me up!
port = 0 # please set me up!
[destination.bigquery]
location = "US"
[destination.bigquery.credentials]
project_id = "project_id" # please set me up!
private_key = "private_key" # please set me up!
client_email = "client_email" # please set me up!
Please consult the detailed setup instructions for the bigquery
destination in the dlt
destinations documentation.
Likewise you can find the setup instructions for sql_database
source in the dlt
verifed sources documentation.
3. Running your pipeline for the first time
The dlt
cli has also created a main pipeline script for you at sql_database_pipeline.py
, as well as a folder sql_database
that contains additional python files for
your source. These files are your local copies which you can modify to fit your needs. In some cases you may find that you only need to do small changes to your
pipelines or add some configurations, in other cases these files can serve as a working starting point for your code, but will need to be adjusted to do what
you need them to do.
After you have set up your credentials, you can run your pipeline like a regular python script with the following command:
python sql_database_pipeline.py
4. Inspecting your load result
You can now inspect the state of your pipeline with the dlt
cli:
dlt pipeline rfam_database info
You can also use streamlit to inspect the contents of your bigquery destination for this:
# install streamlit
pip install streamlit
# run the streamlit app for your pipeline with the dlt cli:
dlt pipeline rfam_database show
5. Next steps to get your pipeline running in production
One of the beauties of dlt
is, that we are just a plain Python library, so you can run your pipeline in any environment that supports Python >= 3.8.
We have a couple of helpers and guides in our docs to get you there:
The Deploy section will show you how to deploy your pipeline to
The running in production section will teach you about:
Pipeline output to destination
After running the initial version pipeline, you can expect the following tables to be created in your destination:
Table Name | Description |
---|---|
family | This table contains various information about a family. It has fields such as author, clen, cmbuild, description, number_of_species, previous_id, rfam_acc, and many others. It appears to be related to some kind of biological or genetic data, possibly related to genomics or bioinformatics. |