Spark任务不可序列化

时间:2017-10-25 13:37:02

标签: scala apache-spark exception serialization serializable

我已经尝试了在StackOverflow上找到的这个问题的所有解决方案但是,尽管如此,我还是无法解决它。 我有一个“MainObj”对象,它实例化一个“Recommendation”对象。当我调用“recommendedProducts”方法时,我总是会收到错误。 以下是该方法的代码:

def recommendationProducts(item: Int): Unit = {

val aMatrix = new DoubleMatrix(Array(1.0, 2.0, 3.0))

def cosineSimilarity(vec1: DoubleMatrix, vec2: DoubleMatrix): Double = {
  vec1.dot(vec2) / (vec1.norm2() * vec2.norm2())
}

val itemFactor = model.productFeatures.lookup(item).head
val itemVector = new DoubleMatrix(itemFactor)

//Here is where I get the error:
val sims = model.productFeatures.map { case (id, factor) =>
  val factorVector = new DoubleMatrix(factor)
  val sim = cosineSimilarity(factorVector, itemVector)
  (id, sim)
}

val sortedSims = sims.top(10)(Ordering.by[(Int, Double), Double] {
  case (id, similarity) => similarity
})

println("\nTop 10 products:")
sortedSims.map(x => (x._1, x._2)).foreach(println)

这是错误:

Exception in thread "main" org.apache.spark.SparkException: Task not serializable
at org.apache.spark.util.ClosureCleaner$.ensureSerializable(ClosureCleaner.scala:298)
at org.apache.spark.util.ClosureCleaner$.org$apache$spark$util$ClosureCleaner$$clean(ClosureCleaner.scala:288)
at org.apache.spark.util.ClosureCleaner$.clean(ClosureCleaner.scala:108)
at org.apache.spark.SparkContext.clean(SparkContext.scala:2094)
at org.apache.spark.rdd.RDD$$anonfun$map$1.apply(RDD.scala:370)
at org.apache.spark.rdd.RDD$$anonfun$map$1.apply(RDD.scala:369)
at org.apache.spark.rdd.RDDOperationScope$.withScope(RDDOperationScope.scala:151)
at org.apache.spark.rdd.RDDOperationScope$.withScope(RDDOperationScope.scala:112)
at org.apache.spark.rdd.RDD.withScope(RDD.scala:362)
at org.apache.spark.rdd.RDD.map(RDD.scala:369)
at RecommendationObj.recommendationProducts(RecommendationObj.scala:269)
at MainObj$.analisiIUNGO(MainObj.scala:257)
at MainObj$.menu(MainObj.scala:54)
at MainObj$.main(MainObj.scala:37)
at MainObj.main(MainObj.scala)
Caused by: java.io.NotSerializableException: org.apache.spark.SparkContext
Serialization stack:
- object not serializable (class: org.apache.spark.SparkContext, value: org.apache.spark.SparkContext@7c2312fa)
- field (class: RecommendationObj, name: sc, type: class org.apache.spark.SparkContext)
- object (class MainObj$$anon$1, MainObj$$anon$1@615bad16)
- field (class: RecommendationObj$$anonfun$37, name: $outer, type: class RecommendationObj)
- object (class RecommendationObj$$anonfun$37, <function1>)
at org.apache.spark.serializer.SerializationDebugger$.improveException(SerializationDebugger.scala:40)
at org.apache.spark.serializer.JavaSerializationStream.writeObject(JavaSerializer.scala:46)
at org.apache.spark.serializer.JavaSerializerInstance.serialize(JavaSerializer.scala:100)
at org.apache.spark.util.ClosureCleaner$.ensureSerializable(ClosureCleaner.scala:295)
... 14 more

我尝试添加: 1)“将Serializable”(Scala)扩展到我的班级 2)“extends extends java.io.Serializable”到我的Class 3)“@transient”到某些部分 4)在这个类中获取模型(和其他特性)(现在我从另一个对象中获取它们,然后将它们传递给我的类,就像参数一样)

我该如何解决?我变得疯了! 提前谢谢!

1 个答案:

答案 0 :(得分:4)

关键在这里:

 field (class: RecommendationObj, name: sc, type: class org.apache.spark.SparkContext)

所以你有一个名为sc的类型为SparkContext的字段。 Spark想要序列化类,所以他也尝试序列化所有字段。

你应该:

  • 使用@transient注释并检查是否为null,然后重新创建
  • 不使用字段中的SparkContext,而是将其放入方法的参数中。但请记住,你永远不应该在map,flatMap等中使用SparkContext里面的闭包。