SparkContext
| Also known as | sc (the variable name in shells)
|
|---|---|
| Field | Distributed data processing |
| Key parts | Connection to the cluster manager, executors, DAG and task schedulers, RDD API, broadcast variables, accumulators, job groups and tags |
| Open source implementations | Apache Spark (Spark Classic only) |
| Related | SparkSession, Driver (Apache Spark), RDD, Spark Connect |
In Apache Spark, a SparkContext is the object in the driver that represents the connection to a Spark cluster; it acquires executors, schedules work on them and is used to create RDDs, accumulators and broadcast variables.[1] The source code still calls it the "main entry point for Spark functionality", but since Spark 2.0.0 DataFrame and SQL code starts from a SparkSession, which creates or reuses a SparkContext underneath.[1][2] In Spark 4 the SparkContext still runs every classic application, but Spark Connect clients cannot use it.
What does SparkContext still do in Spark 4?
SparkContext remains the core of a Spark Classic application in Spark 4.3.0. The cluster overview describes Spark applications as independent sets of processes coordinated by the SparkContext in the driver program: it connects to the cluster manager (standalone, YARN or Kubernetes), acquires executors, sends them the application code and then sends them tasks.[3] DataFrame and SQL queries are planned by the SparkSession and then run as jobs submitted through this SparkContext.
Only one SparkContext may be active per JVM. Creating a second one while the first is running fails with "Only one SparkContext should be running in this JVM (see SPARK-2243)"; the first must be stopped with stop() before a new one is made.[1] A side effect of this design is isolation: each application has its own executors, so two applications cannot share in-memory data except through external storage.[3]
The public API of SparkContext in Spark 4.3.0 still covers these areas:[1]
| Area | Examples of SparkContext methods |
|---|---|
| Creating RDDs | parallelize, textFile, runJob
|
| Shared variables | broadcast, longAccumulator
|
| Shipping dependencies | addFile, addJar
|
| Grouping and cancelling jobs | setJobGroup, cancelJobGroup, setLocalProperty
|
| Inspecting the application | applicationId, uiWebUrl, statusTracker, defaultParallelism, setLogLevel
|
| Fault tolerance for long lineages | setCheckpointDir
|
In PySpark Classic, spark.sparkContext returns the SparkContext behind a session. Code that uses only DataFrames and SQL seldom needs it; code that uses the RDD API, accumulators or sc.setJobGroup does.
Why do Spark Connect clients not have a SparkContext?
A Spark Connect client does not run in the same process as the Spark driver. It builds logical plans and sends them to a Spark Connect server, which runs the driver and its SparkContext. The Spark Connect overview lists two consequences: the protocol, which is built on logical plans, does not support all of Spark's execution APIs, "most importantly RDDs"; and the client, which gets a session rather than the whole application, has no access to the static Spark configuration or the SparkContext.[4]
The reason is that one server's SparkContext serves many client sessions. The overview explains that a Connect client gets a session-based interface and has no access to properties that change the environment for all connected clients, the SparkContext above all.[4] In PySpark 4.3.0, reading spark.sparkContext on a Connect session raises JVM_ATTRIBUTE_NOT_SUPPORTED, and the Scala API marks SparkSession.sparkContext as @ClassicOnly.[5][6]
What replaces SparkContext calls in Spark Connect?
The Spark Connect application guide says that RDDs can no longer be used directly from Spark Connect clients, and that alternatives for missing RDD functionality are provided in the higher-level APIs.[7] For common SparkContext calls, the SparkSession has methods that work over Spark Connect:
| SparkContext usage | Session-level alternative |
|---|---|
sc.parallelize(data) |
spark.createDataFrame(data) or spark.range(n), which build a DataFrame instead of an RDD[8]
|
sc.addFile, sc.addPyFile |
spark.addArtifacts(...)[8]
|
sc.setJobGroup, sc.cancelJobGroup |
spark.addTag(tag), then spark.interruptTag(tag) or spark.interruptAll()[8]
|
The session-level methods in this table work in both Connect and Classic sessions since Spark 4.0.0, so code that uses them runs in either mode.[8]
See also
References
- ↑ 1.0 1.1 1.2 1.3 SparkContext.scala (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 Cluster Mode Overview (Spark 4.3.0). Apache Software Foundation.Retrieved 2026-11-23.
- ↑ 4.0 4.1 Spark Connect Overview (Spark 4.3.0). Apache Software Foundation.Retrieved 2026-11-23.
- ↑ connect/session.py (v4.3.0). Apache Software Foundation.Retrieved 2026-11-23.
- ↑ SparkSession.scala, sql/api (v4.3.0). Apache Software Foundation.Retrieved 2026-11-23.
- ↑ Application Development with Spark Connect (Spark 4.3.0). Apache Software Foundation.Retrieved 2026-11-23.
- ↑ 8.0 8.1 8.2 8.3 session.py (v4.3.0). Apache Software Foundation.Retrieved 2026-11-23.