Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you get the run parameters and runId within Databricks notebook?

When running a Databricks notebook as a job, you can specify job or run parameters that can be used within the code of the notebook. However, it wasn't clear from documentation how you actually fetch them. I'd like to be able to get all the parameters as well as job id and run id.

like image 523
Scott H Avatar asked Jul 21 '20 16:07

Scott H


1 Answers

Job/run parameters

When the notebook is run as a job, then any job parameters can be fetched as a dictionary using the dbutils package that Databricks automatically provides and imports. Here's the code:

run_parameters = dbutils.notebook.entry_point.getCurrentBindings()

If the job parameters were {"foo": "bar"}, then the result of the code above gives you the dict {'foo': 'bar'}. Note that Databricks only allows job parameter mappings of str to str, so keys and values will always be strings.

Note that if the notebook is run interactively (not as a job), then the dict will be empty. The getCurrentBinding() method also appears to work for getting any active widget values for the notebook (when run interactively).

Getting the jobId and runId

To get the jobId and runId you can get a context json from dbutils that contains that information. (Adapted from databricks forum):

import json
context_str = dbutils.notebook.entry_point.getDbutils().notebook().getContext().toJson()
context = json.loads(context_str)
run_id_obj = context.get('currentRunId', {})
run_id = run_id_obj.get('id', None) if run_id_obj else None
job_id = context.get('tags', {}).get('jobId', None)

So within the context object, the path of keys for runId is currentRunId > id and the path of keys to jobId is tags > jobId.

like image 127
Scott H Avatar answered Sep 27 '22 19:09

Scott H