Consistency Level

By default, statements will use the same consistency as set for the entire cluster (via the QueryOptions). But, Cassandra also allows for statement-specific consistency levels to be used for insert, select, update, delete, batch, and raw statements.

They all act the same way, so while the examples below focus on insert, the same rules apply to all of the above.

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

scala> case class Insertable(s: String, i: Int, l: Long) // same as the table
defined class Insertable

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

scala> val consistencyInsert = insertStatement.consistency(ConsistencyLevel.ONE)
consistencyInsert: com.weather.scalacass.scsession.SCInsertStatement = SCInsertStatement(INSERT INTO mykeyspace.mytable (s, i, l) VALUES (some str, 1234, 5678) <CONSISTENCY ONE>)

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

And remove it if necessary, which will mean the statement will be written with the consistency level set for the cluster:

scala> val noConsistencyInsert = insertStatement.defaultConsistency
noConsistencyInsert: com.weather.scalacass.scsession.SCInsertStatement = SCInsertStatement(INSERT INTO mykeyspace.mytable (s, i, l) VALUES (some str, 1234, 5678))

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

Finally, certain consistency levels are only allowed for read vs write operations. See the documentation for more details. In addition, batch statements can only use SERIAL or LOCAL_SERIAL level consistency.