我相信文档在这里有点误导,当您使用Scala时,您实际上会看到如下警告:
... WARN SparkSession$Builder: Use an existing SparkSession, some configuration may not take effect.
在Spark 2.0之前更明显的是在上下文之间有明确的分离:
SparkContext 配置无法在运行时修改 . 您必须先停止现有的上下文 .
SQLContext 配置可以在运行时修改 .
与许多其他选项一样, spark.app.name 绑定到 SparkContext ,并且在不停止上下文的情况下无法修改 .
Reusing existing SparkContext / SparkSession
import org.apache.spark.SparkConf
import org.apache.spark.sql.SparkSession
spark.conf.get("spark.sql.shuffle.partitions")
String = 200
val conf = new SparkConf()
.setAppName("foo")
.set("spark.sql.shuffle.partitions", "2001")
val spark = SparkSession.builder.config(conf).getOrCreate()
... WARN SparkSession$Builder: Use an existing SparkSession ...
spark: org.apache.spark.sql.SparkSession = ...
spark.conf.get("spark.sql.shuffle.partitions")
String = 2001
spark.app.name 配置更新时:
spark.conf.get("spark.app.name")
String = foo
它不会影响 SparkContext :
spark.sparkContext.appName
String = Spark shell
Stopping existing SparkContext / SparkSession
现在让我们停止会话并重复该过程:
spark.stop
val spark = SparkSession.builder.config(conf).getOrCreate()
... WARN SparkContext: Use an existing SparkContext ...
spark: org.apache.spark.sql.SparkSession = ...
spark.sparkContext.appName
String = foo
有趣的是,当我们停止会话时,我们仍然会收到有关使用现有 SparkContext 的警告,但您可以检查它是否已停止 .