Built with Tutify.io

    AIStudyLabs

    Creating SparkSession

    Creating SparkSession

    SparkSession in spark-shell

    By default, Spark shell provides spark object which is an instance of the SparkSession class. We can directly use this object when required in spark-shell

    // Usage of spark variable
    scala> spark.version
    

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

    Create SparkSession From Scala Program

    To create SparkSession in Scala or Python, you need to use the builder pattern method builder() and calling getOrCreate() method. If SparkSession already exists it returns otherwise creates a new SparkSession.

    // 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() 
         println(spark)  
         println("Spark Version : "+spark.version)
    }
    
    // Output:
    // org.apache.spark.sql.SparkSession@2fdf17dc
    // Spark Version : 4.1.1
    

    From the above code: 

    SparkSession.builder() – Return SparkSession.Builder class. This is a builder for SparkSession. master(), appName(), and getOrCreate() are methods of SparkSession.Builder.

    master() – This allows Spark applications to connect and run in different modes (local, standalone cluster, Mesos, YARN), depending on the configuration.

    Get Existing SparkSession

    You can get the existing SparkSession in Scala programmatically using the below example.

    // Get existing SparkSession 
    import org.apache.spark.sql.SparkSession
    val spark2 = SparkSession.builder().getOrCreate()
    print(spark2)
    
    // Output:
    // org.apache.spark.sql.SparkSession@2fdf17dc
    

    Compare the hash with the previous example. Since it returned the existing session object, both objects spark and spark2 have the same hash value.

    Create Another SparkSession

    Sometimes you might be required to create multiple sessions, which you can easily achieve by using newSession() method. This uses the same app name, master as the existing session. Underlying SparkContext will be the same for both sessions as you can have only one context per Spark application.

    // Create a new SparkSession
    val spark3 = spark.newSession()
    print(spark3)
    
    // Output:
    // org.apache.spark.sql.SparkSession@692dba54
    

    Compare this hash with the hash from the above example, it should be different.

    Setting Spark Configs

    If you want to set some configs to SparkSession, use the config() method.

    // Usage of config()
    val spark = SparkSession.builder().master("local[1]").appName("AIStudyLabs.com")                     .config("spark.some.config.option","config-value").getOrCreate()
    

    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