How to programmatically retrieve the workspace url and clusterOwnerUserId?

Viewed 9

I would like to programmatically create the url to download a file.

To do this I need the workspaceUrl and clusterOwnerUserId.

How can I retrieve those in a Databricks notebook?

# how to get the `workspaceUrl` and `clusterOwnerUserId`?

tmp_file = '/tmp/output_abcd.xlsx'
filestore_file = '/FileStore/output_abcd.xlsx'

# code to create file omitted for brevity ...

dbutils.fs.cp(f'file:{tmp_file}', filestore_file)
    
downloadUrl = f'https://{workspaceUrl}/files/output_abcd.xlsx?o={clusterOwnerUserId}'

displayHTML(f"<a href='{downloadUrl}'>download</a>")
1 Answers

The variables are available in the spark conf.

E.g.

clusterOwnerUserId = spark.conf.get('spark.databricks.clusterUsageTags.orgId')
workspaceUrl = spark.conf.get('spark.databricks.workspaceUrl')

Use can then use the details as follows:

tmp_file = '/tmp/output_abcd.xlsx'
filestore_file = '/FileStore/output_abcd.xlsx'

# code to create file omitted for brevity ...

dbutils.fs.cp(f'file:{tmp_file}', filestore_file)

downloadUrl = f'https://{workspaceUrl}/files/output_abcd.xlsx?o={clusterOwnerUserId}'

displayHTML(f"<a href='{downloadUrl}'>download</a>")
Related