HeartSaVioR commented on a change in pull request #29767:
URL: https://github.com/apache/spark/pull/29767#discussion_r501450163
##########
File path:
sql/core/src/main/scala/org/apache/spark/sql/streaming/DataStreamWriter.scala
##########
@@ -457,6 +470,17 @@ final class DataStreamWriter[T] private[sql](ds:
Dataset[T]) {
foreachBatch((batchDs: Dataset[T], batchId: Long) =>
function.call(batchDs, batchId))
}
+ /**
+ * Specifies the underlying output table.
+ *
+ * @since 3.1.0
+ */
+ def table(tableName: String): DataStreamWriter[T] = {
Review comment:
OK I'll change the name to saveAsTable and call start() there.
##########
File path:
sql/core/src/main/scala/org/apache/spark/sql/streaming/DataStreamWriter.scala
##########
@@ -300,54 +301,55 @@ final class DataStreamWriter[T] private[sql](ds:
Dataset[T]) {
"write files of Hive data source directly.")
}
- if (source == "memory") {
- assertNotPartitioned("memory")
+ if (source == SOURCE_NAME_TABLE) {
+ assertNotPartitioned(SOURCE_NAME_TABLE)
+
+ import df.sparkSession.sessionState.analyzer.CatalogAndIdentifier
+
+ import org.apache.spark.sql.connector.catalog.CatalogV2Implicits._
+ val originalMultipartIdentifier = df.sparkSession.sessionState.sqlParser
+ .parseMultipartIdentifier(tableName)
+ val CatalogAndIdentifier(catalog, identifier) =
originalMultipartIdentifier
+
+ // Currently we don't create a logical streaming writer node in logical
plan, so cannot rely
+ // on analyzer to resolve it. Directly lookup only for temp view to
provide clearer message.
+ // TODO (SPARK-27484): we should add the writing node before the plan is
analyzed.
+ if
(df.sparkSession.sessionState.catalog.isTempView(originalMultipartIdentifier)) {
+ throw new AnalysisException(s"Temporary view $tableName doesn't
support streaming write")
+ }
+
+ val tableInstance = catalog.asTableCatalog.loadTable(identifier)
+
+ import
org.apache.spark.sql.execution.datasources.v2.DataSourceV2Implicits._
+ val sink = tableInstance match {
+ case t: SupportsWrite if t.supports(STREAMING_WRITE) => t
+ case t => throw new AnalysisException(s"Table $tableName doesn't
support streaming " +
+ s"write - $t")
+ }
+
+ startQuery(sink, extraOptions)
+ } else if (source == SOURCE_NAME_MEMORY) {
+ assertNotPartitioned(SOURCE_NAME_MEMORY)
if (extraOptions.get("queryName").isEmpty) {
throw new AnalysisException("queryName must be specified for memory
sink")
}
val sink = new MemorySink()
val resultDf = Dataset.ofRows(df.sparkSession, new MemoryPlan(sink,
df.schema.toAttributes))
- val chkpointLoc = extraOptions.get("checkpointLocation")
val recoverFromChkpoint = outputMode == OutputMode.Complete()
- val query =
df.sparkSession.sessionState.streamingQueryManager.startQuery(
- extraOptions.get("queryName"),
- chkpointLoc,
- df,
- extraOptions.toMap,
- sink,
- outputMode,
- useTempCheckpointLocation = true,
- recoverFromCheckpointLocation = recoverFromChkpoint,
- trigger = trigger)
+ val query = startQuery(sink, extraOptions, recoverFromCheckpoint =
recoverFromChkpoint)
resultDf.createOrReplaceTempView(query.name)
query
- } else if (source == "foreach") {
- assertNotPartitioned("foreach")
+ } else if (source == SOURCE_NAME_FOREACH) {
+ assertNotPartitioned(SOURCE_NAME_FOREACH)
val sink = ForeachWriterTable[T](foreachWriter, ds.exprEnc)
- df.sparkSession.sessionState.streamingQueryManager.startQuery(
- extraOptions.get("queryName"),
- extraOptions.get("checkpointLocation"),
- df,
- extraOptions.toMap,
- sink,
- outputMode,
- useTempCheckpointLocation = true,
- trigger = trigger)
- } else if (source == "foreachBatch") {
- assertNotPartitioned("foreachBatch")
+ startQuery(sink, extraOptions)
+ } else if (source == SOURCE_NAME_FOREACH_BATCH) {
+ assertNotPartitioned(SOURCE_NAME_FOREACH_BATCH)
if (trigger.isInstanceOf[ContinuousTrigger]) {
- throw new AnalysisException("'foreachBatch' is not supported with
continuous trigger")
+ throw new AnalysisException(s"'$source' is not supported with
continuous trigger")
}
val sink = new ForeachBatchSink[T](foreachBatchWriter, ds.exprEnc)
- df.sparkSession.sessionState.streamingQueryManager.startQuery(
- extraOptions.get("queryName"),
- extraOptions.get("checkpointLocation"),
- df,
- extraOptions.toMap,
- sink,
- outputMode,
- useTempCheckpointLocation = true,
- trigger = trigger)
+ startQuery(sink, extraOptions)
Review comment:
Ah OK thanks for pointing out. Nice finding. I'll just explicitly call
`.toMap` as it was.
##########
File path:
sql/core/src/main/scala/org/apache/spark/sql/streaming/DataStreamWriter.scala
##########
@@ -380,19 +396,31 @@ final class DataStreamWriter[T] private[sql](ds:
Dataset[T]) {
createV1Sink(optionsWithPath)
}
- df.sparkSession.sessionState.streamingQueryManager.startQuery(
- extraOptions.get("queryName"),
- extraOptions.get("checkpointLocation"),
- df,
- optionsWithPath.originalMap,
- sink,
- outputMode,
- useTempCheckpointLocation = source == "console" || source == "noop",
- recoverFromCheckpointLocation = true,
- trigger = trigger)
+ startQuery(sink, optionsWithPath)
}
}
+ private def startQuery(
+ sink: Table,
+ newOptions: CaseInsensitiveMap[String],
+ recoverFromCheckpoint: Boolean = true): StreamingQuery = {
+ val options = newOptions.originalMap
+ val queryName = options.get("queryName")
Review comment:
OK I'll let it as it is.
##########
File path:
sql/core/src/main/scala/org/apache/spark/sql/streaming/DataStreamWriter.scala
##########
@@ -380,19 +396,31 @@ final class DataStreamWriter[T] private[sql](ds:
Dataset[T]) {
createV1Sink(optionsWithPath)
}
- df.sparkSession.sessionState.streamingQueryManager.startQuery(
- extraOptions.get("queryName"),
- extraOptions.get("checkpointLocation"),
- df,
- optionsWithPath.originalMap,
- sink,
- outputMode,
- useTempCheckpointLocation = source == "console" || source == "noop",
- recoverFromCheckpointLocation = true,
- trigger = trigger)
+ startQuery(sink, optionsWithPath)
}
}
+ private def startQuery(
+ sink: Table,
+ newOptions: CaseInsensitiveMap[String],
+ recoverFromCheckpoint: Boolean = true): StreamingQuery = {
+ val options = newOptions.originalMap
+ val queryName = options.get("queryName")
+ val checkpointLocation = options.get("checkpointLocation")
+ val useTempCheckpointLocation =
SOURCES_ALLOW_ONE_TIME_QUERY.contains(source)
+
+ df.sparkSession.sessionState.streamingQueryManager.startQuery(
Review comment:
OK let me keep it as it is.
----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]