真的......它已经被讨论了很多。
然而,有很多含糊之处,并且提供了一些答案……包括在 jars/executor/driver 配置或选项中复制 jar 引用。
模棱两可和/或遗漏的细节
应为每个选项澄清以下模棱两可、不清楚和/或遗漏的细节:
它影响的选项:
--jars
SparkContext.addJar(...)
方法 SparkContext.addFile(...)
方法 --conf spark.driver.extraClassPath=...
或 --driver-class-path ...
--conf spark.driver.extraLibraryPath=...
, 或 --driver-library-path ...
--conf spark.executor.extraClassPath=...
--conf spark.executor.extraLibraryPath=...
我知道在哪里可以找到 main spark documentation ,特别是关于 how to submit , options可用,还有 JavaDoc .然而,这给我留下了很多漏洞,尽管它也部分回答了。
我希望事情没有那么复杂,有人能给我一个清晰简洁的答案。
如果我从文档中猜测,似乎
--jars
,以及 SparkContext
addJar
和 addFile
方法是将自动分发文件的方法,而其他选项仅修改 ClassPath。为简单起见,可以安全地假设我可以同时使用 3 个主要选项添加其他应用程序 jar 文件:
spark-submit --jar additional1.jar,additional2.jar \
--driver-library-path additional1.jar:additional2.jar \
--conf spark.executor.extraLibraryPath=additional1.jar:additional2.jar \
--class MyClass main-application.jar
在 an answer to another posting 上找到了一篇不错的文章.然而,没有学到新东西。海报确实很好地说明了本地驱动程序(yarn-client)和远程驱动程序(yarn-cluster)之间的区别。牢记这一点绝对重要。
最佳答案
类路径:
ClassPath 会受到影响,具体取决于您提供的内容。有几种方法可以在类路径上设置一些东西:
spark.driver.extraClassPath
或者它的别名 --driver-class-path
在运行驱动程序的节点上设置额外的类路径。 spark.executor.extraClassPath
在 Worker 节点上设置额外的类路径。 如果您希望某个 JAR 对 Master 和 Worker 都有效,则必须在 BOTH 标志中分别指定它们。
分隔符:
Following the same rules as the JVM :
:
--conf "spark.driver.extraClassPath=/opt/prog/hadoop-aws-2.7.1.jar:/opt/prog/aws-java-sdk-1.10.50.jar"
;
--conf "spark.driver.extraClassPath=/opt/prog/hadoop-aws-2.7.1.jar;/opt/prog/aws-java-sdk-1.10.50.jar"
文件分发:
这取决于您运行工作的模式:
16/05/08 17:29:12 INFO HttpFileServer: HTTP File server directory is /tmp/spark-48911afa-db63-4ffc-a298-015e8b96bc55/httpd-84ae312b-5863-4f4c-a1ea-537bfca2bc2b
16/05/08 17:29:12 INFO HttpServer: Starting HTTP Server
16/05/08 17:29:12 INFO Utils: Successfully started service 'HTTP file server' on port 58922.
16/05/08 17:29:12 INFO SparkContext: Added JAR /opt/foo.jar at http://***:58922/jars/com.mycode.jar with timestamp 1462728552732
16/05/08 17:29:12 INFO SparkContext: Added JAR /opt/aws-java-sdk-1.10.50.jar at http://***:58922/jars/aws-java-sdk-1.10.50.jar with timestamp 1462728552767
接受的文件 URI
在 "Submitting Applications" ,Spark 文档很好地解释了文件的可接受前缀:
When using spark-submit, the application jar along with any jars included with the --jars option will be automatically transferred to the cluster. Spark uses the following URL scheme to allow different strategies for disseminating jars:
- file: - Absolute paths and file:/ URIs are served by the driver’s HTTP file server, and every executor pulls the file from the driver HTTP server.
- hdfs:, http:, https:, ftp: - these pull down files and JARs from the URI as expected
- local: - a URI starting with local:/ is expected to exist as a local file on each worker node. This means that no network IO will be incurred, and works well for large files/JARs that are pushed to each worker, or shared via NFS, GlusterFS, etc.
Note that JARs and files are copied to the working directory for each SparkContext on the executor nodes.
如前所述,JAR 被复制到每个 Worker 节点的工作目录中。那究竟是哪里?通常在
/var/run/spark/work
下,你会看到它们是这样的:drwxr-xr-x 3 spark spark 4096 May 15 06:16 app-20160515061614-0027
drwxr-xr-x 3 spark spark 4096 May 15 07:04 app-20160515070442-0028
drwxr-xr-x 3 spark spark 4096 May 15 07:18 app-20160515071819-0029
drwxr-xr-x 3 spark spark 4096 May 15 07:38 app-20160515073852-0030
drwxr-xr-x 3 spark spark 4096 May 15 08:13 app-20160515081350-0031
drwxr-xr-x 3 spark spark 4096 May 18 17:20 app-20160518172020-0032
drwxr-xr-x 3 spark spark 4096 May 18 17:20 app-20160518172045-0033
当您查看内部时,您会看到您部署的所有 JAR:
[*@*]$ cd /var/run/spark/work/app-20160508173423-0014/1/
[*@*]$ ll
total 89988
-rwxr-xr-x 1 spark spark 801117 May 8 17:34 awscala_2.10-0.5.5.jar
-rwxr-xr-x 1 spark spark 29558264 May 8 17:34 aws-java-sdk-1.10.50.jar
-rwxr-xr-x 1 spark spark 59466931 May 8 17:34 com.mycode.code.jar
-rwxr-xr-x 1 spark spark 2308517 May 8 17:34 guava-19.0.jar
-rw-r--r-- 1 spark spark 457 May 8 17:34 stderr
-rw-r--r-- 1 spark spark 0 May 8 17:34 stdout
受影响的选项:
最重要的是要了解优先级 .如果您通过代码传递任何属性,它将优先于您通过
spark-submit
指定的任何选项.这在 Spark 文档中提到:Any values specified as flags or in the properties file will be passed on to the application and merged with those specified through SparkConf. Properties set directly on the SparkConf take highest precedence, then flags passed to spark-submit or spark-shell, then options in the spark-defaults.conf file
因此,请确保将这些值设置在正确的位置,这样当一个优先于另一个时您就不会感到惊讶。
让我们分析每个有问题的选项:
--jars
对比 SparkContext.addJar
: 这些都是一样的,只有一个是通过spark提交设置的,一个是通过代码设置的。选择更适合您的那一款。需要注意的一件重要事情是使用这些选项中的任何一个 不会将 JAR 添加到您的驱动程序/执行程序类路径 ,您需要使用 extraClassPath
显式添加它们配置两者。 SparkContext.addJar
对比 SparkContext.addFile
: 当您有 时使用前者依赖 需要与您的代码一起使用。当您只想将任意文件传递到工作节点时使用后者,这不是代码中的运行时依赖项。 --conf spark.driver.extraClassPath=...
或 --driver-class-path
: 这些都是别名,选哪个都没有关系--conf spark.driver.extraLibraryPath=..., or --driver-library-path ...
同上,别名。 --conf spark.executor.extraClassPath=...
:当您有一个无法包含在 uber JAR 中的依赖项(例如,因为库版本之间存在编译时冲突)并且您需要在运行时加载时使用此选项。 --conf spark.executor.extraLibraryPath=...
这作为 java.library.path
传递JVM 的选项。当您需要对 JVM 可见的库路径时使用此选项。 Would it be safe to assume that for simplicity, I can add additional application jar files using the 3 main options at the same time:
您可以安全地假设这仅适用于客户端模式,而不是集群模式。正如我之前所说的。此外,你给出的例子有一些多余的论点。例如,将 JAR 传递给
--driver-library-path
没用,需要传给extraClassPath
如果您希望它们在您的类路径上。最终,当您在驱动程序和工作线程上部署外部 JAR 时,您想要做的是:spark-submit --jars additional1.jar,additional2.jar \
--driver-class-path additional1.jar:additional2.jar \
--conf spark.executor.extraClassPath=additional1.jar:additional2.jar \
--class MyClass main-application.jar
关于java - 将 jars 添加到 Spark 作业 - spark-submit,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37132559/