在 CLI 中定义 Spark Master 与在 Spark 应用程序代码中定义 'master' 有什么区别?

What is the difference between defining Spark Master in the CLI vs defining 'master' in the Spark application code?

CLI 中定义的 Spark-submit "--master" 和 spark 应用程序代码定义 master 有什么区别?

在 Spark 中,我们可以在如下任一应用程序代码中指定主 URI:

或者我们可以将 spark-submit 中的 master URI 指定为参数的参数,如下所示:

一个优先于另一个吗?他们是否必须在合同上达成一致,所以我在程序 spark-submit 和 spark 应用程序代码中引用了相同 URI 的两个实例,创建了 SparkSession?一个会覆盖另一个吗? SparkSession 对 master 参数的处理方式有何不同,spark-submit master 参数的处理方式有何不同?

如有任何帮助,我们将不胜感激。谢谢!

引用the official documentation

The spark-submit script can load default Spark configuration values from a properties file and pass them on to your application. By default, it will read options from conf/spark-defaults.conf in the Spark directory. For more detail, see the section on loading default configurations.

Loading default Spark configurations this way can obviate the need for certain flags to spark-submit. For instance, if the spark.master property is set, you can safely omit the --master flag from spark-submit. In general, configuration values explicitly set on a SparkConf take the highest precedence, then flags passed to spark-submit, then values in the defaults file.

If you are ever unclear where configuration options are coming from, you can print out fine-grained debugging information by running spark-submit with the --verbose option.

所以所有选项都是有效的,并且如果在多个位置设置相同的选项,则有一个明确定义的层次结构定义优先级。从最高到最低:

  • 应用程序中的显式设置。
  • 命令行参数。
  • 来自配置文件的选项。

来自 Spark 文档:

一般来说,

  • 在 SparkConf 上显式设置的配置值具有最高优先级,
  • 然后标志传递给 spark-submit,
  • 然后是默认文件中的值。

我觉得最灵活的方法是将标志传递给 spark-submit。