Sub topic 7
Parameterizing jobs and tasks
A job that always processes the same hard-coded date or table is hard to reuse. Parameters let one job definition handle a backfill, a test run, and the normal daily run. This chapter covers job parameters, task parameters, dynamic value references, and how each task type receives them. It assumes you can write a notebook that reads widgets, you know the task and dependency model from earlier chapters, and you can read JSON job definitions.
Two levels of parameters
Databricks Jobs has two places to set inputs.
- Job parameters belong to the whole job. They are key-value pairs with a default value. Every task in the job can read them, and you can override them each time you start a run.
- Task parameters belong to one task. Their shape depends on the task type: base parameters for a notebook task, a list of arguments for a Python script task, and so on.
Job parameters suit values that many tasks share, such as a run date, an environment name, or a catalog name. Task parameters suit values that only one task needs, such as a batch size.
All parameter values are strings. A task that needs a number, a date, or a boolean must convert the string itself.
Defining job parameters
Job parameters go in the parameters array of the job definition. This fragment adds two parameters to the job from the earlier chapters.
{
"name": "daily-sales-pipeline",
"parameters": [
{"name": "run_date", "default": "2024-01-01"},
{"name": "env", "default": "dev"}
],
"tasks": [
{
"task_key": "ingest",
"notebook_task": {
"notebook_path": "/Workspace/Shared/ingest",
"base_parameters": {
"batch_size": "500",
"run_date": "{{job.parameters.run_date}}"
}
}
}
]
}The default applies whenever a run starts without an override. A scheduled run always uses the defaults, so set defaults that make sense for the scheduled case. Use an empty or sentinel default only if the notebook handles it.
Dynamic value references
The string {{job.parameters.run_date}} is a dynamic value reference. Databricks replaces it with the real value when the task starts. References work inside task parameter values, and you can use them to pass job parameters into a task explicitly.
| Reference | Resolves to |
|---|---|
| `{{job.parameters.<name>}}` | The value of a job parameter for this run |
| `{{job.id}}` | The numeric ID of the job |
| `{{job.run_id}}` | The ID of the current job run |
| `{{task.run_id}}` | The ID of the current task run |
| `{{job.start_time.iso_date}}` | The date the run started, in ISO format |
| `{{tasks.<task_key>.values.<key>}}` | A task value set by an upstream task (see the task values chapter) |
A reference to a name that does not exist is not silently empty. Treat a typo in a reference as a likely cause of a failed run or of a literal {{...}} string reaching your code.
Reading parameters in a notebook
A notebook task receives parameters as widget values. Define the widget with a default so the notebook also runs interactively.
dbutils.widgets.text("run_date", "2024-01-01")
dbutils.widgets.text("env", "dev")
dbutils.widgets.text("batch_size", "500")
run_date = dbutils.widgets.get("run_date")
env = dbutils.widgets.get("env")
batch_size = int(dbutils.widgets.get("batch_size"))
print(f"Running for {run_date} in {env} with batches of {batch_size}")In a job run, the value passed by the job replaces the widget default. The int(...) call is needed because dbutils.widgets.get always returns a string.
Reading parameters in a Python script task
A Python script task receives its parameters as command-line arguments. Pass them as a list in the task definition.
{
"task_key": "transform",
"spark_python_task": {
"python_file": "/Workspace/Shared/transform.py",
"parameters": ["--run-date", "{{job.parameters.run_date}}", "--env", "{{job.parameters.env}}"]
}
}Read them with argparse.
import argparse
parser = argparse.ArgumentParser()
parser.add_argument("--run-date", required=True)
parser.add_argument("--env", default="dev")
args = parser.parse_args()
print(f"Transforming {args.run_date} in {args.env}")SQL tasks take named parameters that you reference in the query, and wheel tasks take either positional or keyword arguments. In every case, the dynamic value reference is the same, so you only learn the reference syntax once.
Which value wins
When a job parameter and a task parameter share a key, the job parameter takes precedence for notebook tasks. A run-time override beats the job's saved default.
This order explains a common surprise. You set base_parameters on a task, but the value in the run does not match. A job parameter with the same key is overriding it. Use distinct names for task-only values, or remove the duplicate.
Overriding parameters for one run
Override job parameters when you start a run by hand or from automation. With the Jobs API, send job_parameters to run-now.
{
"job_id": 123456789,
"job_parameters": {
"run_date": "2024-06-01",
"env": "prod"
}
}With the Databricks CLI, pass the same body.
databricks jobs run-now 123456789 \
--json '{"job_parameters": {"run_date": "2024-06-01", "env": "prod"}}'The override applies to this run only. The saved defaults do not change. This is the normal way to backfill a past date without editing the job.
Common errors
- A missing widget. The notebook calls
dbutils.widgets.get("run_date")without defining the widget first, and the value was not passed. Define every widget at the top with a default. - Type errors. The code compares
batch_sizeto an integer, but the value is the string"500". Convert once, right after reading. - A literal
{{...}}in output. The reference has a typo or sits in a field that does not support references. Print the received value at the start of the task to check. - A hidden override. A task parameter and a job parameter share a name. See the precedence order above.
- Secrets in parameters. Parameter values show in the run page and the API. Pass a secret scope and key name instead, and fetch the secret in the task with
dbutils.secrets.get.
Parameters and repairs
When you repair a failed run, the rerun uses the job parameters of the original run unless you change them in the repair dialog or request. Successful tasks keep their earlier results. Tasks that rerun see the same run_date as before, so a repair does not silently shift to today's date, as long as the date came from a parameter and not from current_date() inside the code.
That last point is a design rule. Derive dates from a parameter and not from the system clock, so that a retry, a repair, and a backfill all produce reproducible results.
Exercise
A job defines the job parameter run_date with default 2024-01-01. The ingest notebook task also sets base_parameters with run_date equal to 2023-12-31. A scheduled run starts. Which value does the notebook's run_date widget have, and what happens if you start a manual run with job_parameters of {"run_date": "2024-06-01"}?
Show answer
For the scheduled run the widget has 2024-01-01, because the job parameter takes precedence over a task base parameter with the same key and the scheduled run uses the saved default. For the manual run the widget has 2024-06-01, because the run-time override beats the saved default. In both cases the value is a string.