spark-submit 找不到 class(而 class 包含在 jar 中)
spark-submit does not find class (while class is being contained in jar)
我正在构建一个非常简单的 HelloWorld Spark 作业,在 Java 中 Gradle:
package com.example;
public class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello World!");
}
}
我的 gradle 配置非常简单:
def sparkVersion = "2.4.6"
def hadoopVersion = "2.7.3"
dependencies {
compile "org.apache.spark:spark-core_2.11:$sparkVersion"
compile "org.apache.spark:spark-sql_2.11:$sparkVersion"
compile 'org.slf4j:slf4j-simple:1.7.9'
compile "org.apache.hadoop:hadoop-aws:$hadoopVersion"
compile "org.apache.hadoop:hadoop-common:$hadoopVersion"
testCompile group: 'junit', name: 'junit', version: '4.12'
}
我还确保构建了一个 far jar 以包含所有依赖项,就像 SBT 程序集在 Scala 中所做的那样:
jar {
zip64 = true
from {
configurations.runtimeClasspath.collect { it.isDirectory() ? it : zipTree(it) }
}
}
构建运行良好,我的 class 出现在 jar 中:
jar tvf build/libs/output.jar | grep -i hello
com/example/HelloWorld.class
但是,当 运行 spark-submit 作业时:
spark-submit --class 'com.example.HelloWorld' --master=local build/libs/output.jar
我得到的只是调试日志:
20/09/21 13:07:46 WARN Utils: Your hostname, example.local resolves to a loopback address: 127.0.0.1; using 192.168.43.208 instead (on interface en0)
20/09/21 13:07:46 WARN Utils: Set SPARK_LOCAL_IP if you need to bind to another address
20/09/21 13:07:46 WARN NativeCodeLoader: Unable to load native-hadoop library for your platform... using builtin-java classes where applicable
log4j:WARN No appenders could be found for logger (org.apache.spark.deploy.SparkSubmit$$anon).
log4j:WARN Please initialize the log4j system properly.
log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.
我的本地 spark 正确地报告了为 Hadoop 2.7.3 构建的 Scala 2.11 和 Spark 2.4.6。
我还测试了一个更复杂的 Spark 作业,但输出日志是相同的。
代码在 IntelliJ Idea 中 运行 很好(选项 Include dependencies with "Provided" scope 勾选)。
我错过了什么吗?非常感谢
问题可能来自 zip64 = true
或 fat jar 生成(尽管 shadowJar 插件也没有解决这个问题)。
我决定改用 Maven 并使用 maven-assembly-plugin
生成 fat jar,maven-compiler-plugin
只包含与我要构建的 Spark 作业相关的某些文件,最后 maven-jar-plugin
以避免构建包含所有 spark 作业的 jar(每个 jar 1 个作业)。
我正在构建一个非常简单的 HelloWorld Spark 作业,在 Java 中 Gradle:
package com.example;
public class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello World!");
}
}
我的 gradle 配置非常简单:
def sparkVersion = "2.4.6"
def hadoopVersion = "2.7.3"
dependencies {
compile "org.apache.spark:spark-core_2.11:$sparkVersion"
compile "org.apache.spark:spark-sql_2.11:$sparkVersion"
compile 'org.slf4j:slf4j-simple:1.7.9'
compile "org.apache.hadoop:hadoop-aws:$hadoopVersion"
compile "org.apache.hadoop:hadoop-common:$hadoopVersion"
testCompile group: 'junit', name: 'junit', version: '4.12'
}
我还确保构建了一个 far jar 以包含所有依赖项,就像 SBT 程序集在 Scala 中所做的那样:
jar {
zip64 = true
from {
configurations.runtimeClasspath.collect { it.isDirectory() ? it : zipTree(it) }
}
}
构建运行良好,我的 class 出现在 jar 中:
jar tvf build/libs/output.jar | grep -i hello
com/example/HelloWorld.class
但是,当 运行 spark-submit 作业时:
spark-submit --class 'com.example.HelloWorld' --master=local build/libs/output.jar
我得到的只是调试日志:
20/09/21 13:07:46 WARN Utils: Your hostname, example.local resolves to a loopback address: 127.0.0.1; using 192.168.43.208 instead (on interface en0)
20/09/21 13:07:46 WARN Utils: Set SPARK_LOCAL_IP if you need to bind to another address
20/09/21 13:07:46 WARN NativeCodeLoader: Unable to load native-hadoop library for your platform... using builtin-java classes where applicable
log4j:WARN No appenders could be found for logger (org.apache.spark.deploy.SparkSubmit$$anon).
log4j:WARN Please initialize the log4j system properly.
log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.
我的本地 spark 正确地报告了为 Hadoop 2.7.3 构建的 Scala 2.11 和 Spark 2.4.6。 我还测试了一个更复杂的 Spark 作业,但输出日志是相同的。
代码在 IntelliJ Idea 中 运行 很好(选项 Include dependencies with "Provided" scope 勾选)。
我错过了什么吗?非常感谢
问题可能来自 zip64 = true
或 fat jar 生成(尽管 shadowJar 插件也没有解决这个问题)。
我决定改用 Maven 并使用 maven-assembly-plugin
生成 fat jar,maven-compiler-plugin
只包含与我要构建的 Spark 作业相关的某些文件,最后 maven-jar-plugin
以避免构建包含所有 spark 作业的 jar(每个 jar 1 个作业)。