getOrCreate (SparkSession.Builder)

获得现有 SparkSession 的,或者如果没有现有的,则根据该建筑师设置的选项创建新的。

该方法首先检查是否存在有效的全局默认,如果存在,则返回该默认值 SparkSession 。 如果没有有效的全局默认, SparkSession 方法会创建一个新的 SparkSession ,并将其分配为全局默认。 当返回已有 SparkSession 的配置时,会应用该构建器中指定的配置选项。 要创建新会话而不检查,请使用 create

Syntax

getOrCreate()

返回

SparkSession

示例

s1 = SparkSession.builder.config("k1", "v1").getOrCreate()
s1.conf.get("k1") == "v1"
# True

# The configuration of the SparkSession can be changed afterwards.
s1.conf.set("k1", "v1_new")
s1.conf.get("k1") == "v1_new"
# True

# When an existing SparkSession is returned, the config options specified
# in this builder are applied to the existing SparkSession.
s2 = SparkSession.builder.config("k2", "v2").getOrCreate()
s1.conf.get("k1") == s2.conf.get("k1") == "v1_new"
# True
s1.conf.get("k2") == s2.conf.get("k2") == "v2"
# True