Sub topic 17
Defining jobs as code with the Jobs API and Asset Bundles
This chapter assumes you can build a job in the Databricks UI, and that you know tasks, dependencies, and Quartz cron schedules from earlier chapters. You should also have basic JSON and YAML skills.
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. A job defined as text can live in Git, go through code review, and be deployed the same way to dev and prod. Databricks gives you two main routes: the Jobs API and Databricks Asset Bundles.
The Jobs API
The Jobs API 2.1 accepts a JSON job definition. The POST /api/2.1/jobs/create call creates a job, and jobs/reset replaces the settings of an existing one. The definition below holds everything covered in earlier chapters: a shared job cluster, a daily schedule, a retry, and a concurrency limit.
{
"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"}
}
]
}The node_type_id shown is AWS-specific. Use a node type valid for your cloud.
Databricks Asset Bundles
A Databricks Asset Bundle is a project folder with a databricks.yml file plus resource files, deployed with the Databricks CLI. It describes jobs in YAML and keeps source files next to them. Unlike raw API calls, a bundle tracks what it deployed, so renaming or removing a resource in the YAML updates the workspace on the next deploy.
resources:
jobs:
daily_sales:
name: daily-sales-pipeline
schedule:
quartz_cron_expression: "0 0 6 * * ?"
timezone_id: UTC
email_notifications:
on_failure:
- [email protected]
tasks:
- task_key: ingest
notebook_task:
notebook_path: ../src/ingest.ipynb
- task_key: transform
depends_on:
- task_key: ingest
notebook_task:
notebook_path: ../src/transform.ipynbThis example omits compute settings, which depend on your workspace. Add a job cluster or compute reference before you deploy it.
The usual workflow uses three CLI commands:
databricks bundle validatechecks the configuration.databricks bundle deploy -t devcreates or updates the job in thedevtarget.databricks bundle run daily_sales -t devstarts a run.
Common errors
- Editing a bundle-deployed job in the UI. The next deploy overwrites those changes.
- Forgetting the compute settings, which makes validation or deployment fail.
- Using
jobs/resetwith a partial definition. It replaces all settings, so send the full job. - Pasting a node type from the wrong cloud.
Exercise
You renamed a task key in your bundle YAML and ran databricks bundle deploy. Why is it safer to do this through a bundle than by calling jobs/reset with a hand-edited partial JSON?
Show answer
A bundle holds the complete job definition in one reviewed file and deploys the whole thing, so the workspace job always matches the file. jobs/reset replaces all settings, so a partial JSON would drop anything you left out, such as the schedule or retries.