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

    https://github.com/apache/spark/pull/12860#discussion_r61930511
  
    --- Diff: python/pyspark/sql/session.py ---
    @@ -445,6 +452,86 @@ def read(self):
             """
             return DataFrameReader(self._wrapped)
     
    +    @classmethod
    +    @since(2.0)
    +    def builder(cls):
    +        """Returns a new :class:`SparkSession.Builder` for constructing a 
:class:`SparkSession`.
    +        """
    +        return SparkSession.Builder()
    +
    +    class Builder(object):
    +        """Builder for :class:`SparkSession`.
    +        """
    +
    +        _lock = RLock()
    +        _options = {}
    +
    +        @since(2.0)
    +        def config(self, key=None, value=None, conf=None):
    +            """Sets a config option. Options set using this method are 
automatically propagated to
    +            both :class:`SparkConf` and :class:`SparkSession`'s own 
configuration.
    +
    +            For an existing SparkConf, use `conf` parameter.
    +            >>> from pyspark.conf import SparkConf
    +            >>> SparkSession.builder().config(conf=SparkConf())
    +            <pyspark.sql.session.Builder object at ...>
    +
    +            For a (key, value) pair, you can omit parameter names.
    +            >>> SparkSession.builder().config("spark.some.config.option", 
"some-value")
    +            <pyspark.sql.session.Builder object at ...>
    +
    +            :param key: a key name string for configuration property
    +            :param value: a value for configuration property
    +            :param conf: an instance of :class:`SparkConf`
    +            """
    +            with self._lock:
    +                if conf is None:
    +                    self._options[key] = str(value)
    +                else:
    +                    for (k, v) in conf.getAll():
    +                        self._options[k] = v
    +                return self
    +
    +        @since(2.0)
    +        def master(self, master):
    +            """Sets the Spark master URL to connect to, such as "local" to 
run locally, "local[4]"
    +            to run locally with 4 cores, or "spark://master:7077" to run 
on a Spark standalone
    +            cluster.
    +
    +            :param master: a url for spark master
    +            """
    +            return self.config("spark.master", master)
    +
    +        @since(2.0)
    +        def appName(self, name):
    +            """Sets a name for the application, which will be shown in the 
Spark web UI.
    +
    +            :param name: an application name
    +            """
    +            return self.config("spark.app.name", name)
    +
    +        @since(2.0)
    +        def enableHiveSupport(self):
    +            """Enables Hive support, including connectivity to a 
persistent Hive metastore, support
    +            for Hive serdes, and Hive user-defined functions.
    +            """
    +            return self.config("spark.sql.catalogImplementation", "hive")
    +
    +        @since(2.0)
    +        def getOrCreate(self):
    +            """Gets an existing :class:`SparkSession` or, if there is no 
existing one, creates a new
    +            one based on the options set in this builder.
    +            """
    +            with self._lock:
    +                from pyspark.conf import SparkConf
    +                from pyspark.context import SparkContext
    +                from pyspark.sql.context import SQLContext
    +                sparkConf = SparkConf()
    +                for key, value in self._options.items():
    +                    sparkConf.set(key, value)
    +                sparkContext = SparkContext.getOrCreate(sparkConf)
    +                return SQLContext.getOrCreate(sparkContext).sparkSession
    --- End diff --
    
    if we just call scala's `getOrCreate` here then we don't need to fix this 
in the future


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at [email protected] or file a JIRA ticket
with INFRA.
---

---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to