Github user huaxingao commented on a diff in the pull request:

    https://github.com/apache/spark/pull/22295#discussion_r223165392
  
    --- Diff: python/pyspark/sql/session.py ---
    @@ -252,6 +255,20 @@ def newSession(self):
             """
             return self.__class__(self._sc, self._jsparkSession.newSession())
     
    +    @classmethod
    +    @since(2.5)
    +    def getActiveSession(cls):
    +        """
    +        Returns the active SparkSession for the current thread, returned 
by the builder.
    +        >>> s = SparkSession.getActiveSession()
    +        >>> l = [('Alice', 1)]
    +        >>> rdd = s.sparkContext.parallelize(l)
    +        >>> df = s.createDataFrame(rdd, ['name', 'age'])
    +        >>> df.select("age").collect()
    +        [Row(age=1)]
    +        """
    +        return cls._activeSession
    --- End diff --
    
    @HyukjinKwon I am not sure if I follow your suggestion correctly. Does the 
following look right to you?
    session.py
    ```
        @classmethod
        @since(3.0)
        def getActiveSession(cls):
            from pyspark.sql import functions
            return functions.getActiveSession()
    ```
    functions.py
    ```
    @since(3.0)
    def getActiveSession():
        from pyspark.sql import SparkSession
        sc = SparkContext._active_spark_context
        if sc is None:
          sc = SparkContext()
    
        if sc._jvm.SparkSession.getActiveSession().isDefined():
            SparkSession(sc, sc._jvm.SparkSession.getActiveSession().get())
            return SparkSession._activeSession
        else:
            return None
    ```



---

---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscr...@spark.apache.org
For additional commands, e-mail: reviews-h...@spark.apache.org

Reply via email to