取得現有 SparkSession 的,或如果沒有現有的,則根據建構者設定的選項創建新的。
此方法首先檢查是否有有效的全域預設 SparkSession 值,若有則回傳該預設值。 若無有效的全域預設 SparkSession ,該方法會建立新 SparkSession 預設並指派為全域預設。 當已有 SparkSession 的檔案被回傳時,會套用本建構器中指定的設定選項。 若要建立新會話而不檢查,請使用 create。
Syntax
getOrCreate()
Returns
SparkSession
Examples
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