Sub topic 2
Choosing a task type
This chapter assumes you have run Databricks notebooks, know Python or SQL, and have a general idea of what clusters are. A job is made of tasks. Each task has a type that decides what code it runs and what compute it needs. Choosing the right type keeps your workflow simple and cheap.
The common task types
Every task in a job has a task_key (its unique name inside the job) and exactly one type block, such as notebook_task. The types you will use most are these:
| Task type | Runs | Typical use |
|---|---|---|
| Notebook | A Databricks notebook | ETL steps, exploration turned into production |
| Python script | A `.py` file from the workspace, a repo, or cloud storage | Plain Python without notebook features |
| Python wheel | An entry point in a packaged wheel | Shared, tested library code |
| SQL | A query, alert, or dashboard refresh on a SQL warehouse | Reporting and SQL-only transforms |
| JAR | A `main` class in a JAR | Existing Scala or Java Spark code |
| Pipeline | A Lakeflow Declarative Pipelines (DLT) update | Managed streaming or batch pipelines |
| dbt | dbt commands | Teams already using dbt |
Control-flow task types (If/else, For each, Run Job) do not run your code. They steer the workflow. Later chapters cover them.
How to decide
Ask these questions in order:
- Is the logic SQL only? Use a SQL task. It runs on a SQL warehouse, so you do not need a cluster.
- Is it a declarative pipeline? Use a pipeline task, so the pipeline manages its own compute and dependencies.
- Is the code packaged and versioned? Use a Python wheel or JAR task. This suits code that has unit tests and a release process.
- Is it a single script? Use a Python script task.
- Otherwise, use a notebook task. It is the easiest to write and debug, and it is the type used in the example below.
A notebook is convenient, but it is harder to test and review than a wheel. Many teams start with notebooks and move stable logic into wheels.
A notebook task in a job definition
This Jobs API fragment defines one notebook task. The type block, notebook_task, is what makes the task a notebook task. The rest of the task (task_key, retries, compute) is the same for every type.
{
"task_key": "ingest",
"job_cluster_key": "etl_cluster",
"notebook_task": {"notebook_path": "/Workspace/Shared/ingest"},
"max_retries": 2,
"min_retry_interval_millis": 60000
}To switch type, replace the notebook_task block with another one, for example spark_python_task or sql_task, and adjust the compute. A SQL task takes a warehouse_id instead of job_cluster_key.
Common mistakes
- Mixing compute assumptions. A SQL task cannot use a job cluster. A notebook task that runs only SQL still starts a cluster, which costs more and starts slower.
- Duplicate task keys. Each
task_keymust be unique in a job. Dependencies refer to it, so renaming one later breaks the tasks that depend on it. - Putting everything in one task. One large notebook cannot be retried or repaired in parts. Split the work into several tasks so a failure only reruns its own step.
Exercise
Your job has a step that runs one aggregation query and writes a table. The team has no cluster configured for it. Which task type fits best, and what compute does it use?
Show answer
A SQL task. It runs on a SQL warehouse, so you set a warehouse_id and do not need a job cluster. A notebook task would start a cluster only to run SQL.