org.apache.spark.sql.AnalysisException: 'write' can not be called on streaming Dataset/DataFrame

三世轮回 提交于 2021-01-27 14:23:40

问题


I'm trying to write a Spark Structured Streaming (2.3) dataset to ScyllaDB (Cassandra).

My code to write the dataset:

  def saveStreamSinkProvider(ds: Dataset[InvoiceItemKafka]) = {
    ds
      .writeStream
      .format("cassandra.ScyllaSinkProvider")
      .outputMode(OutputMode.Append)
      .queryName("KafkaToCassandraStreamSinkProvider")
      .options(
        Map(
          "keyspace" -> namespace,
          "table" -> StreamProviderTableSink,
          "checkpointLocation" -> "/tmp/checkpoints"
        )
      )
      .start()
  }

My ScyllaDB Streaming Sinks:

class ScyllaSinkProvider extends StreamSinkProvider {
  override def createSink(sqlContext: SQLContext,
                          parameters: Map[String, String],
                          partitionColumns: Seq[String],
                          outputMode: OutputMode): ScyllaSink =
    new ScyllaSink(parameters)
}

class ScyllaSink(parameters: Map[String, String]) extends Sink {
  override def addBatch(batchId: Long, data: DataFrame): Unit =
    data.write
            .cassandraFormat(
              parameters("table"),
              parameters("keyspace")
              //parameters("cluster")
            )
      .mode(SaveMode.Append)
      .save()
}

However, when I run this code, I receive an exception:

...
[error]       +- StreamingExecutionRelation KafkaSource[Subscribe[transactions_load]], [key#7, value#8, topic#9, partition#10, offset#11L, timestamp#12, timestampType#13]
[error]     at org.apache.spark.sql.execution.streaming.StreamExecution.org$apache$spark$sql$execution$streaming$StreamExecution$$runStream(StreamExecution.scala:295)
[error]     at org.apache.spark.sql.execution.streaming.StreamExecution$$anon$1.run(StreamExecution.scala:189)
[error] Caused by: org.apache.spark.sql.AnalysisException: 'write' can not be called on streaming Dataset/DataFrame;
[error]     at org.apache.spark.sql.catalyst.analysis.package$AnalysisErrorAt.failAnalysis(package.scala:42)
[error]     at org.apache.spark.sql.Dataset.write(Dataset.scala:3103)
[error]     at cassandra.ScyllaSink.addBatch(CassandraDriver.scala:113)
[error]     at org.apache.spark.sql.execution.streaming.MicroBatchExecution$$anonfun$org$apache$spark$sql$execution$streaming$MicroBatchExecution$$runBatch$3$$anonfun$apply$16.apply(MicroBatchExecution.scala:477)
...

I have seen a similar question, but that is for CosmosDB - Spark CosmosDB Sink: org.apache.spark.sql.AnalysisException: 'write' can not be called on streaming Dataset/DataFrame


回答1:


You could convert it to an RDD first and then write:

class ScyllaSink(parameters: Map[String, String]) extends Sink {    

  override def addBatch(batchId: Long, data: DataFrame): Unit = synchronized {
    val schema = data.schema
    // this ensures that the same query plan will be used
    val rdd: RDD[Row] = df.queryExecution.toRdd.mapPartitions { rows =>
      val converter = CatalystTypeConverters.createToScalaConverter(schema)
      rows.map(converter(_).asInstanceOf[Row])
    }

    // write the RDD to Cassandra 
  }
}


来源:https://stackoverflow.com/questions/53086376/org-apache-spark-sql-analysisexception-write-can-not-be-called-on-streaming-d

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!