Cassandra java driver set global consistency level
Asked Answered
C

1

6

So in the datastax doc, it states that ConsistencyLevel can be set globally through QueryOptions:

QueryOptions qo = new QueryOptions().setConsistencyLevel(ConsistencyLevel.ALL);

I noticed that this is not a static method and returns an instance of QueryOptions. Does that mean simply calling this method won't set the default consistency level globally and I need to use the QueryOptions when connecting to the Cassandra cluster? I mean, is the following code required (to set the QueryOption when building the Cluster object)

cluster = Cluster.builder().addContactPoint("192.168.0.30")
.withQueryOptions(new QueryOptions()
.setConsistencyLevel(ConsistencyLevel.ONE)
.withRetryPolicy(DefaultRetryPolicy.INSTANCE)
.withLoadBalancingPolicy(new TokenAwarePolicy(new DCAwareRoundRobinPolicy()))
.build();
session = cluster.connect("demo");

My problem is that I don't have access to the code that builds the Cluster instance. Does that mean I can't set global consistency level and have to rely on setting it per Statement?

Crupper answered 1/2, 2018 at 20:15 Comment(0)
E
3

Dataxdriver says that you can set the global value through QueryOptions and for specific queries consistency level you can define per statement. If you don't have access to the cluster building code, you can check the value of consistency level with getConsistencyLevel method. If this is the one which you want to set for most of the statements then your job will be easier. Otherwise you will need to define it per statement basis.

Please take a look at Best practice for managing different consistency levels using the Datastax Cassandra native java client

It describes why having consistency level per statement is better than having a global value.

To answer your questions of

"I noticed that this is not a static method and returns an instance of QueryOptions. Does that mean simply calling this method won't set the default consistency level globally and I need to use the QueryOptions when connecting to the Cassandra cluster? I mean, is the following code required (to set the QueryOption when building the Cluster object)"

Even when you don't call withQueryOptions on the cluster builder object as per the documentation it creates instance with default query options.

Check DefaultConstructor of QueryOptions the constructor creates a query option with default value of consistency level and other params.

Also As per Cluster builder doc , withQueryOptions also returns an instance of Cluster.Builder and you will need to build it again(which is not you want i guess).

So the summary is : The only way to set a global consistency level is while creating the cluster builder object. In your case when you don't have access to the code that builds it, you would have to setup values per statement or can end up with building new one(disclaimer: should not be done!!) and using it.

Etymon answered 1/2, 2018 at 21:12 Comment(2)
Thanks. I just ran my code through debugger and verified that simply calling new QueryOptions().setConsistencyLevel(ConsistencyLevel.ALL) won't set the consistency level globally (I check the consistency level of my PreparedStatement and it's null). I guess you have to use the returned QueryOptions object while building your cluster object.Crupper
Yes to associate the new query options you need to pass it to builder object in withQueryOptions which again gives a Builder instance . To be able to use this builder instance one must invoke .build() method on that.Etymon

© 2022 - 2024 — McMap. All rights reserved.