java 如何使用 spark-submit 将参数/属性传递给 Spark 作业

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/40535304/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-03 05:18:02  来源:igfitidea点击:

How to pass parameters / properties to Spark jobs with spark-submit

javaapache-sparkcommand-line

提问by Michael Lihs

I am running a Spark job implemented in Java using spark-submit. I would like to pass parameters to this job - e.g. a time-startand time-endparameter to parametrize the Spark application.

我正在运行使用 Java 实现的 Spark 作业spark-submit。我想将参数传递给这项工作 - 例如一个time-starttime-end参数来参数化 Spark 应用程序。

What I tried was using the

我试过的是使用

--conf key=value

option of the spark-submitscript, but when I try to read the parameter in my Spark job with

spark-submit脚本的选项,但是当我尝试读取 Spark 作业中的参数时

sparkContext.getConf().get("key")

I get an exception:

我得到一个例外:

Exception in thread "main" java.util.NoSuchElementException: key

Furthermore, when I use sparkContext.getConf().toDebugString()I don't see my value in the output.

此外,当我使用时,我sparkContext.getConf().toDebugString()在输出中看不到我的价值。

Further NoticeSince I want to submit my Spark Job via the Spark REST Service I cannot use an OS Environment Variable or the like.

进一步通知由于我想通过 Spark REST 服务提交我的 Spark 作业,我不能使用操作系统环境变量等。

Is there any possibility to implement this?

有没有可能实现这个?

采纳答案by VladoDemcak

Since you want to use your custom properties you need to place your properties after application.jarin spark-submit(like in spark example [application-arguments]should be your properties. --confshould be spark configuration properties.

由于您想使用自定义属性,因此您需要将属性application.jar放在 in之后spark-submit(就像在 spark 示例中[application-arguments]应该是您的属性。--conf应该是 spark 配置属性。

--conf: Arbitrary Spark configuration propertyin key=value format. For values that contain spaces wrap “key=value” in quotes (as shown).

--conf:key=value 格式的任意Spark 配置属性。对于包含空格的值,将“key=value”用引号括起来(如图所示)。

./bin/spark-submit \
  --class <main-class> \
  --master <master-url> \
  --deploy-mode <deploy-mode> \
  --conf <key>=<value> \
  ... # options
  <application-jar> \
  [application-arguments] <--- here our app arguments

so when you do: spark-submit .... app.jar key=valuein mainmethod you will get args[0]as key=value.

所以,当你这样做:spark-submit .... app.jar key=valuemain方法你会得到args[0]key=value

public static void main(String[] args) {
    String firstArg = args[0]; //eq. to key=value
}

but you want to use key valuepairs you need to parse somehow your app arguments.

但是你想使用key value你需要以某种方式解析你的app arguments.

You can check Apache Commons CLI libraryor some alternative.

您可以检查Apache Commons CLI 库或其他替代方法。

回答by VladoDemcak

Spark configuration will use only keys in the sparknamespace. If you don't won't to use independent configuration tool you can try:

Spark 配置将仅使用spark命名空间中的键。如果您不想使用独立的配置工具,您可以尝试:

--conf spark.mynamespace.key=value

回答by Li Rao

You can pass parameters like this:

您可以像这样传递参数:

./bin/spark-submit \
  --class $classname \
  --master XXX \
  --deploy-mode XXX \
  --conf XXX \
  $application-jar --**key1** $**value** --**key2** $**value2**\

Make sure to replace key1, key2and valuewith proper values.

确保替换key1,key2value使用正确的值。