我有一个简单的Spark应用程序,包含Scala和SBT。首先,我尝试执行以下操作:
运行sbt clean package
运行spark-submit --class Main ./target/scala-2.11/sparktest_2.11-1.0.jar
但它失败并出现以下异常:
Exception in thread "main" java.lang.NoClassDefFoundError: com/fasterxml/jackson/module/scala/DefaultScalaModule$
然后我尝试了SBT的汇编插件,但我得到了以下异常:
java.lang.NoSuchMethodError: com.fasterxml.jackson.databind.introspect.POJOPropertyBuilder.addField(Lcom/fasterxml/jackson/databind/introspect/AnnotatedField;Lcom/fasterxml/jackson/databind/PropertyName;ZZZ)V
正如我所看到的,所有内容都与Jackson lib和Scala支持相关。也许这是与库版本相关的一些问题?
我的build.sbt看起来像这样:
name := "SparkTest"
version := "1.0"
scalaVersion := "2.11.4"
scalacOptions := Seq("-unchecked", "-deprecation", "-encoding", "utf8", "-feature")
libraryDependencies ++= {
Seq(
"org.apache.spark" %% "spark-core" % "1.2.1" % "provided",
"com.fasterxml.jackson.core" % "jackson-core" % "2.4.1",
"com.fasterxml.jackson.core" % "jackson-databind" % "2.4.1",
"com.fasterxml.jackson.module" %% "jackson-module-scala" % "2.4.1"
)
}
我的应用程序代码就是这样:
import com.fasterxml.jackson.databind.{DeserializationFeature, ObjectMapper}
import com.fasterxml.jackson.module.scala.DefaultScalaModule
import org.apache.spark.{SparkConf, SparkContext}
trait JsonUtil {
val mapper = new ObjectMapper()
mapper.registerModule(DefaultScalaModule)
mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false)
}
case class Person(name: String)
object Main extends JsonUtil {
def main(args: Array[String]) {
val conf = new SparkConf().setAppName("Spark Test App")
val sc = new SparkContext(conf)
val inputFile = "/home/user/data/person.json"
val input = sc.textFile(inputFile)
val persons = input.flatMap { line ⇒ {
try {
println(s" [DEBUG] trying to parse '$line'")
Some(mapper.readValue(line, classOf[Person]))
} catch {
case e : Exception ⇒
println(s" [EXCEPTION] ${e.getMessage}")
None
}
}}
println("PERSON LIST:")
for (p ← persons) {
println(s" $p")
}
println("END")
}
}
编辑:问题似乎与Spark应用程序有关。如果我运行简单的应用程序只是为了测试JSON unmarshalling一切正常。但是,如果我尝试从Spark应用程序执行相同操作,则问题将如上所述出现。有什么想法吗?