SparkSession
| Also known as | Spark session, spark (the variable name in shells and notebooks)
|
|---|---|
| Field | Distributed data processing |
| Introduced | Apache Spark 2.0.0 |
| Key parts | Builder (master, remote, appName, config, getOrCreate), runtime configuration (spark.conf), catalog, DataFrame readers, SQL
|
| Open source implementations | Apache Spark (Spark Classic and Spark Connect) |
| Related | SparkContext, Driver (Apache Spark), Spark Connect, Lazy evaluation |
In Apache Spark, a SparkSession is the entry point for programming with the DataFrame and Dataset API: it reads data, runs SQL, holds session configuration and temporary views, and gives access to the catalog.[1] SparkSession was added in Spark 2.0.0 to replace SQLContext and HiveContext, which were kept for backward compatibility.[2] In Spark 4 the same SparkSession API exists in two forms: a classic session that runs inside the driver next to a SparkContext, and a Spark Connect session that is a thin client sending plans to a remote server.
How is a SparkSession created?
Code creates or reuses a SparkSession through its builder. In PySpark the main builder methods are:
master(url): the cluster manager URL for a classic session, for examplelocal[*].remote(url): the Spark Connect server URL, for examplesc://localhost:15002; this makes the session a Connect session.appName(name): the application name shown in the web UI.config(key, value): any Spark setting; values are passed to both theSparkConfand the session's own configuration.enableHiveSupport(): connect to a Hive Metastore for persistent tables (not implemented for Connect sessions).getOrCreate(): return the existing global default session, applying the builder's options to it, or create a new one.[1][3]
from pyspark.sql import SparkSession
spark = (
SparkSession.builder
.master("local[*]")
.appName("orders-report")
.config("spark.sql.shuffle.partitions", "64")
.getOrCreate()
)
spark.sql("SELECT 1 AS x").show()
Interactive tools usually create the session in advance. The PySpark shell creates it at startup and prints "SparkSession available as 'spark'."; in such environments the API documentation says to use the builder to get the existing session.[4][5]
Settings fall into two kinds. Runtime SQL settings such as spark.sql.shuffle.partitions can be changed at any time with spark.conf.set. Settings that shape the driver or executors, such as spark.driver.memory or spark.executor.cores, take effect only when the application starts; in client mode, driver memory must even be passed on the spark-submit command line.[6]
How does SparkSession relate to SparkContext?
A classic SparkSession wraps a SparkContext. The SparkContext is the connection to the cluster and owns the executors, the schedulers and the RDD API; only one SparkContext may be active per JVM.[7] Several SparkSessions can share that one SparkContext: newSession() returns a session with its own SQL configuration, temporary views and UDFs, but the same SparkContext and table cache.[1] Spark Connect sessions gained newSession() in PySpark 4.3.0; the new Connect session starts empty against the same server.[3]
In classic mode, spark.sparkContext returns the underlying SparkContext. In the Spark 4.3.0 Scala API, the sparkContext member is marked @ClassicOnly, and in a PySpark Connect session, reading spark.sparkContext raises the error JVM_ATTRIBUTE_NOT_SUPPORTED.[5][3]
Spark Connect sessions vs classic sessions
A Spark Connect SparkSession offers the same DataFrame and SQL API, but the client and the Spark driver are separate processes. The client builds unresolved logical plans and sends them over gRPC to a Spark Connect server, which analyzes and runs them.[8]
| Classic SparkSession | Spark Connect SparkSession | |
|---|---|---|
| Where it runs | In the driver JVM (PySpark talks to it through Py4J) | In the client process; the driver runs on the Connect server[8] |
| How it is created | SparkSession.builder.master(...)
|
SparkSession.builder.remote("sc://host:port"), or the SPARK_REMOTE environment variable[1]
|
Access to SparkContext and RDDs
|
Yes | No; SparkContext and the RDD API are not supported[8] |
| When a query is analyzed | Eagerly, when each transformation is defined | Lazily, when the plan is needed: schema access, explain, persisting or an action[9]
|
| Static Spark configuration | Set by the application | Not accessible to the client[8] |
| Client package for Python | pyspark
|
pyspark, or the lighter pyspark-client[8]
|
Spark 4.0.0 added the spark.api.mode setting (default classic). Set to connect, it makes a Spark Classic application start a local Spark Connect server and use a Connect session automatically.[6]
Spark Connect introduced session-scoped methods that cover tasks once done through the SparkContext: addArtifacts adds local files, Python files or archives to the session, and addTag with interruptTag or interruptAll cancels running operations. They were added in PySpark 3.5.0 for Connect and work in Spark Classic since 4.0.0.[1]
See also
References
- ↑ 1.0 1.1 1.2 1.3 1.4 session.py (v4.3.0). Apache Software Foundation.Retrieved 2026-11-23.
- ↑ Spark Release 2.0.0. Apache Software Foundation.Retrieved 2026-11-23.
- ↑ 3.0 3.1 3.2 connect/session.py (v4.3.0). Apache Software Foundation.Retrieved 2026-11-23.
- ↑ shell.py (v4.3.0). Apache Software Foundation.Retrieved 2026-11-23.
- ↑ 5.0 5.1 SparkSession.scala, sql/api (v4.3.0). Apache Software Foundation.Retrieved 2026-11-23.
- ↑ 6.0 6.1 Spark Configuration (Spark 4.3.0). Apache Software Foundation.Retrieved 2026-11-23.
- ↑ SparkContext.scala (v4.3.0). Apache Software Foundation.Retrieved 2026-11-23.
- ↑ 8.0 8.1 8.2 8.3 8.4 Spark Connect Overview (Spark 4.3.0). Apache Software Foundation.Retrieved 2026-11-23.
- ↑ Eager vs Lazy: Spark Connect vs Spark Classic (Spark 4.3.0). Apache Software Foundation.Retrieved 2026-11-23.