@@ -13,6 +13,9 @@ def spark(ws: WorkspaceClient):
1313
1414 To enable serverless set the local environment variable `DATABRICKS_SERVERLESS_COMPUTE_ID` to `"auto"`.
1515 If this environment variable is set, Databricks Connect ignores the cluster_id.
16+ If `DATABRICKS_SERVERLESS_COMPUTE_ID` is set to a specific serverless cluster ID, that cluster will be used instead.
17+ However, this is not recommended, as serverless clusters are ephemeral by design.
18+ See more details here: https://docs.databricks.com/en/dev-tools/databricks-connect/cluster-config.html#configure-a-connection-to-serverless-compute
1619
1720 Usage:
1821 ```python
@@ -21,21 +24,32 @@ def test_databricks_connect(spark):
2124 assert rows[0][0] == 1
2225 ```
2326 """
24- if not ws .config .serverless_compute_id :
25- if not ws .config .cluster_id :
26- skip ("No cluster_id found in the environment" )
27- ws .clusters .ensure_cluster_is_running (ws .config .cluster_id )
27+ cluster_id = ws .config .cluster_id
28+ serverless_cluster_id = ws .config .serverless_compute_id
29+ print (f"cluster id is: { serverless_cluster_id } " )
30+
31+ if not serverless_cluster_id :
32+ ensure_cluster_is_running (cluster_id , ws )
33+
34+ if serverless_cluster_id and serverless_cluster_id != "auto" :
35+ ensure_cluster_is_running (serverless_cluster_id , ws )
2836
2937 try :
3038 # pylint: disable-next=import-outside-toplevel
3139 from databricks .connect import ( # type: ignore[import-untyped]
3240 DatabricksSession ,
3341 )
3442
35- if ws . config . serverless_compute_id :
43+ if serverless_cluster_id :
3644 logging .debug ("Using serverless compute" )
3745 return DatabricksSession .builder .serverless (True ).getOrCreate ()
3846 return DatabricksSession .builder .sdkConfig (ws .config ).getOrCreate ()
3947 except ImportError :
4048 skip ("Please run `pip install databricks-connect`" )
4149 return None
50+
51+
52+ def ensure_cluster_is_running (cluster_id , ws ):
53+ if not cluster_id :
54+ skip ("No cluster_id found in the environment" )
55+ ws .clusters .ensure_cluster_is_running (cluster_id )
0 commit comments