Built with Tutify.io

    AIStudyLabs

    What is Spark Context?

    What is Spark Context?

    Introduction

    pyspark.SparkContext is an entry point to the PySpark functionality that is used to communicate with the cluster and to create an RDD, accumulator, and broadcast variables.

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

    Spark Context Diagram

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

    Related: How to get current SparkContext & its configurations in Spark

    SparkContext in PySpark shell

    By default, the PySpark shell creates and provides sc object, which is an instance of the SparkContext class. We can directly use this object where required without the need to create.

    sc.appName
    

    Similar to the PySpark shell, in most of the tools, notebooks, and Azure Databricks, the environment itself creates a default SparkContext object for us to use.

    Create SparkContext

    Since PySpark 2.0, Creating a SparkSession creates a SparkContext internally and exposes the sparkContext variable to use. Hence, you don't have to explicitly create a SparkContext object.

    # Create SparkSession from builder
    from pyspark.sql import SparkSession
    
    spark = SparkSession.builder.master("local[1]") \
                        .appName('AIStudyLabs.com') \
                        .getOrCreate()
    
    # Access sparkContext variable
    print(spark.sparkContext)
    print("Spark App Name : " + spark.sparkContext.appName)
    
    # Outputs:
    # <SparkContext master=local[1] appName=AIStudyLabs.com>
    # Spark App Name : AIStudyLabs.com
    

    Note: As I explained in the SparkSession article, you can create any number of SparkSession objects however, for all those objects underlying there will be only one SparkContext.

    Stop PySpark 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 down it first by using the stop() method and create a new SparkContext.

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

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

    When you try to create multiple SparkContext you will get the below error.

    ValueError: Cannot run multiple SparkContexts at once;

    Summary and Key Takeaways

    Introduction to PySpark for Data Engineering — SparkSession & SparkContext

    Introduction to PySpark

    Setup PySpark 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

    PySpark SQL Functions

    Advanced DataFrame Features

    PySpark SQL

    Spark Read & Write CSV Files

    Working with JSON Files

    Working with Parquet Files

    What Next?

    Tutorial Not Found