Skip to main content

Python Guide: Loading Data from SQL Database to Databricks using dlt Library

Need help deploying these pipelines, or figuring out how to run them in your data stack?

Join our Slack community or book a call with our support engineer Adrian.

Welcome to our technical documentation. Here, you will learn how to load data from sql_database, a structured data management system, into databricks, an innovative data analytics platform. We will be using dlt, an open-source Python library.

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. Type pip 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 to dlt 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 databricks:

pip install "dlt[databricks]"

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 databricks. You can run the following commands to create a starting point for loading data from sql_database to databricks:

# 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 databricks
# 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[databricks]>=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.databricks.credentials]
catalog = "catalog" # please set me up!
server_hostname = "server_hostname" # please set me up!
http_path = "http_path" # please set me up!
access_token = "access_token" # please set me up!
Settings up your integrations

Please consult the detailed setup instructions for the databricks 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 databricks 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:

Pipeline output to destination

After running the initial version pipeline, you can expect the following tables to be created in your destination:

Table NameDescription
familyThis 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.

Additional pipeline guides

This demo works on codespaces. Codespaces is a development environment available for free to anyone with a Github account. You'll be asked to fork the demo repository and from there the README guides you with further steps.
The demo uses the Continue VSCode extension.

Off to codespaces!

DHelp

Ask a question

Welcome to "Codex Central", your next-gen help center, driven by OpenAI's GPT-4 model. It's more than just a forum or a FAQ hub – it's a dynamic knowledge base where coders can find AI-assisted solutions to their pressing problems. With GPT-4's powerful comprehension and predictive abilities, Codex Central provides instantaneous issue resolution, insightful debugging, and personalized guidance. Get your code running smoothly with the unparalleled support at Codex Central - coding help reimagined with AI prowess.