Batch Statements

Insert, updates, and deletes can be batched into a single statement sent to Cassandra, and, using a batch type of Logged, will either all succeed or all fail. There are performance implications to using batch statements, which you can read about here.

In the Scala-Cass library, you can model a batch statement by passing prepared statements to a batch statement, which can be accomplished in a number of ways:

batchOf

scala> case class Query(s: String)
defined class Query

scala> case class NewValue(i: Int, l: Long)
defined class NewValue

scala> val updateStatement = sSession.update("mytable", NewValue(1234, 5678L), Query("some str"))
updateStatement: com.weather.scalacass.scsession.SCUpdateStatement = SCUpdateStatement(UPDATE mykeyspace.mytable SET i=1234, l=5678 WHERE s=some str)

scala> case class Insertable(s: String, i: Int, l: Long)
defined class Insertable

scala> val insertStatement = sSession.insert("mytable", Insertable("some other str", 4321, 8765L))
insertStatement: com.weather.scalacass.scsession.SCInsertStatement = SCInsertStatement(INSERT INTO mykeyspace.mytable (s, i, l) VALUES (some other str, 4321, 8765))

scala> val deleteStatement = sSession.deleteRow("mytable", Query("a third str"))
deleteStatement: com.weather.scalacass.scsession.SCDeleteStatement = SCDeleteStatement(DELETE  FROM mykeyspace.mytable WHERE s=a third str)

scala> val batchOfStatement = sSession.batchOf(updateStatement, insertStatement, deleteStatement)
batchOfStatement: com.weather.scalacass.scsession.SCBatchStatement =
SCBatchStatement(
  BEGIN LOGGED BATCH
    UPDATE mykeyspace.mytable SET i=1234, l=5678 WHERE s=some str;
    INSERT INTO mykeyspace.mytable (s, i, l) VALUES (some other str, 4321, 8765);
    DELETE  FROM mykeyspace.mytable WHERE s=a third str;
  APPLY BATCH;
)

scala> batchOfStatement.execute()
res2: com.weather.scalacass.Result[com.datastax.driver.core.ResultSet] = Right(ResultSet[ exhausted: true, Columns[]])

batch

scala> val statementsToBatch = List(updateStatement, insertStatement, deleteStatement)
statementsToBatch: List[com.weather.scalacass.scsession.SCStatement[com.datastax.driver.core.ResultSet] with com.weather.scalacass.scsession.SCBatchStatement.Batchable] = List(SCUpdateStatement(UPDATE mykeyspace.mytable SET i=1234, l=5678 WHERE s=some str), SCInsertStatement(INSERT INTO mykeyspace.mytable (s, i, l) VALUES (some other str, 4321, 8765)), SCDeleteStatement(DELETE  FROM mykeyspace.mytable WHERE s=a third str))

scala> val batchStatement = sSession.batch(statementsToBatch)
batchStatement: com.weather.scalacass.scsession.SCBatchStatement =
SCBatchStatement(
  BEGIN LOGGED BATCH
    UPDATE mykeyspace.mytable SET i=1234, l=5678 WHERE s=some str;
    INSERT INTO mykeyspace.mytable (s, i, l) VALUES (some other str, 4321, 8765);
    DELETE  FROM mykeyspace.mytable WHERE s=a third str;
  APPLY BATCH;
)

scala> batchStatement.execute()
res3: com.weather.scalacass.Result[com.datastax.driver.core.ResultSet] = Right(ResultSet[ exhausted: true, Columns[]])

+ (build)

scala> val oneBatchStatement = sSession.batchOf(updateStatement)
oneBatchStatement: com.weather.scalacass.scsession.SCBatchStatement =
SCBatchStatement(
  BEGIN LOGGED BATCH
    UPDATE mykeyspace.mytable SET i=1234, l=5678 WHERE s=some str;
  APPLY BATCH;
)

scala> val twoBatchStatement = oneBatchStatement + insertStatement
twoBatchStatement: com.weather.scalacass.scsession.SCBatchStatement =
SCBatchStatement(
  BEGIN LOGGED BATCH
    UPDATE mykeyspace.mytable SET i=1234, l=5678 WHERE s=some str;
    INSERT INTO mykeyspace.mytable (s, i, l) VALUES (some other str, 4321, 8765);
  APPLY BATCH;
)

scala> val threeBatchStatement = twoBatchStatement + deleteStatement
threeBatchStatement: com.weather.scalacass.scsession.SCBatchStatement =
SCBatchStatement(
  BEGIN LOGGED BATCH
    UPDATE mykeyspace.mytable SET i=1234, l=5678 WHERE s=some str;
    INSERT INTO mykeyspace.mytable (s, i, l) VALUES (some other str, 4321, 8765);
    DELETE  FROM mykeyspace.mytable WHERE s=a third str;
  APPLY BATCH;
)

scala> threeBatchStatement.execute()
res4: com.weather.scalacass.Result[com.datastax.driver.core.ResultSet] = Right(ResultSet[ exhausted: true, Columns[]])

++ (append)

scala> val fromListBatchStatement = oneBatchStatement ++ List(insertStatement, deleteStatement)
fromListBatchStatement: com.weather.scalacass.scsession.SCBatchStatement =
SCBatchStatement(
  BEGIN LOGGED BATCH
    UPDATE mykeyspace.mytable SET i=1234, l=5678 WHERE s=some str;
    INSERT INTO mykeyspace.mytable (s, i, l) VALUES (some other str, 4321, 8765);
    DELETE  FROM mykeyspace.mytable WHERE s=a third str;
  APPLY BATCH;
)

scala> fromListBatchStatement.execute()
res5: com.weather.scalacass.Result[com.datastax.driver.core.ResultSet] = Right(ResultSet[ exhausted: true, Columns[]])
scala> val otherBatchStatement = sSession.batchOf(insertStatement, deleteStatement)
otherBatchStatement: com.weather.scalacass.scsession.SCBatchStatement =
SCBatchStatement(
  BEGIN LOGGED BATCH
    INSERT INTO mykeyspace.mytable (s, i, l) VALUES (some other str, 4321, 8765);
    DELETE  FROM mykeyspace.mytable WHERE s=a third str;
  APPLY BATCH;
)

scala> val fromOtherBatchStatement = oneBatchStatement ++ otherBatchStatement
fromOtherBatchStatement: com.weather.scalacass.scsession.SCBatchStatement =
SCBatchStatement(
  BEGIN LOGGED BATCH
    UPDATE mykeyspace.mytable SET i=1234, l=5678 WHERE s=some str;
    INSERT INTO mykeyspace.mytable (s, i, l) VALUES (some other str, 4321, 8765);
    DELETE  FROM mykeyspace.mytable WHERE s=a third str;
  APPLY BATCH;
)

scala> fromOtherBatchStatement.execute()
res6: com.weather.scalacass.Result[com.datastax.driver.core.ResultSet] = Right(ResultSet[ exhausted: true, Columns[]])

and (build multiple)

scala> val andBatchStatement = oneBatchStatement and (insertStatement, deleteStatement)
andBatchStatement: com.weather.scalacass.scsession.SCBatchStatement =
SCBatchStatement(
  BEGIN LOGGED BATCH
    UPDATE mykeyspace.mytable SET i=1234, l=5678 WHERE s=some str;
    INSERT INTO mykeyspace.mytable (s, i, l) VALUES (some other str, 4321, 8765);
    DELETE  FROM mykeyspace.mytable WHERE s=a third str;
  APPLY BATCH;
)

scala> andBatchStatement.execute()
res7: com.weather.scalacass.Result[com.datastax.driver.core.ResultSet] = Right(ResultSet[ exhausted: true, Columns[]])

Batch Type

You can additionally specify the batch type of the statement, but it defaults to LOGGED.

scala> import com.datastax.driver.core.BatchStatement
import com.datastax.driver.core.BatchStatement

scala> val withTypeBatchStatement = batchStatement.withBatchType(BatchStatement.Type.LOGGED)
withTypeBatchStatement: com.weather.scalacass.scsession.SCBatchStatement =
SCBatchStatement(
  BEGIN LOGGED BATCH
    UPDATE mykeyspace.mytable SET i=1234, l=5678 WHERE s=some str;
    INSERT INTO mykeyspace.mytable (s, i, l) VALUES (some other str, 4321, 8765);
    DELETE  FROM mykeyspace.mytable WHERE s=a third str;
  APPLY BATCH;
)

scala> withTypeBatchStatement.execute()
res8: com.weather.scalacass.Result[com.datastax.driver.core.ResultSet] = Right(ResultSet[ exhausted: true, Columns[]])