cancel
Showing results forĀ 
Search instead forĀ 
Did you mean:Ā 
Data Engineering
Join discussions on data engineering best practices, architectures, and optimization strategies within the Databricks Community. Exchange insights and solutions with fellow data engineers.
cancel
Showing results forĀ 
Search instead forĀ 
Did you mean:Ā 

Is there a way to programatically retrieve a workspace name ?

AmarK
Databricks Employee
Databricks Employee

Is there a spark command in databricks that will tell me what databricks workspace I am using? I’d like to parameterise my code so that I can update delta lake file paths automatically depending on the workspace (i.e. it picks up the dev workspace name when in dev and will pick up the prod workspace name in prod). Is this possible?

5 REPLIES 5

Navya_R
Databricks Employee
Databricks Employee

You may run the below in a notebook within the workspace:

dbutils.entry_point.getDbutils().notebook().getContext().toJson()

The output contains: 

browserHostName - is the workspace name

orgId - is the workspaceID

AmarK
Databricks Employee
Databricks Employee

Screenshot 2022-02-04 at 08.54.25Thanks Navya! But this doesn't work for me on a High Concurrency cluster. It seems that toJson() isn't whitelisted.

mehdi_lamrani
New Contributor II
from databricks.sdk import WorkspaceClient
workspace_client = WorkspaceClient()
workspace_hostname = workspace_client.config.host

How does it even make sense? Did you even see how old the post is? It's confusing! What made you reply to this post, wondering!

WiliamRosa
New Contributor II

To programmatically retrieve the Databricks workspace name from within a notebook, you can use Spark configuration or the notebook context. One method is to read the workspace URL using spark.conf.get("spark.databricks.workspaceUrl") and then extract the instance name—for example, by splitting at the first period. Alternatively, you can access the notebook context with dbutils.notebook.entry_point.getDbutils().notebook().getContext().browserHostName().toString() (or via the equivalent JSON tags), which provides the hostname corresponding to the workspace. Both approaches give you a way to dynamically capture and use the workspace identifier in your code.

Wiliam Rosa
Data Engineer | Machine Learning Engineer
LinkedIn: linkedin.com/in/wiliamrosa

Join Us as a Local Community Builder!

Passionate about hosting events and connecting people? Help us grow a vibrant local community—sign up today to get started!

Sign Up Now