Built with Tutify.io

    AIStudyLabs

    What is Spark Context?

    What is Spark Context?

    Introduction

    SparkContext has been available since Spark 1.x (JavaSparkContext for Java), and it used to be an entry point to Spark before introducing SparkSession in 2.0. Creating SparkContext is the first step to using RDD and connecting to a Spark Cluster.

    What is SparkContext

    Since Spark 1.x, SparkContext is an entry point to Spark and is defined in org.apache.spark.SparkContext package. It is used to programmatically create Spark RDD, accumulators, and broadcast variables on the cluster. Its object sc is the default variable available in spark-shell and can be programmatically created using the SparkContext class.

    Note that you can create only one active SparkContext per JVM. You should stop() the active SparkContext before creating a new one.

    <img height="319" width="596" src="https://aistudylabs.com/wp-content/uploads/2023/09/image-8.png" />

    The Spark driver program creates and uses SparkContext to connect to the cluster manager, submit Spark jobs, and know what resource manager (YARN, Mesos, or Standalone) to communicate with. It is the heart of the Spark application.

    SparkContext in spark-shell

    By default, spark-shell provides an sc object, which is an instance of the SparkContext class. We can directly use this object where required.

    // 'sc' is a SparkContext variable in spark-shell
    scala> sc.appName
    

    Similar to the Spark shell, in most of the tools, notebooks, and Azure Databricks, the environment itself creates a default SparkContext object for us to use, so you don’t have to worry about creating a Spark context.

    Create SparkContext

    Since Spark 2.0, we mostly use SparkSession as most of the methods available in SparkContext are also present in SparkSession. SparkSession internally creates the SparkContext and exposes the sparkContext variable to use.

    At any given time, only one SparkContext instance should be active per JVM. If you want to create another, you should stop the existing SparkContext (using stop()) before creating a new one.

    // Imports
    import org.apache.spark.sql.SparkSession
    
    object SparkSessionTest extends App {
      // Create SparkSession object
      val spark = SparkSession.builder()
        .master("local[1]")
        .appName("AIStudyLabs.com")
        .getOrCreate()
    
      // Access spark context
      println(spark.sparkContext)
      println("Spark App Name: " + spark.sparkContext.appName)
    }
    
    // Output:
    // org.apache.spark.SparkContext@2fdf17dc
    // Spark App Name: AIStudyLabs.com
    

    Stop SparkContext

    You can stop the SparkContext by calling the stop() method. As explained above, you can have only one SparkContext per JVM. If you want to create another, you need to shut it down first by using the stop() method and create a new SparkContext.

    // SparkContext stop() method
    spark.sparkContext.stop()
    

    When Spark executes this statement, it logs the message INFO SparkContext: Successfully stopped SparkContext to the console or to a log file.

    Spark 1.x – Creating SparkContext using Scala Program

    In Spark 1.x, first, you need to create a SparkConf instance by assigning the app name and setting the master by using the SparkConf static methods setAppName() and setMaster() respectively. Then, pass the SparkConf object as an argument to the SparkContext constructor to create Spark Context.

    // Import
    import org.apache.spark.{SparkConf, SparkContext}
    
    // Create SparkConf object
    val sparkConf = new SparkConf()
      .setAppName("aistudylabs.com")
      .setMaster("local[1]")
    
    // Create Spark context (deprecated)
    val sparkContext = new SparkContext(sparkConf)
    

    The SparkContext constructor has been deprecated in 2.0; hence the recommendation is to use a static method getOrCreate() that internally creates SparkContext. This function instantiates a SparkContext and registers it as a singleton object.

    // Create Spark Context
    val sc = SparkContext.getOrCreate(sparkConf)
    

    Further Reading

    For additional learning on this topic, I would recommend reading the following from AIStudyLabs.com.

    Summary and Key Takeaways

    Apache Spark Tutorial with Scala Examples — SparkSession & SparkContext

    Introduction to Apache Spark

    Setup Apache Spark Environment

    SparkSession & SparkContext

    Resilient Distributed Datasets (RDDs)

    Apache Spark Concepts

    Cache, Persistence and Shared Variables

    DataFrame Introduction

    DataFrame Narrow Transformation

    DataFrame Wider Transformation

    DataFrame Actions

    Spark SQL Functions

    Advanced DataFrame Features

    Using SQL Queries

    Spark Read & Write CSV Files

    Working with JSON Files

    Working with Parquet Files

    Spark Dataset

    What Next?

    Tutorial Not Found