【发布时间】:2017-11-05 21:58:19
【问题描述】:
我对应该使用哪个 scala 感到困惑。运行 spark-submit 应用程序时出现此错误:
17/06/05 06:59:46 ERROR yarn.ApplicationMaster: User class threw
exception: java.lang.NoSuchMethodError: scala.reflect.api.JavaUniverse.runtimeMirror(Ljava/lang/ClassLoader;)Lscala/reflect/api/JavaMirrors$JavaMirror;
java.lang.NoSuchMethodError: scala.reflect.api.JavaUniverse.runtimeMirror(Ljava/lang/ClassLoader;)Lscala/reflect/api/JavaMirrors$JavaMirror;
at com.xxx.push_up.App$.main(App.scala:255)
at com.xxx.push_up.App.main(App.scala)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at org.apache.spark.deploy.yarn.ApplicationMaster$$anon$2.run(ApplicationMaster.scala:637)
我发现这是因为 scala 在编译和执行之间不兼容。我使用此代码检查运行时 scala 版本:
println("SparkContext version: "+ sc.version)
println("Scala version: "+ scala.tools.nsc.Properties.versionString)
输出是:
SparkContext version: 2.1.1
Scala version: version 2.11.8
我的 pom.xml 是:
...
<build>
<plugins>
<plugin>
<groupId>net.alchim31.maven</groupId>
<artifactId>scala-maven-plugin</artifactId>
<version>3.1.6</version>
<configuration>
<scalaCompatVersion>2.11.8</scalaCompatVersion>
<scalaVersion>2.11.8</scalaVersion>
</configuration>
<executions>
<execution>
<phase>compile</phase>
<goals>
<goal>compile</goal>
<goal>testCompile</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.3</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>
<dependencies>
<dependency> <!-- Spark dependency -->
<groupId>org.apache.spark</groupId>
<artifactId>spark-core_2.11</artifactId>
<version>2.1.1</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.apache.spark</groupId>
<artifactId>spark-sql_2.11</artifactId>
<version>2.1.1</version>
</dependency>
<dependency>
<groupId>org.apache.spark</groupId>
<artifactId>spark-hive_2.11</artifactId>
<version>2.1.1</version>
</dependency>
</dependences>
....
我不知道出了什么问题。谢谢。
App.scala:255 中的错误代码:
val gcm_log_df = sqlContext.createDataFrame(gcm_log_raw_rdd.filter(_.length == 12), gcm_log_raw_schema).filter("pid != 'unknown'").select("pid","channel")
这是我第一次使用sqlContext,我认为它会引发问题。
我加了scala依赖,还是一样的问题:
<dependency>
<groupId>org.scala-lang</groupId>
<artifactId>scala-library</artifactId>
<version>2.11.8</version>
</dependency>
【问题讨论】:
-
异常发生在 App.scala 第 25 行。你能用 -+ 5 行代码在这个位置发布你的代码内容吗?
-
你是否在 pom 文件中包含了 scala 库依赖项?我猜你忘记了。
-
@RameshMaharjan 我添加了依赖,仍然是同样的问题。我将程序从 1.6.1 切换到 spark 2.1.1,因为在 1.6.1 中使用 collect_list 存在问题,在该项目中,我不包含 scala 依赖,可以正常运行其余代码。
-
NoSuchMethodError 主要是缺少必要的库或版本冲突。我没有在您的 pom 文件中看到 scala 库依赖项。尝试包含
<dependency> <groupId>org.scala-lang</groupId> <artifactId>scala-library</artifactId> <version>2.11.8</version> </dependency>,如果错误仍然存在,那么您将不得不检查项目中是否存在冲突的 scala 库。 -
您是否也升级了 Spark 集群?它在云端运行吗?
标签: scala maven apache-spark apache-spark-sql