Sub topic 12
Defining compute with the Jobs and Clusters APIs
This chapter assumes you can read JSON, know the difference between all-purpose, job, and serverless compute, and have a workspace with a personal access token or other API authentication. Defining compute as JSON lets you review it, version it in Git, and recreate it the same way each time.
Two APIs, two lifecycles
The Clusters API manages all-purpose clusters. You create one, and it exists until you terminate or delete it. The Jobs API manages scheduled work. A job can declare its own compute, which exists only for the duration of a run.
| Goal | API | Compute lifecycle |
|---|---|---|
| Shared interactive cluster | Clusters API | Persists, restarts on demand |
| Scheduled pipeline on its own cluster | Jobs API with `job_clusters` | Created at run start, terminated at run end |
| Scheduled pipeline with no cluster to size | Jobs API with no cluster settings | Serverless, managed by Databricks |
An all-purpose cluster
Send this body to POST /api/2.1/clusters/create.
{
"cluster_name": "team-interactive",
"spark_version": "15.4.x-scala2.12",
"node_type_id": "i3.xlarge",
"autoscale": {
"min_workers": 1,
"max_workers": 4
},
"autotermination_minutes": 60,
"data_security_mode": "USER_ISOLATION"
}Each field maps to a concept from earlier chapters:
spark_versionselects the Databricks Runtime.node_type_idselects the virtual machine type.i3.xlargeis an AWS example. Azure and GCP use different names.autoscalelets the cluster run between 1 and 4 workers depending on load. Usenum_workersinstead for a fixed size.autotermination_minutesshuts the cluster down after 60 idle minutes, which limits idle spend.data_security_modeset toUSER_ISOLATIONis standard access mode, which works with Unity Catalog.
A job cluster
In the Jobs API, you declare compute once under job_clusters and point tasks at it with job_cluster_key.
{
"name": "nightly-etl",
"job_clusters": [
{
"job_cluster_key": "etl_cluster",
"new_cluster": {
"spark_version": "15.4.x-scala2.12",
"node_type_id": "i3.xlarge",
"num_workers": 2,
"data_security_mode": "USER_ISOLATION"
}
}
],
"tasks": [
{
"task_key": "ingest",
"job_cluster_key": "etl_cluster",
"notebook_task": {
"notebook_path": "/Workspace/Users/[email protected]/ingest"
}
}
]
}The cluster is created when the run starts and terminated when it finishes. It has no autotermination_minutes, because the run's end is the termination. Several tasks can share one job_cluster_key, so they reuse a single cluster instead of each starting their own.
A serverless task
For serverless, leave out the cluster settings.
{
"name": "nightly-etl-serverless",
"tasks": [
{
"task_key": "ingest",
"notebook_task": {
"notebook_path": "/Workspace/Users/[email protected]/ingest"
}
}
]
}In a workspace where serverless compute for jobs is enabled, a notebook task that names no cluster runs on serverless compute. There is no node type, runtime version, or worker count to choose.
Common errors
- Setting both
autoscaleandnum_workers. Provide one of them. - Copying a node type across clouds.
i3.xlargeexists only on AWS. Pick a valid type for your cloud. - Leaving out
data_security_mode. The default may not give you the access mode you need for Unity Catalog. Set it explicitly. - Expecting serverless without the feature. If serverless for jobs is not enabled, a task with no cluster is not valid.
- Using a runtime that has been retired. Check which
spark_versionvalues your workspace supports.
Exercise
A job has three tasks that run one after another. You want them to share one cluster that is created at the start of the run and terminated at the end. Which fields do you use, and where?
Show answer
Declare one entry under job_clusters with a job_cluster_key and a new_cluster block. Then set the same job_cluster_key on each of the three tasks. The cluster starts with the run and terminates when the run finishes, so you don't set autotermination_minutes.
Exercise
You paste the all-purpose cluster body into a workspace on Azure and the request fails. What is the most likely cause, and what do you change?
Show answer
The node_type_id value i3.xlarge is an AWS instance type. Replace it with a node type that exists on Azure.