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
?
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With