Databricks DE ยท Topic 11.3

Databricks Connect

Run DataFrame/Spark code from your local IDE against remote Databricks compute โ€” IDE-native development & debugging, with data and heavy compute staying in Databricks.

local IDE โ†’ remote computeSpark Connect (gRPC)DBR 13.3 LTS+dev tool

The one-liner

Databricks Connect = code in VS Code/PyCharm locally, but DataFrame transformations run on a remote Databricks cluster/serverless (via the Spark Connect gRPC protocol). You get IDE tooling (debuggers, tests, git) without moving data to your laptop.
๐ŸŽฎ Analogy: a remote control for a powerful machine โ€” you hold the remote (laptop/IDE) and press buttons (DataFrame code); the actual work runs on the big machine in the data center (Databricks), and results come back.

What runs where โ€” click to trace a query

โ–ธ general code = local ยท DataFrame ops = remote

๐Ÿ–ฅ๏ธ Local (your IDE)

general Python/Scala/R
edit + debugbuild the DataFrame plan

โ˜๏ธ Remote (Databricks)

cluster / serverless + your data
Spark plan executesUC data read
โ†‘ DataFrame plan sent over Spark Connect โ†“ results returned

How it works โ€” deep dive

1 ยท The Spark Connect architecture (v2)

Mechanism: modern Databricks Connect (DBR 13.0+, the databricks-connect PyPI package) is a thin client speaking Spark Connect gRPC โ€” DataFrame ops serialize to a plan run on the remote driver. Trade-off: the v2 package is a clean break from the legacy pre-DBR-13 one โ€” don't mix them up.

# The modern client: pip install "databricks-connect"
# Builds DataFrame plans locally, runs them remotely over Spark Connect (gRPC).
from databricks.connect import DatabricksSession   # the v2 entry point

2 ยท Install & version-match to the cluster

Mechanism: install a databricks-connect version that matches the cluster's DBR (available DBR 13.3 LTS+), in an isolated venv. Trade-off: each target DBR may need its own pinned client โ€” standardize one DBR per environment.

# Isolated env; pin the client to the cluster's DBR (e.g. 15.4 LTS).
python -m venv .venv && source .venv/bin/activate
pip install --upgrade "databricks-connect==15.4.*"   # match the cluster DBR
# โš ๏ธ verify โ€” confirm the exact version against your cluster's runtime.

3 ยท Build a DatabricksSession

Mechanism: DatabricksSession.builder replaces SparkSession.builder. getOrCreate() reuses/auto-configs; .serverless() / .clusterId(...) / .remote("sc://...") target compute explicitly. Trade-off: serverless sessions expire after 10 min idle (client auto-closes).

from databricks.connect import DatabricksSession

# A) default config (profile/env) โ€” simplest for local dev:
spark = DatabricksSession.builder.getOrCreate()

# B) target serverless or a specific cluster explicitly:
spark = DatabricksSession.builder.serverless().getOrCreate()
# spark = DatabricksSession.builder.clusterId("0712-...-abcd").getOrCreate()

4 ยท Authentication โ€” profiles & config

Mechanism: Connect uses standard Databricks SDK auth โ€” a .databrickscfg profile (host + token/OAuth) or env vars are picked up by getOrCreate(); .remote(...) takes an explicit Spark Connect string. Trade-off: an inline token is handy once but must never be committed โ€” prefer a profile/OAuth.

# Explicit Spark Connect connection string (verified form):
spark = DatabricksSession.builder.remote(
    f"sc://{workspace_host}:443/;token={token};x-databricks-cluster-id={cluster_id}"
).getOrCreate()
# Prefer a .databrickscfg profile / OAuth so no token lives in code.

5 ยท What runs locally vs remotely

Mechanism: general Python (loops, app logic, tests) runs locally; any DataFrame/SQL op becomes a plan executed on the remote cluster against governed UC data. UDFs serialize from the client and run on the cluster. Trade-off: UDF deps must exist on the cluster, not just locally.

# local control flow + remote Spark execution in one script:
for region in ["West", "East"]:                 # runs LOCALLY
    df = spark.read.table("prod.sales.orders")  # executes on the CLUSTER
    total = df.filter(df.region == region).agg({"amount": "sum"})  # remote plan
    print(region, total.collect()[0][0])        # result returned to the IDE

6 ยท Local testing & debugging โ€” and the limits

Mechanism: it's a normal Python process โ€” set breakpoints, run pytest, lint/type-check against real remote data, then ship the same code via DABs. Trade-off: not every notebook-only feature maps 1:1 (some dbutils/display); scheduled prod still runs as Jobs/pipelines.

# A pytest unit test exercising a transform against the remote session:
def test_revenue_by_region(spark):                # spark = DatabricksSession fixture
    out = revenue_by_region(spark.read.table("prod.sales.orders"))
    assert "region" in out.columns and out.count() > 0

Databricks Connect vs notebooks โ€” click each

Databricks Connect โ€” develop in your IDE.
  • Debuggers, linters, unit tests, native git
  • Compute & data remain in Databricks
  • Great for app development & testable code
Notebooks โ€” develop in the workspace.
  • Fast interactive exploration, built-in viz
  • All in-platform, no local setup
  • Great for ad-hoc analysis & collaboration

Why it matters

๐Ÿง‘โ€๐Ÿ’ป Real-world use case: build a transform in VS Code with breakpoints + pytest against a dev cluster via Databricks Connect; once green, commit (Git folders) and deploy (DABs) โ€” never hand-pasted into a notebook.

Uses, edge cases & limitations

Common gotchas