Read a table
This recipe reads a Unity Catalog table using the Databricks SQL Connector.
Code snippet
app.py
from functools import lru_cache
from databricks import sql
from databricks.sdk.core import Config
cfg = Config() # Set the DATABRICKS_HOST environment variable when running locally
@lru_cache(maxsize=1)
def get_connection(http_path):
return sql.connect(
server_hostname=cfg.host,
http_path=http_path,
credentials_provider=lambda: cfg.authenticate,
)
def read_table(table_name, conn):
with conn.cursor() as cursor:
query = f"SELECT * FROM {table_name}"
cursor.execute(query)
return cursor.fetchall_arrow().to_pandas()
http_path_input = "/sql/1.0/warehouses/xxxxxx"
table_name = "catalog.schema.table"
conn = get_connection(http_path_input)
df = read_table(table_name, conn)
info
This sample uses Pythons's lru_cache. Adapt the caching behavior to fit your specific use case.
Resources
Permissions
Your app service principal needs the following permissions:
SELECT
on the Unity Catalog tableCAN USE
on the SQL warehouse
See Unity Catalog privileges and securable objects for more information.
Dependencies
- Databricks SDK -
databricks-sdk
- Databricks SQL Connector -
databricks-sql-connector
- Dash -
dash
requirements.txt
databricks-sdk
databricks-sql-connector
dash