Search code examples
javacassandradatastaxdatastax-java-driver

How to set consistency level for all queries using Cassandra Java Driver?


On an earlier version I would do something like this:

QueryOptions queryOptions = new QueryOptions();
queryOptions.setConsistencyLevel(ConsistencyLevel.LOCAL_ONE)
cluster = Cluster.builder()
        .withQueryOptions(queryOptions)
        ...
        .build();

I can't find a way to do this with DataStax Java Driver 4.2

The closest I could find is

boundStatement.setConsistencyLevel(ConsistencyLevel.LOCAL_ONE);

but I would like to set the level for all queries.


Solution

  • In Java driver 4.x, configuration is done other way - you either specify configuration options in application.conf file (preferred method), or specify it programmatically via DriverConfigLoader.programmaticBuilder (mostly useful for tools builders). See driver documentation for details.

    For your case, you need to specify values for following configuration parameters (see configuration reference for details, also in source code):

    • datastax-java-driver.basic.request.consistency for "normal" requests;
    • datastax-java-driver.basic.request.serial-consistency for lightweight transactions;

    P.S. It's better to upgrade to latest version - 4.6.0 was released yesterday...