I created a simple python script to change the cluster_id
within the .databricks-connect
configuration file.
To execute, ensure your virtual env has environment variable DATABRICKS_CLUSTER configured. Obtaining the cluster ID is shown here in the official databricks-connect
documentation.
Set the environment variable with:
export DATABRICKS_CLUSTER=your-cluster-id
Once the environment variable is set, simply use the following python script to switch cluster whenever your new virtual environment is activated.
import os
import json
#Get databricks cluster associated with current virtual env
DATABRICKS_CLUSTER = os.getenv('DATABRICKS_CLUSTER')
HOME = os.getenv('HOME')
#Open the databricks-connect config file
with open(f'{HOME}/.databricks-connect', 'r') as j:
config = json.loads(j.read())
#Update new cluster ID
config['cluster_id'] = DATABRICKS_CLUSTER
#Save the databricks connect config file
with open(f'{HOME}/.databricks-connect', 'w') as outfile:
json.dump(config, outfile, indent=4)