Sub topic 8
Sharing values between tasks
This chapter assumes you can build a multi-task job with dependencies and that you know how job parameters work. Here you will learn how one task passes a small result to the tasks after it.
What a task value is
A task value is a small piece of data that one task publishes and later tasks in the same run can read. Use task values for facts about a run, such as a row count, a file path, or a status flag. Do not use them to move datasets. Write large data to a table and pass its name instead.
Task values live only for the current run. They are not job parameters. Parameters are set before the run starts, while task values are produced while it runs.
Setting and reading a value
The upstream notebook calls dbutils.jobs.taskValues.set. The downstream notebook calls dbutils.jobs.taskValues.get and names the task that set the value.
# In the notebook for task "ingest"
row_count = spark.table("sales.raw_orders").count()
dbutils.jobs.taskValues.set(key="row_count", value=row_count)
# In a notebook for a downstream task
row_count = dbutils.jobs.taskValues.get(
taskKey="ingest", key="row_count", default=0, debugValue=0
)
print(f"Ingest produced {row_count} rows")The get call takes these arguments:
| Argument | Meaning |
|---|---|
| `taskKey` | The `task_key` of the task that set the value. |
| `key` | The name given in `set`. |
| `default` | Returned if the task ran but never set that key. |
| `debugValue` | Returned when the notebook runs outside a job, such as interactively. |
Without debugValue, an interactive run of the downstream notebook raises an error, because no job run exists to read from.
Dependencies matter
A task can only read values from tasks that finish before it. In the job above, transform depends on ingest, so the read is safe. If the two tasks have no dependency path between them, the value may not exist when get runs. Draw the dependency first, then add the value.
Using values outside notebooks
You can also reference a value in a task's settings with a dynamic value reference. The form is {{tasks.<task_key>.values.<key>}}. This works in places such as task parameters and condition tasks, where no notebook code runs. The next chapters use it to branch on a value, for example {{tasks.ingest.values.row_count}}.
Common errors
- Wrong task key. The
taskKeymust match thetask_keyin the job definition, not the notebook name. - Missing dependency. The reading task does not depend on the setting task, so the value is not reliably available.
- Value too large. Task values have a size limit of a few kilobytes. Store large results in a table and pass the table name.
- Reading a skipped task. If the setting task failed or was skipped, the value was never set. Use
defaultso the notebook can handle that case. - Interactive run fails. Add
debugValueso you can test the notebook outside a job.
Exercise
Your transform notebook runs fine inside the job but raises an error when you run it by hand in the workspace. It calls dbutils.jobs.taskValues.get(taskKey="ingest", key="row_count"). What is the cause, and how do you fix it?
Show answer
Outside a job run there is no ingest task to read from, so get fails. Add a debugValue argument, for example debugValue=0, so the call returns that value in interactive runs. Adding default=0 also covers the case where the task ran but never set the key.