Databricks DE ยท Topic 11.3
Run DataFrame/Spark code from your local IDE against remote Databricks compute โ IDE-native development & debugging, with data and heavy compute staying in Databricks.
โธ general code = local ยท DataFrame ops = remote
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
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.
DatabricksSessionMechanism: 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()
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.
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
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 (13.x+).