Spark 2.0:通过 GetOrCreate 重新定义 SparkSession 参数并且在 WebUI 中看不到变化

Spark 2.0: Redefining SparkSession params through GetOrCreate and NOT seeing changes in WebUI

我正在使用 Spark 2.0 和 PySpark。

我正在通过 2.0 中引入的 GetOrCreate 方法重新定义 SparkSession 参数:

This method first checks whether there is a valid global default SparkSession, and if yes, return that one. If no valid global default SparkSession exists, the method creates a new SparkSession and assigns the newly created SparkSession as the global default.

In case an existing SparkSession is returned, the config options specified in this builder will be applied to the existing SparkSession.

https://spark.apache.org/docs/2.0.1/api/python/pyspark.sql.html#pyspark.sql.SparkSession.Builder.getOrCreate

到目前为止一切顺利:

from pyspark import SparkConf

SparkConf().toDebugString()
'spark.app.name=pyspark-shell\nspark.master=local[2]\nspark.submit.deployMode=client'

spark.conf.get("spark.app.name")
'pyspark-shell'

然后我重新定义 SparkSession 配置并承诺在 WebUI 中看到更改

appName(name)
Sets a name for the application, which will be shown in the Spark web UI.

https://spark.apache.org/docs/2.0.1/api/python/pyspark.sql.html#pyspark.sql.SparkSession.Builder.appName

c = SparkConf()
(c
 .setAppName("MyApp")
 .setMaster("local")
 .set("spark.driver.memory","1g")
 )

from pyspark.sql import SparkSession
(SparkSession
.builder
.enableHiveSupport() # metastore, serdes, Hive udf
.config(conf=c)
.getOrCreate())

spark.conf.get("spark.app.name")
'MyApp'

现在,当我转到 localhost:4040 时,我希望看到 MyApp 作为应用名称。

不过,我还是看到了pyspark-shell application UI

我哪里错了?

提前致谢!

我认为文档在这里有点误导,当您使用 Scala 时,您实际上会看到这样的警告:

... WARN SparkSession$Builder: Use an existing SparkSession, some configuration may not take effect.

在 Spark 2.0 之前更明显,上下文之间有明确的分离:

  • SparkContext 配置无法在运行时修改。您必须先停止现有上下文。
  • SQLContext 配置可以在运行时修改。

spark.app.name 与许多其他选项一样,绑定到 SparkContext,并且在不停止上下文的情况下无法修改。

重复使用现有的 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

停止现有 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 的警告,但您可以检查它是否确实已停止。

我运行遇到同样的问题,纠结了很久,然后找到一个简单的解决办法:

spark.stop()

然后再次构建新的 sparksession