Sub topic 2
Attaching a notebook to compute
This chapter assumes you can create a notebook in the Databricks workspace and write basic Python or SQL. A notebook holds code, but it cannot run anything until it is attached to compute. Compute is the set of machines that executes your cells.
What attaching does
When you attach a notebook, Databricks links the notebook to a compute resource. Each notebook gets its own execution context on that compute, called a session. The session holds your variables, imported modules, and temporary views. Two notebooks attached to the same cluster do not see each other's variables.
You pick compute from the selector at the top right of the notebook. Common choices are:
- All-purpose compute: a cluster you start and share for interactive work. You pay while it runs, so set an auto-termination time.
- Serverless compute: compute that Databricks manages and starts in seconds. You do not configure machines. It is available only in workspaces where it is enabled, and it supports a more limited set of settings than classic clusters.
- Job compute: a cluster created for one job run and removed afterward. You do not attach to it by hand. A job assigns it to the notebook task.
Attaching and running
- Open the notebook and select the compute selector.
- Choose a running cluster, start a stopped one, or pick serverless.
- Wait for the status to show the notebook is connected.
- Run a cell with Shift+Enter.
Check that the attachment works by running a small cell:
print(spark.version)
spark.range(3).show()If this prints a Spark version and three rows, the notebook is connected and the session is working.
Detaching and state
Detaching a notebook, or restarting the cluster, ends the session. All variables are lost, and you must run the cells again. The notebook text and saved results stay. Data you wrote to tables or files also stays. This is why long work should write results to storage and not rely on variables.
You can use Detach & re-attach to get a clean session without restarting the whole cluster. This is useful when stale variables cause confusing results.
Common errors
| Symptom | Likely cause | Fix |
|---|---|---|
| Cell stays "Waiting to run" | Cluster is starting or busy | Wait, or check the cluster state |
| `NameError` for a variable you defined | Session was reset after detach or restart | Re-run the defining cells |
| Library import fails | Library is not installed on this compute | Install it on the cluster, or use `%pip install` in the notebook |
| "Permission denied" when selecting a cluster | You lack attach permission | Ask an admin for the *Can Attach To* permission |
Choosing compute
Use a small all-purpose cluster or serverless for development. Use job compute for scheduled runs, because it is isolated and usually cheaper. Remember that the compute type can limit which languages and features work, so check the selector if a command is rejected.
Exercise
You restart a cluster and a notebook cell now raises NameError for a DataFrame you created earlier. Why did this happen, and what should you do?
Show answer
Restarting the cluster ended the notebook's session, which held the variable. The notebook text stays, but the in-memory state is gone. Re-run the cells that create the DataFrame. For work you cannot afford to lose, write results to a table.