我想使用结构化流媒体将行附加到文本文件。此代码会生成SparkException: Task not serializable
。我认为不允许toDF
。我怎么能让这段代码起作用?
df.writeStream
.foreach(new ForeachWriter[Row] {
override def open(partitionId: Long, version: Long): Boolean = {
true
}
override def process(row: Row): Unit = {
val df = Seq(row.getString(0)).toDF
df.write.format("text").mode("append").save(output)
}
override def close(errorOrNull: Throwable): Unit = {
}
}).start
答案 0 :(得分:1)
您无法在df.write.format("text").mode("append").save(output)
方法中调用process
。它将在执行程序端运行。您可以改用文件接收器,例如
df.writeStream.format("text")....