Sub topic 17
Defining jobs as code with the Jobs API and Asset Bundles
This chapter assumes you can build a multi-task job in the Jobs UI. You should know task keys, depends_on, job clusters, and Quartz cron schedules from earlier chapters. You should also be comfortable with the command line and basic YAML and JSON.
Why define jobs as code
A job built by clicking in the UI is hard to review, copy between workspaces, or restore after someone edits it by accident. A job defined as text can be stored in Git, reviewed in a pull request, and deployed the same way to development and production.
Databricks gives you two main routes to this:
| Approach | What you write | Best for |
|---|---|---|
| Jobs API 2.1 | A JSON job definition sent over HTTPS | Scripts, other tools, one-off automation |
| Databricks Asset Bundles | YAML files deployed with the Databricks CLI | Teams that want versioned projects with several environments |
Both routes produce the same kind of job. The bundle CLI calls the Jobs API for you, so the YAML fields mirror the JSON fields.
Creating a job with the Jobs API
The settings you used in the UI map to JSON fields. The three-task pipeline from earlier chapters looks like this when saved as job.json. The node_type_id shown is AWS-specific and differs on other clouds.
{
"name": "daily-sales-pipeline",
"max_concurrent_runs": 1,
"schedule": {
"quartz_cron_expression": "0 0 6 * * ?",
"timezone_id": "UTC",
"pause_status": "UNPAUSED"
},
"job_clusters": [
{
"job_cluster_key": "etl_cluster",
"new_cluster": {
"spark_version": "15.4.x-scala2.12",
"node_type_id": "i3.xlarge",
"num_workers": 2
}
}
],
"tasks": [
{
"task_key": "ingest",
"job_cluster_key": "etl_cluster",
"notebook_task": {"notebook_path": "/Workspace/Shared/ingest"},
"max_retries": 2,
"min_retry_interval_millis": 60000
},
{
"task_key": "transform",
"depends_on": [{"task_key": "ingest"}],
"job_cluster_key": "etl_cluster",
"notebook_task": {"notebook_path": "/Workspace/Shared/transform"}
},
{
"task_key": "publish",
"depends_on": [{"task_key": "transform"}],
"job_cluster_key": "etl_cluster",
"notebook_task": {"notebook_path": "/Workspace/Shared/publish"}
}
]
}Send it with curl. Set DATABRICKS_HOST to your workspace URL, for example https://myworkspace.cloud.databricks.com, and DATABRICKS_TOKEN to a personal access token or other valid token.
curl -X POST "$DATABRICKS_HOST/api/2.1/jobs/create" \
-H "Authorization: Bearer $DATABRICKS_TOKEN" \
-H "Content-Type: application/json" \
-d @job.jsonThe response contains the new job's ID, such as {"job_id": 123456789}. Start a run of that job with the run-now endpoint:
curl -X POST "$DATABRICKS_HOST/api/2.1/jobs/run-now" \
-H "Authorization: Bearer $DATABRICKS_TOKEN" \
-H "Content-Type: application/json" \
-d '{"job_id": 123456789}'Create, reset, and update
Running create twice gives you two jobs with the same name, because job names are not unique. To change an existing job, use one of two endpoints:
POST /api/2.1/jobs/resetreplaces all settings of a job with the settings you send. Anything you leave out is removed.POST /api/2.1/jobs/updatechanges only the fields innew_settings. You can delete fields by listing them infields_to_remove.
A script that must be safe to run repeatedly has to track the job_id itself, for example by looking the job up by name with jobs/list first. This bookkeeping is the main reason teams move to bundles.
Defining a job with Asset Bundles
A bundle is a project folder with a databricks.yml file at the root. It declares the bundle name, the resources to deploy, and one or more targets such as dev and prod. The CLI tracks which workspace resources belong to which bundle and target, so you can deploy again and get an update instead of a duplicate.
A typical layout:
daily_sales/
databricks.yml
resources/
daily_sales_job.yml
src/
ingest.ipynb
transform.ipynbThe root file defines targets and variables:
bundle:
name: daily_sales
include:
- resources/*.yml
variables:
node_type:
description: Worker node type for the job cluster
default: i3.xlarge
targets:
dev:
mode: development
default: true
workspace:
host: https://myworkspace.cloud.databricks.com
prod:
mode: production
workspace:
host: https://myworkspace.cloud.databricks.comThe job itself goes in resources/daily_sales_job.yml. It repeats the earlier example, with a job cluster added so the file is complete:
resources:
jobs:
daily_sales:
name: daily-sales-pipeline
schedule:
quartz_cron_expression: "0 0 6 * * ?"
timezone_id: UTC
email_notifications:
on_failure:
- [email protected]
job_clusters:
- job_cluster_key: etl_cluster
new_cluster:
spark_version: 15.4.x-scala2.12
node_type_id: ${var.node_type}
num_workers: 2
tasks:
- task_key: ingest
job_cluster_key: etl_cluster
notebook_task:
notebook_path: ../src/ingest.ipynb
- task_key: transform
depends_on:
- task_key: ingest
job_cluster_key: etl_cluster
notebook_task:
notebook_path: ../src/transform.ipynbNotebook paths are relative to the YAML file that mentions them. The CLI uploads the notebooks and rewrites the paths to their workspace location.
The bundle workflow
After authenticating the CLI, you use four commands in a loop:
databricks bundle validate -t dev
databricks bundle deploy -t dev
databricks bundle run daily_sales -t dev
databricks bundle destroy -t devvalidatechecks the configuration and reports schema errors before anything is deployed.deployuploads files and creates or updates the job.runstarts the job whose resource key isdaily_sales. This is the YAML key, not the display name.destroydeletes everything the bundle deployed to that target.
What targets change
mode: development is meant for personal testing. It prefixes resource names with [dev your_name] and pauses schedules, so your test copy does not compete with production. mode: production turns on stricter validation checks. You can also override any setting per target, such as a smaller cluster in dev, using a resources block under the target.
Gotchas
- Edits in the UI are overwritten. The next
bundle deployreplaces the job's settings with the YAML. Treat the repository as the source of truth. - Renaming the resource key creates a new job. The CLI identifies a job by its key and target. Changing
daily_salesto another key deploys a new job, and the old one remains until you remove it. resetis destructive. A partial JSON body sent toresetsilently drops the omitted settings. Useupdatefor partial changes.- Schedules in
devare paused. If a development job never runs on its cron, check the mode before debugging the expression. - Cloud-specific values.
node_type_iddiffers on AWS, Azure, and GCP. Keep it in a variable so a target can override it.
Exercise
A teammate edits the schedule of a bundle-deployed job in the Jobs UI to run at 07:00. The next day your CI pipeline runs databricks bundle deploy -t prod. What time does the job run afterwards, and what is the right way to make 07:00 permanent?
Show answer
After the deploy, the job runs at the time in the YAML (06:00 UTC), because the deploy overwrites the settings made in the UI. To make 07:00 permanent, change quartz_cron_expression in the bundle's YAML file (for example to "0 0 7 ?"), review and merge the change in Git, and deploy again.
Exercise
You need to change only max_concurrent_runs on an existing job through the Jobs API without touching the tasks. Which endpoint do you call, and why not reset?
Show answer
Call POST /api/2.1/jobs/update with the jobid and `newsettings containing maxconcurrentruns. The reset` endpoint replaces all settings, so any setting missing from the request body, such as the tasks, would be removed.