Introduction
JavaSparkContext has been available since Spark 1.x and served as the primary entry point for Java-based Spark applications before the introduction of SparkSession in Spark 2.0. It is used to initialize Spark, connect to a cluster, and start working with RDDs.
In modern Spark (2.x+), SparkSession is the recommended entry point, but JavaSparkContext is still important when working with low-level RDD APIs in Java.
What is JavaSparkContext
JavaSparkContext is defined in the org.apache.spark.api.java package and acts as a Java-friendly wrapper around SparkContext.
It allows Java developers to:
- Create RDDs (
JavaRDD,JavaPairRDD) - Work with accumulators and broadcast variables
- Interact with the Spark cluster using Java APIs
Unlike SparkContext (which is Scala-based), JavaSparkContext provides APIs that are natural to Java developers, avoiding Scala interoperability complexities.
⚠️ Note: Only one active SparkContext can exist per JVM. Always call
stop()before creating a new one.
SparkContext vs JavaSparkContext
Core Difference
- SparkContext
- Core engine of Spark
- Designed for Scala
- Works with
RDD<T>
- JavaSparkContext
- Wrapper around SparkContext
- Designed for Java developers
- Works with
JavaRDD<T>and Java-friendly APIs
Key Differences
- JavaSparkContext provides Java-specific abstractions
- SparkContext uses Scala-based APIs
- Both share the same underlying execution engine
- JavaSparkContext internally delegates to SparkContext
👉 In practice:
- Use JavaSparkContext when working with RDDs in Java
- SparkContext is rarely used directly in Java code
SparkContext in spark-shell
In spark-shell, an sc object is automatically created, which is an instance of SparkContext.
- This shell is Scala-based
- JavaSparkContext is not directly available here
- For Java, you typically write standalone applications instead of using spark-shell
Creating JavaSparkContext (Modern Approach)
Since Spark 2.0, SparkSession is the preferred entry point. It internally creates and manages SparkContext.
Java Example:
SparkSession spark = SparkSession.builder()
.appName("Example")
.master("local[*]")
.getOrCreate();
// Access SparkContext
SparkContext sc = spark.sparkContext();
// Wrap into JavaSparkContext
JavaSparkContext jsc = new JavaSparkContext(sc);
