Scylla Documentation Logo Documentation
  • Server
    • Scylla Open Source
    • Scylla Enterprise
    • Scylla Alternator
  • Cloud
    • Scylla Cloud
    • Scylla Cloud Docs
  • Tools
    • Scylla Manager
    • Scylla Monitoring Stack
    • Scylla Operator
  • Drivers
    • CQL Drivers
    • DynamoDB Drivers
Download
Menu

Caution

You're viewing documentation for a previous version of Scylla Java Driver. Switch to the latest stable version.

Scylla Java Driver Manual Statements Simple statements

Simple statements¶

Use SimpleStatement for queries that will be executed only once (or a few times) in your application:

SimpleStatement statement = new SimpleStatement(
    "SELECT value FROM application_params WHERE name = 'greeting_message'");
session.execute(statement);

When you don’t need to customize anything on the SimpleStatement object, there is a convenient shortcut:

session.execute("SELECT value FROM application_params WHERE name = 'greeting_message'");

Each time you execute a simple statement, Cassandra will parse the query string again; nothing is cached (neither on the client nor on the server):

client                             driver                Cassandra
--+----------------------------------+---------------------+------
  |                                  |                     |
  | session.execute(SimpleStatement) |                     |
  |--------------------------------->|                     |
  |                                  | QUERY(query_string) |
  |                                  |-------------------->|
  |                                  |                     |
  |                                  |                     |
  |                                  |                     | - parse query string
  |                                  |                     | - execute query
  |                                  |                     |
  |                                  |       ROWS          |
  |                                  |<--------------------|
  |                                  |                     |
  |<---------------------------------|                     |

If you execute the same query often (or a similar query with different column values), consider a prepared statement instead.

Using values¶

Instead of sending a raw query string, you can use bind markers and provide values separately:

  • by position:

    String paramName = ...
    session.execute(
        "SELECT value FROM application_params WHERE name = ?",
        paramName);
    
  • by name:

    // Just a convenience to build a java.util.Map with a one-liner
    import com.google.common.collect.ImmutableMap;
    
    String paramName = ...
    session.execute(
        "SELECT value FROM application_params WHERE name = :n",
        ImmutableMap.<String, Object>of("n", paramName));
    

This syntax has a few advantages:

  • if the values already come from some other part of your code, it looks cleaner than doing the concatenation yourself;

  • you don’t need to translate the values to their string representation. The driver will sent them alongside the query, in their serialized binary form.

The number of values must match the query string, and their types must match the database schema. Note that the driver does not parse query strings, so it cannot perform those checks on the client side; if you make a mistake, the query will be sent anyway, and the error will be caught by Cassandra (InvalidQueryException is a server-side error):

session.execute(
        "SELECT value FROM application_params WHERE name = ?",
        "foo", "bar");
// Exception in thread "main" com.datastax.driver.core.exceptions.InvalidQueryException:
// Invalid amount of bind variables

Value type inference¶

Another consequence of not parsing query strings is that the driver has to make a guess on how to serialize values, based on their Java type (see the default type mappings). This can be tricky, in particular for numeric types:

// schema: create table bigints(b bigint primary key)
session.execute(
        "INSERT INTO bigints (b) VALUES (?)",
        1);
// Exception in thread "main" com.datastax.driver.core.exceptions.InvalidQueryException:
// Expected 8 or 0 byte long (4)

The problem here is that the literal 1 has the Java type int. So the driver serializes it as a CQL int (4 bytes), but the server expects a CQL bigint (8 bytes). The fix is to specify the correct Java type:

session.execute(
        "INSERT INTO bigints (b) VALUES (?)",
        1L);

In the same vein, strings are always serialized to varchar, so you could have a problem if you target an ascii column:

// schema: create table ascii_quotes(id int primary key, t ascii)
session.execute(
        "INSERT INTO ascii_quotes (id, t) VALUES (?, ?)",
        1, "Touché sir, touché...");
// Exception in thread "main" com.datastax.driver.core.exceptions.InvalidQueryException:
// Invalid byte for ascii: -61

In that situation, there is no way to hint at the correct type. Your only option is to serialize the value manually:

ProtocolVersion protocolVersion = cluster.getConfiguration().getProtocolOptions().getProtocolVersionEnum();
ByteBuffer bytes = DataType.ascii().serialize("Touché sir, touché...", protocolVersion);
session.execute(
        "INSERT INTO ascii_quotes (id, t) VALUES (?, ?)",
        1, bytes);
PREVIOUS
Statements
NEXT
Prepared statements
  • 3.10.2.x
    • 4.13.0.x
    • 4.12.0.x
    • 4.11.1.x
    • 4.10.0.x
    • 4.7.2.x
    • 3.11.2.x
    • 3.11.0.x
    • 3.10.2.x
    • 3.7.2.x
  • Scylla Java Driver for Scylla and Apache Cassandra®
  • API Documentation
  • Manual
    • Address resolution
    • Asynchronous programming
    • Authentication
    • Compression
    • Control connection
    • Custom Codecs
    • Custom Payloads
    • Query idempotence
    • Load balancing
    • Logging
    • Metadata
    • Metrics
    • Native protocol
    • Object Mapper
      • Definition of mapped classes
      • Using custom codecs
      • Using the mapper
    • OSGi
    • Paging
    • Connection pooling
    • Query timestamps
    • Reconnection
    • Retries
    • Using the shaded JAR
    • Socket options
    • Speculative query execution
    • SSL
    • Statements
      • Simple statements
      • Prepared statements
      • Built statements
      • Batch statements
    • Using Tuples with the Java driver
    • User-defined types
  • Upgrade guide
    • Migrating from Astyanax
      • Configuration
      • Language change : from Thrift to CQL
      • Queries and Results
  • Frequently Asked Questions
  • Changelog
  • Create an issue
  • Edit this page

On this page

  • Simple statements
    • Using values
    • Value type inference
Logo
Docs Contact Us About Us
Mail List Icon Slack Icon
© 2022, ScyllaDB. All rights reserved.
Last updated on 25 May 2022.
Powered by Sphinx 4.3.2 & ScyllaDB Theme 1.2.2