Spark流媒体:任务"预测"不可序列化

时间:2015-10-25 09:47:15

标签: scala serialization apache-spark apache-spark-mllib

我正在尝试使用模型进行预测的火花流程序,但是这样做会出错:任务不可序列化。

代码:

val model = sc.objectFile[DecisionTreeModel]("DecisionTreeModel").first() 
val parsedData = reducedData.map { line =>
  val arr = Array(line._2._1,line._2._2,line._2._3,line._2._4,line._2._5,line._2._6,line._2._7,line._2._8,line._2._9,line._2._10,line._2._11)
  val vector = LabeledPoint(line._2._4, Vectors.dense(arr))
  model.predict(vector.features))
}

我粘贴错误:

scala> val parsedData = reducedData.map { line =>
     |     val arr = Array(line._2._1,line._2._2,line._2._3,line._2._4,line._2._5,line._2._6,line._2._7,line._2._8,line._2._9,line._2._10,line._2._11)
     |     val vector=LabeledPoint(line._2._4, Vectors.dense(arr))
     |     model.predict(vector.features)
     | }
org.apache.spark.SparkException: Task not serializable
    at org.apache.spark.util.ClosureCleaner$.ensureSerializable(ClosureCleaner.scala:304)
    at org.apache.spark.util.ClosureCleaner$.org$apache$spark$util$ClosureCleaner$$clean(ClosureCleaner.scala:294)
    at org.apache.spark.util.ClosureCleaner$.clean(ClosureCleaner.scala:122)
    at org.apache.spark.SparkContext.clean(SparkContext.scala:2030)
    at org.apache.spark.streaming.dstream.DStream$$anonfun$map$1.apply(DStream.scala:528)
    at org.apache.spark.streaming.dstream.DStream$$anonfun$map$1.apply(DStream.scala:528)
    at org.apache.spark.rdd.RDDOperationScope$.withScope(RDDOperationScope.scala:147)
    at org.apache.spark.rdd.RDDOperationScope$.withScope(RDDOperationScope.scala:108)
    at org.apache.spark.SparkContext.withScope(SparkContext.scala:709)
    at .......

我该如何解决这个问题?

谢谢!

1 个答案:

答案 0 :(得分:1)

参考此链接: https://databricks.gitbooks.io/databricks-spark-knowledge-base/content/troubleshooting/javaionotserializableexception.html

在你的情况下,"模型"在驱动程序中实例化并在map中使用,导致对象通过网络从驱动程序发送到执行程序,因此它应该是可序列化的。如果您不能使模型可序列化,请尝试通过在map中实例化模型来避免序列化。您可能还需要控制在执行程序中创建此对象的频率 - 每行一次(默认),每个任务一次(即线程)或一次每个遗嘱执行人(即jvm)。

最后,我不认为你可以拥有一个全球性的模型"你可以从多个执行者那里引起变异的对象 - 以防万一你正在寻找的东西(无论你是否需要使其可序列化)。评论欢迎这一点。