Sub topic 2
The spark session you already have
This chapter assumes you can attach a notebook to compute in a Databricks workspace and that you know basic Python or SQL. You do not need to have used Spark before.
What the spark session is
Spark is the engine that runs your queries across a cluster. The entry point to that engine is a SparkSession. It is the object you use to create DataFrames, read files, run SQL, and look up tables.
In a Databricks notebook, a SparkSession already exists when the first cell runs. It is bound to the variable spark. You do not build it, configure it, or stop it.
print(type(spark))
print(spark.version)The first line prints the session class. The second prints the Spark version of your compute, which is useful when you read documentation.
Where the session fits
Every DataFrame you create comes from the session, directly or indirectly. The session holds the connection to the compute and the current catalog settings.
Common entry points
These are the session methods you will use most.
| Method | What it returns |
|---|---|
| `spark.createDataFrame(rows, schema)` | A DataFrame built from in-memory rows |
| `spark.read` | A reader that loads files or tables into a DataFrame |
| `spark.sql("...")` | The result of a SQL query as a DataFrame |
| `spark.table("catalog.schema.table")` | A registered table as a DataFrame |
The first example builds a small DataFrame from rows and a schema string. It is the dataset used in the rest of the tutorial.
from pyspark.sql import functions as F
orders = spark.createDataFrame(
[
(1, "alice", "2024-01-05", 120.0),
(2, "bob", "2024-01-06", 75.5),
(3, "alice", "2024-02-10", 30.0),
(4, "carol", "2024-02-11", None),
],
"order_id INT, customer STRING, order_date STRING, amount DOUBLE",
)
orders = orders.withColumn("order_date", F.to_date("order_date"))
orders.printSchema()
display(orders)The schema string uses DDL syntax, the same column-and-type text you would write in a CREATE TABLE statement. The None value becomes a null in the amount column.
Session settings
The session also exposes runtime configuration through spark.conf. You can read a setting and change it for the current notebook session.
print(spark.conf.get("spark.sql.shuffle.partitions"))
spark.conf.set("spark.sql.shuffle.partitions", "64")Settings changed this way last only as long as the session. Cluster-wide settings belong in the compute configuration.
Common errors
- Creating your own session. Calling
SparkSession.builder.getOrCreate()in a notebook usually returns the existing session, so it adds nothing. Usespark. - Stopping the session. Calling
spark.stop()breaks the notebook. You then have to detach and reattach the compute. - Forgetting the imports. The session is predefined. The functions module is not. Add
from pyspark.sql import functions as Fyourself. - Assuming session state is shared. Temporary views and
spark.confchanges belong to one notebook session. Another notebook does not see them.
Exercise
A teammate's notebook starts with spark = SparkSession.builder.appName("etl").getOrCreate(). Is this line needed in a Databricks notebook, and why?
Show answer
No. A session is already bound to spark when the notebook starts. In a notebook, getOrCreate() normally returns that existing session, so the line has no useful effect. Remove it and use spark directly.
Exercise
You run spark.conf.set("spark.sql.shuffle.partitions", "64") in one notebook. A colleague's notebook is attached to the same cluster. Does their session use 64 partitions?
Show answer
No. The setting applies to your notebook's session only. Their notebook has its own session and keeps its own value.