How can I get the current SparkSession in any place of the codes?

前端 未结 3 1531
暗喜
暗喜 2020-12-25 15:11

I have created a session in the main() function, like this:

val sparkSession = SparkSession.builder.master(\"local[*]\").appName(\"Simple Applic         


        
相关标签:
3条回答
  • 2020-12-25 15:42

    When SparkSession variable has been defined as

    val sparkSession = SparkSession.builder.master("local[*]").appName("Simple Application").getOrCreate()
    

    This variable is going to point/refer to only one SparkSession as its a val. And you can always pass to different classes for them to access as well as

    val newClassCall = new NewClass(sparkSession)
    

    Now you can use the same sparkSession in that new class as well.

    0 讨论(0)
  • 2020-12-25 15:43

    Since 2.2.0 you can access the active SparkSession through:

    /**
     * Returns the active SparkSession for the current thread, returned by the builder.
     *
     * @since 2.2.0
     */
    def getActiveSession: Option[SparkSession] = Option(activeThreadSession.get)
    

    or default SparkSession:

    /**
     * Returns the default SparkSession that is returned by the builder.
     *
     * @since 2.2.0
     */
    def getDefaultSparkSession: Option[SparkSession] = Option(defaultSession.get)
    
    0 讨论(0)
  • 2020-12-25 16:07

    Once a session was created (anywhere), you can safely use:

    SparkSession.builder().getOrCreate()
    

    To get the (same) session anywhere in the code, as long as the session is still alive. Spark maintains a single active session so unless it was stopped or crashed, you'll get the same one.

    0 讨论(0)
提交回复
热议问题