【发布时间】:2020-12-07 05:10:18
【问题描述】:
我正在尝试将我的流式数据集写入 Cassandra。
我有以下类的流式数据集;
case class UserSession(var id: Int,
var visited: List[String]
)
我在 Cassandra 中也有以下键空间/表。 (博客=KeySpace,会话=表
CREATE KEYSPACE blog WITH REPLICATION = { 'class' : 'SimpleStrategy', 'replication_factor' : 1 };
CREATE TABLE blog.session(id int PRIMARY KEY, visited list<text>);
我选择list<text>作为访问,因为我访问的类型是List<String>
我的foreach作者如下
class SessionCassandraForeachWriter extends ForeachWriter[UserSession] {
/*
- on every batch, on every partition `partitionId`
- on every "epoch" = chunk of data
- call the open method; if false, skip this chunk
- for each entry in this chunk, call the process method
- call the close method either at the end of the chunk or with an error if it was thrown
*/
val keyspace = "blog"
val table = "session"
val connector = CassandraConnector(sparkSession.sparkContext.getConf)
override def open(partitionId: Long, epochId: Long): Boolean = {
println("Open connection")
true
}
override def process(sess: UserSession): Unit = {
connector.withSessionDo { session =>
session.execute(
s"""
|insert into $keyspace.$table("id")
|values (${sess.id},${sess.visited})
""".stripMargin)
}
}
override def close(errorOrNull: Throwable): Unit = println("Closing connection")
}
查看我的流程函数可能会有所帮助,因为这可能会引发错误。我的主要是以下。
finishedUserSessionsStream: DataSet[UserSession]
def main(args: Array[String]): Unit = {
/// make finishedUserSessionStreams.....
finishedUserSessionsStream.writeStream
.option("checkpointLocation", "checkpoint")
.foreach(new SessionCassandraForeachWriter)
.start()
.awaitTermination()
}
这给了我以下错误
org.apache.spark.sql.catalyst.analysis.UnsupportedOperationChecker$.throwError(UnsupportedOperationChecker.scala:431)
【问题讨论】:
-
使用什么版本的 Spark Cassandra 连接器?和 Spark 版本
-
spark cassandra 连接器是“3.0.0-alpha2”,我使用的是 spark 版本 3.0.0
标签: apache-spark cassandra spark-structured-streaming spark-cassandra-connector