Sub topic 1
Why automate Databricks, and which tool to use
This chapter explains what you gain by driving Databricks from scripts, and it compares the four tools the rest of the tutorial covers. You will pick a tool by task, so later chapters can go straight into commands and code.
Prerequisites
This chapter assumes you already have the following:
- Basic command-line use: running commands in a shell, setting environment variables, and piping output between programs.
- Familiarity with the Databricks workspace, including what jobs and clusters are and how you create them in the web UI.
- A basic understanding of HTTP requests and JSON.
- Basic Python, which you need for the SDK examples in later chapters.
You do not need to install anything yet. The next chapter covers installing the Databricks CLI.
Why move from the UI to scripts
The web UI suits exploration. You click through a job, adjust a setting, and see the result. It becomes a poor fit once the same action has to happen many times, on a schedule, or in several places.
Automation addresses these problems:
- Repeatability. A script performs the same steps each time. A person clicking through a form can skip a step or pick the wrong option.
- Review and history. When a job definition lives in a file under version control, every change has an author, a date, and a diff. Edits made in the UI leave a weaker trail.
- Multiple environments. Promoting a job from development to production by hand means recreating it twice. With code, you deploy the same definition to two targets.
- Integration. Other systems can only start a Databricks job if there is a programmatic interface. Examples are an orchestrator, a CI pipeline, or an internal deployment tool.
- Scale. Updating 200 jobs through the UI is tedious and error prone. A loop over a list of job IDs is not.
Automation also has costs. You must manage credentials for non-interactive use, handle failures that no person is watching, and keep scripts in step with API changes. The later chapters on authentication, error handling, and credential security address these costs directly.
The four tools
Every tool in this tutorial talks to the same thing: the Databricks REST API. The REST API is a set of HTTPS endpoints on your workspace host. You send JSON and receive JSON. The other tools are layers on top of it.
| Tool | What it is | Best for |
|---|---|---|
| REST API with curl or `requests` | Direct HTTP calls | Systems where you cannot install the CLI or SDK, and endpoints that wrappers do not cover yet |
| Databricks CLI | A command-line program with command groups such as `jobs` and `clusters` | Shell scripts, quick administrative tasks, and one-off operations |
| Databricks SDK for Python | A Python library with typed clients and built-in authentication | Application code and larger scripts with logic and error handling |
| Databricks Asset Bundles | Project definitions in `databricks.yml`, deployed through the CLI | Managing jobs and other resources as code, and CI/CD |
REST API
The REST API is the foundation. Anything the UI can do is available here, and new features usually appear in the API first. You build each request yourself: the URL, the authentication header, the JSON body, error handling, and pagination.
curl -sS -X POST "$DATABRICKS_HOST/api/2.1/jobs/run-now" \
-H "Authorization: Bearer $DATABRICKS_TOKEN" \
-H "Content-Type: application/json" \
-d '{"job_id": 123}'This call starts a run of an existing job. The response contains a run_id. Raw HTTP gives full control and works from any language, but you carry all the boilerplate.
CLI
The CLI wraps the API in commands. It handles authentication, builds the URL, and prints the response. It also stores connection details in named profiles, which Chapter 4 covers.
databricks auth login --host https://<workspace-host>
databricks current-user me
databricks auth profilesThe first command signs you in through the browser using OAuth and saves a profile. The second confirms which user the CLI is acting as. The third lists the saved profiles. Add --output json to a command and pipe the result to jq to feed other scripts:
databricks jobs list --output json | jq -r '.[] | "\(.job_id)\t\(.settings.name)"'Shell is a good fit when the task is a short sequence of commands. It is a poor fit when you need loops with retries, data structures, or tests.
SDK
The Databricks SDK for Python gives you objects and methods instead of URLs and JSON. It reads credentials from the environment or a profile, and it follows pagination for you.
from databricks.sdk import WorkspaceClient
w = WorkspaceClient() # reads DATABRICKS_HOST/DATABRICKS_TOKEN or a profile
for job in w.jobs.list():
print(job.job_id, job.settings.name)Compare this with the same task over raw HTTP. You would write the request, check the status code, parse the JSON, and loop over pages. The SDK is the better choice when the automation is real code that you will maintain and test.
Asset Bundles
Asset Bundles work at a different level. The other three tools issue individual commands. A bundle describes a whole set of resources, such as jobs, in a YAML file. The CLI then makes the workspace match that description.
bundle:
name: demo_bundle
variables:
cluster_id:
description: ID of an existing cluster to run the job on
resources:
jobs:
demo_job:
name: demo_job
tasks:
- task_key: main
existing_cluster_id: ${var.cluster_id}
notebook_task:
notebook_path: ./demo_notebook.ipynb
targets:
dev:
mode: development
default: true
workspace:
host: https://<workspace-host>Deployment takes three commands:
databricks bundle validate -t dev
databricks bundle deploy -t dev --var cluster_id=<cluster-id>
databricks bundle run -t dev --var cluster_id=<cluster-id> demo_jobA bundle is declarative: you state the end result, not the steps. This suits resources that should be reviewed, versioned, and deployed to more than one environment.
Choosing a tool
Start from the task, not the tool. These questions usually settle it:
- Are you defining a resource that should live in version control? Use an Asset Bundle. Jobs, pipelines, and similar resources belong here.
- Is it a short, one-off operation, or a few commands in a shell script? Use the CLI.
- Is it part of a Python program with branching, retries, or tests? Use the SDK.
- Is there no CLI or SDK available, or does the endpoint you need have no wrapper? Call the REST API directly. The
databricks apicommand is a middle path, because it sends a raw request but still handles your authentication.
The tools combine well. A common setup uses a bundle to deploy a job, a CI step to run databricks bundle run, and a small SDK script to check the results.
Common mistakes
- Using the UI to edit a job that a bundle manages. The next deploy overwrites the manual edit. Pick one source of truth.
- Reaching for raw HTTP first. It works, but you will reimplement authentication and pagination that the CLI and SDK already provide.
- Putting a personal access token in a script. Tokens in source files leak through version control. Use environment variables or a secret store.
- Writing a Bash script for logic-heavy work. Once a script needs nested data and error handling, move it to Python.
What the rest of the tutorial covers
The chapters follow this order. Setup and authentication come first, because every other tool depends on them. The CLI and scripting chapters come next, then the REST API, the Jobs API, pagination and errors, and the SDK. After that come bundles, credential handling, and CI/CD. The last chapter explains the difference between workspace-level and account-level APIs.
Exercise
You need to deploy the same job definition to a development workspace and a production workspace, with every change reviewed in pull requests. Which tool fits best, and why?
Show answer
Databricks Asset Bundles. The job is described in a databricks.yml file under version control, so each change goes through review. Separate targets (for example dev and prod) let you deploy the same definition to each workspace with databricks bundle deploy -t <target>.
Exercise
A teammate needs a one-off listing of all job IDs and names for a report. Which tool is the quickest, and what would the command look like?
Show answer
The CLI. Run databricks jobs list --output json | jq -r '.[] | "\(.job_id)\t\(.settings.name)"'. It needs no code project and prints one job per line.
Exercise
A Python service must start a job run, wait for it to finish, and retry on failure, with unit tests. Should it use the CLI, the SDK, or raw HTTP? Give one reason.
Show answer
The SDK. It fits application code that needs branching, retries, and tests, and it handles authentication and pagination so the service avoids hand-written HTTP code.