To execute a CQL query, you create a Statement instance and pass it to Session#execute or Session#executeAsync. The driver provides various implementations:
- SimpleStatement: a simple implementation built directly from a character string. Typically used for queries that are executed only once or a few times.
- BoundStatement: obtained by binding values to a prepared statement. Typically used for queries that are executed often, with different values.
- BatchStatement: a statement that groups multiple statements to be executed as a batch.
All statement types share a common set of options, that can be set through either setters or a builder:
- configuration profile name, or the configuration profile itself if it's been built dynamically.
- custom payload to send arbitrary key/value pairs with the request (you only use this if you have a custom query handler on the server).
- idempotent flag.
- tracing flag.
- query timestamp.
- paging state.
- per-query keyspace (Cassandra 4 or above).
When setting these options, keep in mind that statements are immutable, and every method returns a different instance:
SimpleStatement statement =
SimpleStatement.newInstance("SELECT release_version FROM system.local");
// Won't work: statement isn't modified in place
statement.setConfigProfileName("oltp");
statement.setIdempotent(true);
// Do this instead:
statement = statement.setConfigProfileName("oltp").setIdempotent(true);