在Spark3中无法通过spark.conf.set方法设置配置
2022-01-27 16:18:04.0
问题描述
在Spark 3中,当我们想要通过spark.conf.set方法设置配置参数时,如下:
spark.conf.set("spark.executor.cores", "2")
这时,会出现如下错误信息:
Scala:
org.apache.spark.sql.AnalysisException: Cannot modify the value of a Spark config: spark.executor.cores
Python:
AnalysisException: Cannot modify the value of a Spark config: spark.executor.cores
问题分析
从Spark 3 开始,不允许通过SparkSession.sparkContext来修改配置。
解决方法
将spark.sql.legacy.setCommandRejectsSparkCoreConfs参数选项设为flase(或False),就可以使用Spark 2中的spark.conf.set方法来设置配置项了。
Scala:
spark.conf.set("spark.sql.legacy.setCommandRejectsSparkCoreConfs", false)
Python:
spark.conf.set("spark.sql.legacy.setCommandRejectsSparkCoreConfs", False)
然后再执行以下代码,一切OK!
spark.conf.set("spark.executor.cores", "2") spark.conf.set("spark.executor.memory", "4g") spark.conf.set("spark.driver.host", "myhost")