Sub topic 3
Using the Databricks SDK for Python
Assume you know basic Python, JSON, HTTP, and Databricks jobs. The SDK is a Python library that wraps Databricks APIs. It handles authentication, request construction, and pagination, so it is usually clearer than writing raw HTTP calls.
Use the SDK for workspace automation
Install the SDK in your environment:
pip install databricks-sdkThe client reads DATABRICKS_HOST and DATABRICKS_TOKEN, or a configured Databricks profile. The following script lists every job:
from databricks.sdk import WorkspaceClient
workspace = WorkspaceClient()
for job in workspace.jobs.list():
name = job.settings.name if job.settings else "(unnamed)"
print(job.job_id, name)Use the SDK when your automation needs Python logic, typed request objects, or automatic pagination. Use the CLI for short shell commands and the REST API when an endpoint is not yet represented by the SDK.
Define jobs with Asset Bundles
Databricks Asset Bundles let you store jobs and related resources as code. A databricks.yml file describes the resources and deployment targets.
bundle:
name: demo_bundle
variables:
cluster_id:
description: Existing cluster ID
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>This makes the job reviewable, repeatable, and deployable from version control.
Validate, deploy, and run
Run the bundle lifecycle in order:
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_jobValidation catches configuration errors before deployment. Deployment creates or updates the resources. Running starts the deployed job.
Protect credentials in CI/CD
Never commit tokens to source control or put them directly in bundle files. Store secrets in the CI/CD platform's secret store, expose them only as environment variables, and grant the service principal the smallest required permissions. OAuth or workload identity is preferable to long-lived personal access tokens when your platform supports it.
Choose the correct API scope
Workspace APIs manage objects inside one workspace, such as jobs, clusters, and notebooks. Account APIs manage account-wide objects, such as workspaces, users, groups, and account-level service principals. Their hosts, permissions, and SDK clients can differ. Confirm the endpoint's scope before writing automation.
Exercise
Why might you choose the SDK instead of a raw curl request?
Show answer
The SDK provides Python methods and handles common details such as authentication and pagination.