【问题标题】:Compiling Spark Scala Program into jar file using installed spark and maven使用已安装的 spark 和 maven 将 Spark Scala 程序编译成 jar 文件
【发布时间】:2016-06-20 20:01:17
【问题描述】:

仍在尝试熟悉 maven 并将我的源代码编译成 jar 文件以供 spark-submit。我知道如何为此使用 IntelliJ,但想了解它实际上是如何工作的。我有一个 EC2 服务器,其中已经安装了所有最新软件,例如 spark 和 scala,并且有我现在想用 maven 编译的示例 SparkPi.scala 源代码。我的愚蠢问题首先是,我是否可以只使用我安装的软件来构建代码,而不是从 maven 存储库中检索依赖项,以及如何从基本的 pom.xml 模板开始添加适当的要求。我不完全理解 maven 到底在做什么,我怎样才能测试我的源代码的编译? 据我了解,我只需要拥有标准目录结构src/main/scala,然后想运行mvn package。我也想用 maven 而不是 sbt 进行测试。

【问题讨论】:

  • 取决于您要达到的目标。在本地机器上运行示例或在集群上运行示例。
  • 尝试在 EC2 上的 spark 集群上运行示例。我知道如何使用 IntelliJ 在本地编译,但是在服务器上编译源代码的正确方法是什么。

标签: java scala maven apache-spark


【解决方案1】:

除了@Krishna, 如果您有mvn project,请在pom.xml 上使用mvn clean package。确保您的pom.xml 中有以下build 以生成fat-jar。 (这是我的情况,我是怎么做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>

更多详情:link 如果您有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")

如需了解更多信息,请参阅 thisthis

到目前为止,主要目标是获取包含目标文件夹中所有依赖项的 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 是两个参数。同样从工具的角度来看,我正在 Intellijas IDE 中构建。

【讨论】:

  • 谢谢。所以基本上据我了解。您不需要在 pom.xml 中包含 spark 或 scala 依赖项吗?想了解为什么我有时会在 pom.xml 中看到所有软件依赖项,而不是像您所展示的那样将其排除在外。当您在 IntelliJ 中编码时,您是否只是将 spark 和 scala 作为模块添加,然后运行基本的 maven 构建来创建用于 spark-submit 的 fat-jar?
  • @prometheus2305 简短回答您的问题: 1. 是的,我们需要应用程序所需的所有依赖项,位于build.sbtpom.xml。 2. 我正在制作 Scala-SBT 项目,然后在 build.sbtplugin.sbt 中添加内容。据我所知,这是最简单的方法。使用我上面提到的链接。
  • 还是有点糊涂。如果我只需要将项目打包到 jar 中以便在已经有 spark 的单独远程集群上的 spark-submit 上运行它,我需要在 pom.xml 中显式添加 spark 和 scala 以将这些依赖项包含在 jar 中还是我只是只需要一个最小的 maven 来编译和创建一个 jar 文件吗?
  • 是的,复制pom.xml 如果你已经从其他任何项目中复制,如果你正在制作新项目,然后从头开始制作并在pom.xml 中添加依赖项,然后如果你有代码存在然后把它代码在src。如果您没有代码,请为新项目编写代码。我的意思是让fat-jar 你需要添加这些build。说得通?如果有问题请告诉我。
【解决方案2】:

请按以下步骤操作

# 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...>

【讨论】:

    猜你喜欢
    • 2017-08-22
    • 2017-06-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-04-19
    • 2019-03-01
    • 1970-01-01
    • 2018-05-09
    相关资源
    最近更新 更多