SQLAlchemy Dialect for Databricks

0.2.0 · active · verified Mon Apr 13

SQLAlchemy-Databricks is a SQLAlchemy dialect for connecting to Databricks. It enables Python applications to interact with Databricks SQL Endpoints and Unity Catalog using the familiar SQLAlchemy ORM or Core API. The current version is 0.2.0, supporting Python 3.8 and above. The release cadence appears to be infrequent, with two major releases to date.

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to establish a connection to a Databricks SQL Endpoint using `sqlalchemy-databricks`. It uses environment variables for sensitive connection details and executes a simple `SELECT 1` query to verify connectivity. Ensure your Databricks SQL Endpoint is running and your token has the necessary permissions.

from sqlalchemy import create_engine, text
import os

# Databricks connection details
# These are typically found in the Databricks SQL Endpoint connection details.
# server_hostname is like 'dbc-xxxxxxxx-yyyy.cloud.databricks.com'
# http_path is like '/sql/1.0/endpoints/zzzzzzzzzzzzzzzz'
# token is a Databricks Personal Access Token or Azure AD Token

DATABRICKS_SERVER_HOSTNAME = os.environ.get('DATABRICKS_SERVER_HOSTNAME', 'your_databricks_hostname')
DATABRICKS_HTTP_PATH = os.environ.get('DATABRICKS_HTTP_PATH', 'your_http_path')
DATABRICKS_TOKEN = os.environ.get('DATABRICKS_TOKEN', 'your_databricks_token')

# Construct the connection string
connection_string = f"databricks://token:{DATABRICKS_TOKEN}@{DATABRICKS_SERVER_HOSTNAME}/{DATABRICKS_HTTP_PATH}"

# Create the engine
try:
    engine = create_engine(connection_string)

    # Establish a connection and execute a simple query
    with engine.connect() as connection:
        result = connection.execute(text("SELECT 1")) # Simple test query
        print("Connection successful! Query result:", result.scalar())

    print("\nSuccessfully connected to Databricks and executed a query.")

except Exception as e:
    print(f"Error connecting to Databricks: {e}")
    print("Please ensure DATABRICKS_SERVER_HOSTNAME, DATABRICKS_HTTP_PATH, and DATABRICKS_TOKEN environment variables are set correctly.")

view raw JSON →