DatabricksNotebookOperator¶
Use the DatabricksNotebookOperator to launch and monitor
notebook job runs on Databricks as Airflow tasks.
Examples¶
Running a notebook in Databricks on a new cluster¶
new_cluster_spec = {
"cluster_name": "",
"spark_version": "11.3.x-scala2.12",
"aws_attributes": {
"first_on_demand": 1,
"availability": "SPOT_WITH_FALLBACK",
"zone_id": "us-east-2b",
"spot_bid_price_percent": 100,
"ebs_volume_count": 0,
},
"node_type_id": "i3.xlarge",
"spark_env_vars": {"PYSPARK_PYTHON": "/databricks/python3/bin/python3"},
"enable_elastic_disk": False,
"data_security_mode": "LEGACY_SINGLE_USER_STANDARD",
"runtime_engine": "STANDARD",
"num_workers": 8,
}
notebook_1 = DatabricksNotebookOperator(
task_id="notebook_1",
notebook_path="/Shared/Notebook_1",
notebook_packages=[
{
"pypi": {
"package": "simplejson==3.18.0",
"repo": "https://pypi.org/simple",
}
},
{"pypi": {"package": "Faker"}},
],
source="WORKSPACE",
new_cluster=new_cluster_spec,
)
Running a notebook in Databricks on an existing cluster¶
notebook_2 = DatabricksNotebookOperator(
task_id="notebook_2",
notebook_path="/Shared/Notebook_2",
notebook_packages=[
{
"pypi": {
"package": "simplejson==3.18.0",
"repo": "https://pypi.org/simple",
}
},
],
source="WORKSPACE",
existing_cluster_id="existing_cluster_id",
)
Configuring Databricks-native task retries¶
Use max_retries, min_retry_interval_millis and retry_on_timeout to configure
Databricks-native task retries.
Databricks reruns failed task attempts within the same job run, so Airflow sees only the final result.
Set max_retries to -1 to retry indefinitely, or 0 to disable retries.
These settings are independent of the Airflow task-level retries parameter, which retries the
whole Airflow task:
DatabricksNotebookOperator(
task_id="notebook",
notebook_path="/path/to/notebook",
source="WORKSPACE",
existing_cluster_id="existing_cluster_id",
max_retries=3,
min_retry_interval_millis=2000,
retry_on_timeout=True,
)
Airflow retries behaves differently depending on where the operator runs. For a standalone
operator, a retry reconnects to the run the previous try submitted only when that try was lost
while the run was still going; a run that already reached a terminal failure on Databricks is
replaced by a fresh submit (see Durable execution below). Inside a
DatabricksWorkflowTaskGroup,
the Airflow task monitors a sub-run that was already submitted by the workflow launch task, so a
retry only re-polls the terminal sub-run. Use max_retries to retry Databricks work inside a
workflow task group.
Inside a
DatabricksWorkflowTaskGroup,
a task that exhausts a finite max_retries is reported as failed as soon as its final failed
attempt is observed, so downstream failure handling is not delayed by long-running sibling tasks.
Only unlimited retries (max_retries=-1) keep the Airflow task waiting (or deferring) until the
parent workflow run reaches a terminal state, because Databricks may still launch a retry attempt
under the same task_key until then. Sibling tasks in the run continue independently.
Durable execution¶
Outside a
DatabricksWorkflowTaskGroup,
this operator submits a run and then waits for it, on the worker or in the Triggerer when
deferrable=True. By default it runs in a durable mode: the Databricks run id is persisted to
task state store before the wait begins, so if
the worker crashes or is preempted and the task is retried, the operator reconnects to the run that
is already executing on Databricks instead of submitting a duplicate.
On retry the operator checks the prior run’s state:
if it is still running, the operator reconnects and continues polling
if it already succeeded, the operator returns immediately without resubmitting
if it failed terminally, the operator submits a fresh run
Pass durable=False to always submit a fresh run on retry.
Unlike DatabricksSubmitRunOperator and
DatabricksRunNowOperator, where
durable has no effect in deferrable mode, durable execution here also covers
deferrable=True: the run id is stored before the task defers, so a try lost while the Triggerer
was waiting still reconnects on the next one.
Durable execution requires Airflow 3.3 or newer, since it relies on the task state store. On earlier Airflow versions the flag is a no-op and the operator always submits a fresh run on retry. If the task state store is unavailable at runtime, the operator logs that crash recovery is disabled and behaves the same way.
Inside a DatabricksWorkflowTaskGroup nothing changes: the run is submitted by the workflow
launch task and this operator only monitors its own task within that shared run, so a retry already
reconnects rather than resubmitting.
[state_store] default_retention_days (30 days by default) is stamped on the run id when it is
stored, so a retry that comes later than that finds nothing and submits a fresh run instead of
reconnecting, whatever schedule airflow state-store clean runs on. Size that setting against
your longest retry_delay. See
Task and Asset State Store Cleanup.
Clearing a task discards the stored run id by default, so the next attempt submits a fresh run.
Set keep_task_state when clearing to keep it instead.
Resumable Tasks lists which clear paths discard it and
describes the [state_store] clear_on_success setting.
Where the id is kept, clearing behaves like a retry rather than a fresh start. If the stored run
already succeeded, the next attempt reads it back, re-pushes its XComs and returns without
submitting anything. If the run is still going and wait_for_termination=False, the attempt
reconnects to it and returns immediately, also without submitting.