仍在尝试熟悉maven并将我的源代码编译成jar文件以进行spark-submit。我知道如何使用 IntelliJ 来实现此目的,但想了解它实际上是如何工作的。我有一个 EC2 服务器,其中已经安装了所有最新的软件,例如 Spark 和 scala,并且有示例 SparkPi.scala 源代码,我现在想用 Maven 进行编译。我的愚蠢问题首先是,我是否可以只使用我安装的软件来构建代码,而不是从 Maven 存储库检索依赖项,以及如何从基本的 pom.xml 模板开始添加适当的需求。我不完全理解 Maven 到底在做什么以及如何测试源代码的编译? 据我了解,我只需要有标准的目录结构
src/main/scala
,然后想要运行mvn package
。我也想用 Maven 而不是 sbt 进行测试。
除了@Krishna, 如果您有
mvn project
,请在 mvn clean package
上使用 pom.xml
。确保您的 build
中有以下 pom.xml
来制作 fat-jar
。 (这是我的案例,我是如何制作罐子的)
<build><sourceDirectory>src</sourceDirectory>
<plugins><plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.0</version>
<configuration>
<source>1.7</source>
<target>1.7</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<version>2.4</version>
<configuration>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>
<executions>
<execution>
<id>assemble-all</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin></plugins>
</build>
欲了解更多详情:链接 如果您有
sbt project
,请使用 sbt clean assembly
来制作 fat-jar
。为此,您需要以下配置,例如 build.sbt
中的示例
assemblyJarName := "WordCountSimple.jar"
//
val meta = """META.INF(.)*""".r
assemblyMergeStrategy in assembly := {
case PathList("javax", "servlet", xs@_*) => MergeStrategy.first
case PathList(ps@_*) if ps.last endsWith ".html" => MergeStrategy.first
case n if n.startsWith("reference.conf") => MergeStrategy.concat
case n if n.endsWith(".conf") => MergeStrategy.concat
case meta(_) => MergeStrategy.discard
case x => MergeStrategy.first
}
还有
plugin.sbt
喜欢:
addSbtPlugin("com.eed3si9n" % "sbt-assembly" % "0.13.0")
到目前为止,主要目标是获取具有目标文件夹中所有依赖项的 fat-jar。使用该 jar 在集群中运行,如下所示:
hastimal@nm:/usr/local/spark$ ./bin/spark-submit --class com.hastimal.wordcount --master yarn-cluster --num-executors 15 --executor-memory 52g --executor-cores 7 --driver-memory 52g --driver-cores 7 --conf spark.default.parallelism=105 --conf spark.driver.maxResultSize=4g --conf spark.network.timeout=300 --conf spark.yarn.executor.memoryOverhead=4608 --conf spark.yarn.driver.memoryOverhead=4608 --conf spark.akka.frameSize=1200 --conf spark.io.compression.codec=lz4 --conf spark.rdd.compress=true --conf spark.broadcast.compress=true --conf spark.shuffle.spill.compress=true --conf spark.shuffle.compress=true --conf spark.shuffle.manager=sort /users/hastimal/wordcount.jar inputRDF/data_all.txt /output
这里我有
inputRDF/data_all.txt /output
是两个参数。同样从工具的角度来看,我正在 Intellij
作为 IDE 进行构建。
请按照以下步骤操作
# create assembly jar upon code change
sbt assembly
# transfer the jar to a cluster
scp target/scala-2.10/myproject-version-assembly.jar <some location in your cluster>
# fire spark-submit on your cluster
$SPARK_HOME/bin/spark-submit --class not.memorable.package.applicaiton.class --master yarn --num-executor 10 \
--conf some.crazy.config=xyz --executor-memory=lotsG \
myproject-version-assembly.jar \
<glorious-application-arguments...>
pom.xml
配置更新 Maven maven-jar-plugin
<plugin>
<!-- Build an executable JAR -->
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>3.1.0</version>
<configuration>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<classpathPrefix>lib/</classpathPrefix>
<mainClass>com.spark.Main</mainClass>
</manifest>
</archive>
</configuration>
</plugin>
此配置可确保生成的 JAR 文件包含必要的依赖项并指定主类。2.使用附加 JVM 参数运行 JAR
java -jar --add-opens=java.base/sun.nio.ch=ALL-UNNAMED my-application-jar-with-dependencies.jar
此参数确保运行应用程序时未命名模块可以访问 sun.nio.ch
包。我希望这个解决方案可以帮助其他面临类似问题的人!