【问题标题】:Using sbt to package scala source code in cmd : error missing 'SparkContext.class'使用 sbt 在 cmd 中打包 scala 源代码:错误缺少'SparkContext.class'
【发布时间】:2017-10-14 23:36:34
【问题描述】:

我是spark新手,关注tutorial学习。我已经安装 openjdk 版本“1.8.0_121”(web-binary) Hadoop 2.8.0(网络二进制) 斯卡拉版本 2.11.8 (apt) 和 spark 版本 2.1.1(web-binary-pre-build-with-hadoop 2.6.0 或更高版本)。

我运行了 SparkPi 示例并成功。但是,当我尝试使用通过org said 安装的sbt 0.13.15(apt) 打包我的第一个spark 应用程序时,出现了一些错误。

我知道某处的设置一定是错误的,但在this link 中找不到。谁能帮助我?谢谢:)

我的项目是这样的:

---SparkApp
  |---simple.sbt
  |---src
      |---main
          |---scala
              |--- SimpleApp.scala

我项目中的点sbt文件是:

name := "Simple Project"

version := "0.13.15"

scalaVersion := "2.11.8"

libraryDependencies += "org.apache.spark" %% "spark-core" % "2.1.1"

错误日志是这样的:

hadoop@master:~/Mycode/SparkApp$ sbt package
[warn] Executing in batch mode.
[warn]   For better performance, hit [ENTER] to switch to interactive mode, or
[warn]   consider launching sbt without any commands, or explicitly passing 'shell'
[info] Loading project definition from /home/hadoop/Mycode/SparkApp/project
[info] Set current project to Simple Project (in build file:/home/hadoop/Mycode/SparkApp/)
[info] Compiling 1 Scala source to /home/hadoop/Mycode/SparkApp/target/scala-2.11/classes...
[error] missing or invalid dependency detected while loading class file 'SparkContext.class'.
[error] Could not access term akka in package <root>,
[error] because it (or its dependencies) are missing. Check your build definition for
[error] missing or conflicting dependencies. (Re-run with `-Ylog-classpath` to see the problematic classpath.)
[error] A full rebuild may help if 'SparkContext.class' was compiled against an incompatible version of <root>.
[error] one error found
[error] (compile:compileIncremental) Compilation failed
[error] Total time: 2 s, completed May 16, 2017 1:08:53 PM

一些提示可能是问题所在:

  1. 当我输入 spark-shell 时,我得到了这个 Using Scala version 2.11.8 (Java HotSpot(TM) 64-Bit Server VM, Java 1.8.0_131) ,这与我输入时不同输入 java -version openjdk 版本“1.8.0_121”。这会是问题吗?
  2. 安装 sbt 后我什么也没做。我应该为设置做点什么吗?比如让 sbt 知道我的 scala 和 spark 的位置。如何?
  3. 我没有 maven,是吗?

------------ 第二次编辑 -------

在dot sbt 文件中添加-Ylog-classpath 后,如this link 所说。我得到了一个很长的类路径打印出来,太长了,无法在此处显示。 问题尚未解决

如前所述,我提供 SimpleApp.scala :

/* SimpleApp.scala */
import org.apache.spark.SparkContext
import org.apache.spark.SparkContext._
import org.apache.spark.SparkConf

object SimpleApp {
  def main(args: Array[String]) {
    val logFile = "file:///usr/local/spark/README.md" // Should be some file on your system
    val conf = new SparkConf().setAppName("Simple Application")
    val sc = new SparkContext(conf)
    val logData = sc.textFile(logFile, 2).cache()
    val numAs = logData.filter(line => line.contains("a")).count()
    val numBs = logData.filter(line => line.contains("b")).count()
    println("Lines with a: %s, Lines with b: %s".format(numAs, numBs))
  }
}

【问题讨论】:

  • @Rahul 我应该尝试使用 Intellij 吗?
  • 你可以试试。在 Spark 和 Scala 开发方面,intelliJ 是最好的 IDE,而且插件随时可用。
  • 另外,您能否在 build.sbt 中添加 %provided a end 您的依赖项并尝试? libraryDependencies += "org.apache.spark" %% "spark-core" % "2.1.1" % "provided"
  • @Rahul 感谢您的建议,我的问题已经解决。我仍然在我的代码中使用了你的解决方案。

标签: scala apache-spark sbt


【解决方案1】:

tl;dr如果您想开发 Spark 应用程序,您不必安装 Spark。

在您作为 Spark 开发人员的早期(使用 spark-shellspark-submit 等工具)在本地安装 Spark 确实有很大帮助,但不需要强烈推荐。

换句话说,您作为 Spark 包安装的内容与您在开发 Spark 应用程序时可以使用和想要使用的内容无关。

在 sbt 管理的 Scala 项目中,您可以在 libraryDependencies 设置中定义要用作依赖项的内容,包括 Spark 依赖项,如下所示:

libraryDependencies += "org.apache.spark" %% "spark-core" % "2.1.1"

令我惊讶的是,你做到了。

似乎您使用两个不同的项目目录来解释您在做什么~/Mycode/SparkApp(您在其中执行sbt package)和---Pro(其中您显示build.sbt )。

假设您的 simple.sbt 如下所示:

name := "Simple Project"

version := "0.13.15"

scalaVersion := "2.11.8"

libraryDependencies += "org.apache.spark" %% "spark-core" % "2.1.1"

我可以找到一个只有的问题,即version 设置我相信0.13.15 以反映sbt 的版本。

请注意,它们以任何方式相关,version 是您的应用程序的版本,而项目中使用的 sbt 版本在 project/build.properties 中定义(鉴于sbt 0.13.15 的最新版本)应该如下:

sbt.version = 0.13.15

您在执行sbt package(在/home/hadoop/Mycode/SparkApp)时遇到的问题是您的应用程序定义了对Akka 的依赖,正如您在错误消息中看到的那样:

[info] Set current project to Simple Project (in build file:/home/hadoop/Mycode/SparkApp/)
[info] Compiling 1 Scala source to /home/hadoop/Mycode/SparkApp/target/scala-2.11/classes...
[error] missing or invalid dependency detected while loading class file 'SparkContext.class'.
[error] Could not access term akka in package <root>,
[error] because it (or its dependencies) are missing. Check your build definition for
[error] missing or conflicting dependencies. (Re-run with `-Ylog-classpath` to see the problematic classpath.)
[error] A full rebuild may help if 'SparkContext.class' was compiled against an incompatible version of <root>.
[error] one error found
[error] (compile:compileIncremental) Compilation failed

从 Spark 1.6 左右开始,Spark 不再使用 Akka,所以我项目以某种方式引用了 Akka 库,如果它们用于火花。

希望我们会很快解决很多猜测。

【讨论】:

    【解决方案2】:

    感谢大家的关注。我的问题刚刚通过删除由一次不成功操作生成的 /project 和 /target 文件夹解决。造成这种情况的原始陷阱仍然未知。但是这个结局对我来说已经足够了。再次感谢。 :)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-03-08
      • 2014-08-23
      • 1970-01-01
      • 1970-01-01
      • 2011-07-07
      • 1970-01-01
      • 1970-01-01
      • 2013-03-14
      相关资源
      最近更新 更多