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:ย 

Retrieve job-level parameters in Python

John_Rotenstein
New Contributor II

Parameters can be passed to Tasks and the values can be retrieved with:

dbutils.widgets.get("parameter_name")

More recently, we have been given the ability to add parameters to Jobs.

However, the parameters cannot be retrieved like Task parameters.

Question: How can we retrieve job-level parameters in notebook code?

 

10 REPLIES 10

cbern
New Contributor III

@Retired_mod This method works for Task parameters. Is there a way to access Job parameters that apply to the entire workflow, set under a heading like this in the UI:

cbern_0-1714069648952.png

I am able to read Job parameters in a different way from Task parameters using  dynamic value references:

{{tasks.[task_name].values.[value_name]}}

vs.

{{job.parameters.[name]}} 

This works for reading Parameters in the Workflow itself, such as:

cbern_1-1714070664308.png

Is there an analogous way to read a Job parameter within a Notebook? The Note on this page seems to indicate that these dynamic value references are available in Notebooks, but how do you reference them in Python?

cbern
New Contributor III

A coworker has answered this question for me, posting it for anyone else looking for an answer:

run_parameters = dbutils.notebook.entry_point.getCurrentBindings() 
context = json.loads(dbutils.notebook.entry_point.getDbutils().notebook().getContext().toJson())
job_param = run_parameters[key]

cbern
New Contributor III

 

an update to my answer: Databricks has advised us that the `dbutils.notebook.entry_point` method is not supported (could be deprecated), and the recommended way to read in a job parameter is through widgets, i.e. `dbutils.widgets.get("param_key")` (similar to Task parameters -- if you have a Task param and Job param with the same name, the Job param value takes precedence). 

If you want to read a dynamic value reference like the run_id inside a Notebook, you can set the reference (e.g. `{{job.run_id}}`) a Task or Job param, and read it like a widget.

xiangzhu
Contributor III

I think `dbutils.widgets.get` is for notebooks only, not for python job as asked by this thread

xiangzhu
Contributor III

ah sorry, the thread asked for notebooks too.

nevertheless, I'm search for getting job params in pure python jobs

You need to push down your parameters to a task level. Eg:

  1. Create a job level parameter called "my_param"
  2. Make a reference to his job parameter in the task level parameters box. Eg:
    ["--my_param","{{job.parameters.my_param}}"]
  3. Read the task level parameter using argparser in your .py file

Thanks !

How do I read the task level parameter using argparser?

@DineshOjha 

The task-level parameter is not very useful, in my opinion, because it is hardcoded and not a real parameter. In such cases, I often use a config.py file to define all task level parameters directly within Python as a configuration.

However, job-level parameters are really useful since we can change their values dynamically when manually triggering a job run.

I'm not aware if Databricks' dbutils provides a built-in method to directly return the current job parameters, but we can work around this by querying the Jobs API.

At the very beginning of the task's Python code:

  1. Use dbutils to get the current job run ID.
  2. Use the Jobs API to retrieve the current job info using the job run ID. (whether from raw api call, or from the databricks_cli sdk, not the new go lib, but the old python one)
  3. Extract the job_parameters from the returned job info.
  4. Save all the job_parameters as environment variables.

From anywhere in your code, you can now access the current job parameters from the environment variables without using argparse.

For steps 1โ€“4, you could write a function to encapsulate the entire process.

lprevost
Contributor

The only thing that has worked for me consistently in python is 

params = dbutils.widgets.getAll() where an empty dictionary is returned if I'm in interactive mode and the job/task params are returned if they are present.

Connect with Databricks Users in Your Area

Join a Regional User Group to connect with local Databricks users. Events will be happening in your city, and you wonโ€™t want to miss the chance to attend and share knowledge.

If there isnโ€™t a group near you, start one and help create a community that brings people together.

Request a New Group