spark 上没有有效的构造函数

no valid constructor on spark

这是我的代码:

class FNNode(val name: String)

case class Ingredient(override val name: String, category: String) extends FNNode(name)


val ingredients: RDD[(VertexId, FNNode)] = 
sc.textFile(PATH+"ingr_info.tsv").
      filter(! _.startsWith("#")).
      map(line => line.split('\t')).
      map(x => (x(0).toInt ,Ingredient(x(1), x(2))))

并且我定义这些变量时没有错误。然而,当试图执行它时:

ingredients.take(1)

我明白了

org.apache.spark.SparkException: Job aborted due to stage failure: Exception while getting task result: java.io.InvalidClassException: $iwC$$iwC$Ingredient; no valid constructor
    at org.apache.spark.scheduler.DAGScheduler.org$apache$spark$scheduler$DAGScheduler$$failJobAndIndependentStages(DAGScheduler.scala:1431)
    at org.apache.spark.scheduler.DAGScheduler$$anonfun$abortStage.apply(DAGScheduler.scala:1419)

根据答案 ,这似乎与序列化问题有关。但是,如果确实是序列化问题,我不知道如何解决。

我正在按照 this 书中的代码进行操作,所以我认为这至少应该在某些时候起作用?

这为我解决了你的问题:

class FNNode(val name: String) extends Serializable