Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/apache-spark/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Scala 如何使用文件格式的更新输出模式?_Scala_Apache Spark_Spark Structured Streaming - Fatal编程技术网

Scala 如何使用文件格式的更新输出模式?

Scala 如何使用文件格式的更新输出模式?,scala,apache-spark,spark-structured-streaming,Scala,Apache Spark,Spark Structured Streaming,我尝试在更新输出模式下使用spark结构化流媒体写入文件。我发现,只要配置了控制台格式,它就可以正常工作。但如果我将输出模式更改为: val query = sessionUpdates .writeStream .outputMode("update") .format("json") .option("path", "/work/output/data") .option("checkpointLocation", "/work/output/checkpoint")

我尝试在更新输出模式下使用spark结构化流媒体写入文件。我发现,只要配置了控制台格式,它就可以正常工作。但如果我将输出模式更改为:

 val query = sessionUpdates
  .writeStream
  .outputMode("update")
  .format("json")
  .option("path", "/work/output/data")
  .option("checkpointLocation", "/work/output/checkpoint")
  .start()
我发现以下错误:

 Exception in thread "main" org.apache.spark.sql.AnalysisException: Data source json does not support Update output mode;
        at org.apache.spark.sql.execution.datasources.DataSource.createSink(DataSource.scala:279)
        at org.apache.spark.sql.streaming.DataStreamWriter.start(DataStreamWriter.scala:286)
        at palyground.StructuredStreamingMergeSpans$.main(StructuredStreamingMergeSpans.scala:84)
        at palyground.StructuredStreamingMergeSpans.main(StructuredStreamingMergeSpans.scala)
我可以使用更新模式并使用FileFormat将结果表写入文件接收器吗?
在源代码中,我找到了一个模式匹配,确保了附加模式。

您无法使用spark结构化流在更新模式下写入文件。你需要为它写
ForeachWriter
。我在这里为每个作家写了简单的文章。您可以根据自己的要求进行修改

val writerForText = new ForeachWriter[Row] {
    var fileWriter: FileWriter = _

    override def process(value: Row): Unit = {
      fileWriter.append(value.toSeq.mkString(","))
    }

    override def close(errorOrNull: Throwable): Unit = {
      fileWriter.close()
    }

    override def open(partitionId: Long, version: Long): Boolean = {
      FileUtils.forceMkdir(new File(s"src/test/resources/${partitionId}"))
      fileWriter = new FileWriter(new File(s"src/test/resources/${partitionId}/temp"))
      true

    }
  }

val query = sessionUpdates
  .writeStream
  .outputMode("update")
  .foreach(writerForText)
  .start()

任何
FileFormat
接收器(包括
json
)都需要
Append
输出模式,这会触发结构化流媒体

在Spark 2.4中,您可以使用
DataStreamWriter.foreach
运算符或全新的
DataStreamWriter.foreachBatch
运算符,该运算符只接受一个函数,该函数接受批次的数据集和批次ID

foreachBatch(function: (Dataset[T], Long) => Unit): DataStreamWriter[T]

谢谢你的快速回复。我只需要将ForeachWriter[行]更改为ForeachWriter[会话更新]。
foreachBatch(function: (Dataset[T], Long) => Unit): DataStreamWriter[T]