简体   繁体   中英

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.

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;

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM