作者:书友36110188 | 来源:互联网 | 2022-12-25 19:24
如何解决《线程“主”中的异常java.lang.NoClassDefFoundError:org/apache/spark/streaming/StreamingContext》经验,为你挑选了1个好方法。
大家好,在下面的代码中找不到类StreamingContext。
import org.apache.spark.streaming.{Seconds, StreamingContext}
import org.apache.spark.{SparkConf, SparkContext}
object Exemple {
def main(args: Array[String]): Unit = {
val cOnf= new SparkConf().setMaster("local[*]").setAppName("Exemple")
val sc = new SparkContext(conf)
val ssc = new StreamingContext(sc, Seconds(2)) //this line throws error
}
}
这是错误:
Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/spark/streaming/StreamingContext
at Exemple$.main(Exemple.scala:16)
at Exemple.main(Exemple.scala)
Caused by: java.lang.ClassNotFoundException: org.apache.spark.streaming.StreamingContext
at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:338)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
... 2 more
Process finished with exit code 1
我使用以下build.sbt文件:
name := "exemple"
version := "1.0.0"
scalaVersion := "2.11.11"
// https://mvnrepository.com/artifact/org.apache.spark/spark-sql
libraryDependencies += "org.apache.spark" %% "spark-sql" % "2.2.0"
// https://mvnrepository.com/artifact/org.apache.spark/spark-streaming
libraryDependencies += "org.apache.spark" %% "spark-streaming" % "2.2.0" % "provided"
// https://mvnrepository.com/artifact/org.apache.spark/spark-streaming-kafka-0-10
libraryDependencies += "org.apache.spark" %% "spark-streaming-kafka-0-10" % "2.2.0"
我使用intellij运行按钮运行Exemple类,但出现错误。在sbt shell中可以正常工作。在我的依赖模块中,我可以找到火花依赖。该代码在intellij中编译。而且我可以在外部库中看到火花依赖(在左侧项目面板内部)。你有什么主意吗。看起来并不复杂。
1> Ravikumar..:
请从spark-streaming库中删除提供的内容。
libraryDependencies += "org.apache.spark" %% "spark-streaming" % "2.2.0"
更改后,仍然存在依赖性问题,请排除重复的jar。
"org.apache.spark" %% "spark-streaming-kafka-0-10" % "2.2.0" excludeAll(
ExclusionRule(organization = "org.spark-project.spark", name = "unused"),
ExclusionRule(organization = "org.apache.spark", name = "spark-streaming"),
ExclusionRule(organization = "org.apache.hadoop")
),
希望这可以帮助。
谢谢拉维