You can implement addition by joining (on the primary key of the rows) your input data with data in Cassandra using the leftJoinWithCassandraTable
, then perform addition of the input data with fetched data, and writing data back to Cassandra.
Something like this (adopted from my own code):
// this case class is matching to Cassandra table & input data...
case class Data(....)
val data = ...data_that_you_received_and_casted_to_Data_case_class...
val joined = data.leftJoinWithCassandraTable[Data]("ks", "table")
// perform update of existing values, and prepare new data
val summed = joined.map({ case (n: Data, c: Option[Data]) =>
c match {
// if there is no data in Cassandra, just return input data
case None => Data(n)
// there is data in Cassandra, do the sum
case Some(s) =>
Data(..., n.neu + s.neu)}
})
// and write updated/new values
summed.saveToCassandra("ks", "table")
P.S. I would recommend to use Spark Structured Streaming that is directly supported by Spark Cassandra Connector 2.5.0, together with so-called direct join for data frames. Your code would be simpler & potentially more optimized when using dataframes.