Sub topic 6
Running code against a workspace with Databricks Connect
This chapter assumes you can write pytest tests, know PySpark DataFrames, and have read the previous chapter on the shared spark fixture. It shows how to run the same code against a real workspace from your laptop or from CI.
What Databricks Connect does
Databricks Connect is a client library that lets local Python code use Databricks compute. It is built on Spark Connect. Your code builds DataFrame operations locally. The client sends them as a logical plan to the workspace, where a cluster or serverless compute runs them. Only the results come back.
Your Python process, including pytest, runs locally. The Spark work runs remotely, next to real tables and Unity Catalog permissions.
Installing it in its own environment
databricks-connect ships its own build of PySpark. Installing it next to the pyspark package breaks both. Keep two environments: one for local unit tests with pyspark, and one for integration tests with databricks-connect.
python -m venv .venv-connect
source .venv-connect/bin/activate
pip install "databricks-connect==15.4.*" pytestChoose a version that matches your compute. For clusters, the client version must not be newer than the cluster's Databricks Runtime, and the local Python minor version must match the one the runtime uses. Serverless compute has its own compatibility rules, so check the documentation for your client version.
Authenticating and choosing compute
Databricks Connect uses the standard Databricks unified authentication. It reads a configuration profile or environment variables, so the same code works on your laptop and in CI.
export DATABRICKS_HOST=https://adb-1111111111111111.1.azuredatabricks.net
export DATABRICKS_CLIENT_ID=<service-principal-application-id>
export DATABRICKS_CLIENT_SECRET=<secret>
export DATABRICKS_CLUSTER_ID=<cluster-id>
export USE_DATABRICKS_CONNECT=1DATABRICKS_CLUSTER_ID selects an existing cluster. To use serverless compute instead, call DatabricksSession.builder.serverless(True).getOrCreate() in the fixture. The fixture from the previous chapter already switches on USE_DATABRICKS_CONNECT, so tests need no changes to run in either mode.
Writing an integration test
An integration test uses real tables instead of hand-built rows. Mark these tests so you can run them separately from the fast unit tests.
import pytest
from pyspark.sql import functions as F
from my_project.transforms import add_order_total
@pytest.mark.integration
def test_add_order_total_on_sample_table(spark):
source = spark.read.table("staging.orders.sample_orders").limit(100)
result = add_order_total(source)
assert "total" in result.columns
assert result.filter(F.col("total").isNull()).count() == 0Register the marker in pytest.ini to avoid warnings, then select tests by marker:
pytest -m integrationCommon errors
- Both packages installed. Import errors or odd attribute errors usually mean
pysparkanddatabricks-connectshare one environment. Recreate the environment. - Version mismatch. A client newer than the runtime fails at session creation. Pin the client to the runtime version.
- Authentication fails. Check that the service principal has access to the compute and to the catalog and schema you read.
- Unsupported APIs. Spark Connect does not support everything classic PySpark does. RDD operations and some
SparkContextcalls fail. Stay with the DataFrame API. - Slow tests. Each action makes a network round trip. Limit input rows and avoid calling
count()orcollect()in loops.
Exercise
Your integration tests fail with import errors right after you ran pip install databricks-connect in the same virtual environment you use for unit tests. What is the likely cause, and how do you fix it?
Show answer
databricks-connect bundles its own PySpark, which conflicts with the separately installed pyspark package. Create a second environment for Databricks Connect (for example .venv-connect), install databricks-connect there with a version matching the cluster's Databricks Runtime, and keep pyspark only in the unit-test environment.