首页 > 解决方案 > 迭代数据框时任务不可序列化,scala

问题描述

下面是我的代码,当我尝试遍历每一行时:

val df: DataFrame = sqlContext.read
  .format("com.databricks.spark.csv")
  .option("header", true) // Use first line of all files as header
  .option("delimiter", TILDE)
  .option("inferSchema", "true") // Automatically infer data types
  .load(fileName._2)

val accGrpCountsIds: DataFrame = df.groupBy("accgrpid").count()
LOGGER.info(s"DataFrame Count - ${accGrpCountsIds.count()}")
accGrpCountsIds.show(3)

//switch based on file names and update the model.
accGrpCountsIds.foreach(accGrpRow => {
  val accGrpId = accGrpRow.getLong(0)
  val rowCount = accGrpRow.getInt(1)
}

当我尝试使用上面的数据框进行交互时foreach,我得到一个任务不可序列化错误。我怎样才能做到这一点?

标签: scalaapache-sparkdataframe

解决方案


您的 foreach 中是否还有其他未共享的类型?或者这就是你所做的一切,但它不起作用?

accGrpCountsIds.foreach(accGrpRow => {
  val accGrpId = accGrpRow.getLong(0)
  val rowCount = accGrpRow.getInt(1)
}

另外,您可能会觉得这有用吗? 任务不可序列化:java.io.NotSerializableException 仅在类而非对象上调用闭包外的函数时


推荐阅读