Sub topic 12
Turning notebooks into scheduled jobs
This chapter assumes you can run a notebook on compute and query a Unity Catalog table by its three-level name. It covers how a notebook becomes a repeatable, scheduled job.
From interactive to automated
A notebook you run by hand is useful for exploration. Once the work needs to run every night, or after another step finishes, you move it into a job. A job is a named unit of automation that runs one or more tasks on a trigger. Each execution of a job is called a run.
The parts of a job
- Task: one step, such as running a notebook, a Python script, or a SQL query.
- Dependencies: a task can wait for other tasks, so the tasks form a directed acyclic graph (a flow with no loops).
- Compute: each task runs on job compute, serverless compute, or a SQL warehouse. Job compute is created when the run starts and ends when it finishes, so you pay for it only while the run lasts. This is why scheduled work usually avoids all-purpose compute, which is billed at a higher rate and is meant for interactive use.
- Trigger: a cron-style schedule, a file arrival, a table update, or a manual start.
- Parameters: values passed into a task so one notebook can serve many runs.
Preparing a notebook for a job
Parameters reach a notebook through widgets. A widget is an input field defined in code. When a job supplies a value, it overrides the widget's default.
from pyspark.sql import functions as F
dbutils.widgets.text("min_amount", "0")
min_amount = float(dbutils.widgets.get("min_amount"))
big_orders = (
spark.table("main.tutorial.orders")
.filter(F.col("amount") >= min_amount)
)
big_orders.write.mode("overwrite").saveAsTable("main.tutorial.big_orders")This notebook reads a threshold, filters the orders table, and writes the result as a new Delta table. Writing with overwrite makes the task safe to retry, because a second run replaces the first result instead of duplicating it.
Creating the job
- Open Jobs & Pipelines in the workspace sidebar and create a job.
- Add a task, choose the notebook type, and select the notebook path.
- Choose compute for the task.
- Add parameters, for example
min_amountwith the value20. - Add a schedule, then set notifications for failures.
A job runs as an identity, either a user or a service principal. That identity needs USE CATALOG, USE SCHEMA, and SELECT on the tables it reads, and permission to write where it saves results. A common failure is a job that works in your notebook but fails on schedule because the job identity lacks those privileges.
Common errors
| Symptom | Likely cause |
|---|---|
| Table or schema not found | Job identity lacks `USE CATALOG` or `USE SCHEMA` |
| Run duplicates rows | Task appends without a safe retry strategy |
| Widget value ignored | Parameter name differs from the widget name |
| Slow start on every run | Job compute must start fresh. Consider serverless |
Exercise
A nightly job reads main.tutorial.orders and works when you run the notebook by hand, but the scheduled run fails with a permission error. What do you check first, and why does the notebook run differ from the job run?
Show answer
Check which identity the job runs as and whether it has USE CATALOG on main, USE SCHEMA on main.tutorial, and SELECT on the table. Your manual run uses your own privileges, while the scheduled run uses the job's identity, which may have fewer.