使用Spark Cluster中的Spray将JSON反序列化/解析为Scala中的对象AbstractMethodError

时间:2015-12-07 18:18:45

标签: json scala apache-spark spray spray-json

我正在尝试在Spark中进行一些Scala JSON解析。它在我的测试中本地工作,但在远程集群上失败。

相关进口:

import com.fasterxml.jackson.databind.ObjectMapper
import com.fasterxml.jackson.module.scala.DefaultScalaModule
import com.fasterxml.jackson.module.scala.experimental.ScalaObjectMapper
import spray.json._

课程:

case class Item(name: String,
                      siteType: String,
                      productCategoryType: String,
                      variants: List[VariantRange]) extends Serializable
case class VariantRange(name: String, start: Double, end: Double) extends Serializable

case class ItemList(items: List[Item])

object ItemJsonProtocol extends DefaultJsonProtocol {
  implicit val variantRangeFormat = jsonFormat(VariantRange, "name", "start", "end")
  implicit val itemFormat = jsonFormat(Item, "name", "siteType", "productCategoryType", "variants")

  implicit object itemListJsonFormat extends RootJsonFormat[ItemList] {
    override def write(obj: ItemList): JsValue = ???

    override def read(json: JsValue): ItemList = ItemList(json.convertTo[List[Item]])
  }
}

解析代码:

def parseItems(json: String) = {
  val mapper = new ObjectMapper() with ScalaObjectMapper
  mapper.registerModule(DefaultScalaModule)
  val jsonAst = json.parseJson

  import ItemJsonProtocol._
  jsonAst.convertTo[ItemList].items
}

def parseItems(): List[Item] = {
  parseItems(Source.fromInputStream(getClass.getResourceAsStream("/data/items.json")).mkString)
}

此测试在本地通过:

  test("Parses resources file") {
    val items = parseItems()
    items shouldNot be (null)
  }

但是在Spark中它显示了这个:

15/12/07 17:59:05 ERROR ApplicationMaster: User class threw exception: java.lang.AbstractMethodError: com.intentmedia.data.AggregationJob$$anon$1.com$fasterxml$jackson$module$scala$experimental$ScalaObjectMapper$_setter_$com$fasterxml$jackson$module$scala$experimental$ScalaObjectMapper$$typeCache_$eq(Lorg/spark-project/guava/cache/LoadingCache;)V
java.lang.AbstractMethodError: com.intentmedia.data.AggregationJob$$anon$1.com$fasterxml$jackson$module$scala$experimental$ScalaObjectMapper$_setter_$com$fasterxml$jackson$module$scala$experimental$ScalaObjectMapper$$typeCache_$eq(Lorg/spark-project/guava/cache/LoadingCache;)V
    at com.fasterxml.jackson.module.scala.experimental.ScalaObjectMapper$class.$init$(ScalaObjectMapper.scala:50)
    at com.intentmedia.data.AggregationJob$$anon$1.<init>(AggregationJob.scala:76)
    at com.intentmedia.data.AggregationJob$.parseExperiments(AggregationJob.scala:76)
    at com.intentmedia.data.AggregationJob$.parseExperiments(AggregationJob.scala:85)
    at com.intentmedia.data.AggregationJob$.runWorkflow(AggregationJob.scala:66)
    at com.intentmedia.spark.SparkJob$class.main(SparkJob.scala:43)
    at com.intentmedia.data.AggregationJob$.main(AggregationJob.scala:49)
    at com.intentmedia.data.AggregationJob.main(AggregationJob.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:525)
15/12/07 17:59:05 INFO ApplicationMaster: Final app status: FAILED, exitCode: 15, (reason: User class threw exception: java.lang.AbstractMethodError: com.intentmedia.data.AggregationJob$$anon$1.com$fasterxml$jackson$module$scala$experimental$ScalaObjectMapper$_setter_$com$fasterxml$jackson$module$scala$experimental$ScalaObjectMapper$$typeCache_$eq(Lorg/spark-project/guava/cache/LoadingCache;)V)

当我读取对象时,是否需要使用某种缓存?我没有正确处理资源文件吗?

0 个答案:

没有答案