为什么完整输出模式需要聚合?

Jac*_*ski 14 apache-spark spark-structured-streaming

我在Apache Spark 2.2中使用最新的结构化流,并得到以下异常:

org.apache.spark.sql.AnalysisException:当流数据框架/数据集上没有流聚合时,不支持完整输出模式;;

为什么完整输出模式需要流聚合?如果Spark允许在流式查询中没有聚合的完整输出模式,会发生什么?

scala> spark.version
res0: String = 2.2.0

import org.apache.spark.sql.execution.streaming.MemoryStream
import org.apache.spark.sql.SQLContext
implicit val sqlContext: SQLContext = spark.sqlContext
val source = MemoryStream[(Int, Int)]
val ids = source.toDS.toDF("time", "id").
  withColumn("time", $"time" cast "timestamp"). // <-- convert time column from Int to Timestamp
  dropDuplicates("id").
  withColumn("time", $"time" cast "long")  // <-- convert time column back from Timestamp to Int

import org.apache.spark.sql.streaming.{OutputMode, Trigger}
import scala.concurrent.duration._
scala> val q = ids.
     |   writeStream.
     |   format("memory").
     |   queryName("dups").
     |   outputMode(OutputMode.Complete).  // <-- memory sink supports checkpointing for Complete output mode only
     |   trigger(Trigger.ProcessingTime(30.seconds)).
     |   option("checkpointLocation", "checkpoint-dir"). // <-- use checkpointing to save state between restarts
     |   start
org.apache.spark.sql.AnalysisException: Complete output mode not supported when there are no streaming aggregations on streaming DataFrames/Datasets;;
Project [cast(time#10 as bigint) AS time#15L, id#6]
+- Deduplicate [id#6], true
   +- Project [cast(time#5 as timestamp) AS time#10, id#6]
      +- Project [_1#2 AS time#5, _2#3 AS id#6]
         +- StreamingExecutionRelation MemoryStream[_1#2,_2#3], [_1#2, _2#3]

  at org.apache.spark.sql.catalyst.analysis.UnsupportedOperationChecker$.org$apache$spark$sql$catalyst$analysis$UnsupportedOperationChecker$$throwError(UnsupportedOperationChecker.scala:297)
  at org.apache.spark.sql.catalyst.analysis.UnsupportedOperationChecker$.checkForStreaming(UnsupportedOperationChecker.scala:115)
  at org.apache.spark.sql.streaming.StreamingQueryManager.createQuery(StreamingQueryManager.scala:232)
  at org.apache.spark.sql.streaming.StreamingQueryManager.startQuery(StreamingQueryManager.scala:278)
  at org.apache.spark.sql.streaming.DataStreamWriter.start(DataStreamWriter.scala:247)
  ... 57 elided
Run Code Online (Sandbox Code Playgroud)

小智 6

我认为问题是输出模式。不使用 OutputMode.Complete,而是使用 OutputMode.Append,如下所示。

scala> val q = ids
    .writeStream
    .format("memory")
    .queryName("dups")
    .outputMode(OutputMode.Append)
    .trigger(Trigger.ProcessingTime(30.seconds))
    .option("checkpointLocation", "checkpoint-dir")
    .start
Run Code Online (Sandbox Code Playgroud)

  • 这个答案如何得到支持?追加和完成是完全不同的模式 (4认同)
  • 但 `OutputMode.Append` 的作用与 `OutputMode.Complete` 不同 (2认同)

hi-*_*zir 5

从《结构化流编程指南》中的其他查询(不包括聚合mapGroupsWithStateflatMapGroupsWithState):

不支持使用完整模式,因为将所有未聚合的数据保留在结果表中是不可行的。

要回答这个问题:

如果Spark允许流查询中没有聚合的完整输出模式会怎样?

可能是OOM。

令人困惑的部分是为什么dropDuplicates("id")不将其标记为聚合。