5
votes

I am using spring-data-cassandra (1.3.1.RELEASE) to connect to a cassandra database. Is there a way to change the consistency level in spring-data-cassandra. By default it is level 1 (What is the default consistency level in spring-data-cassandra?). But how to change that?

Thank you!

8
Ok, it looks like there is no way to set a custom consistency level yet: jira.spring.io/browse/DATACASS-145 - K.E.

8 Answers

6
votes

You can now set this level in spring boot by setting the following properties in application.properties file.

spring.data.cassandra.consistency-level=quorum
spring.data.cassandra.serial-consistency-level=quorum
3
votes

It is not possible to set a custom consistency level when using the CrudRepository Interface of spring-data-cassandra. There is an open issue https://jira.spring.io/browse/DATACASS-14. Reason is that the SimpleCassandraRepository which implements the methods of the interface calls the CassandraOperations methods without the WriteOptions parameter.

My solution: I wrote a custom crudrepository implementation, in this I can set the consistency level on my own.

2
votes

If you are not using boot and want to set this globally, give this a shot

@Configuration
public class CassandraConfig extends AbstractCassandraConfiguration {
...
@Override
protected QueryOptions getQueryOptions() {
    QueryOptions queryOptions = new QueryOptions();
    queryOptions.setConsistencyLevel(ConsistencyLevel.LOCAL_QUORUM);
    return queryOptions;
}
2
votes

Docs say you can set it as annotation https://docs.spring.io/spring-data/cassandra/docs/current/reference/html/#cassandra.repositories.queries.options .

IMO, it is always better to control the consistency level per request rather than per application. Repositories:

public interface PersonRepository extends CrudRepository<Person, String> {

    @Consistency(ConsistencyLevel.LOCAL_ONE)
    List<Person> findByLastname(String lastname);

    List<Person> findByFirstname(String firstname, QueryOptions options);
}

CassandraTemplate:

cassandraTemplate.insert(myEntity, new WriteOptions().builder()
                    .consistencyLevel(ConsistencyLevel.QUORUM).build())
1
votes

These are the consistency levels defined right now :
ANY, ONE , TWO , THREE , QUORUM , ALL , LOCAL_QUORUM, EACH_QUORUM , SERIAL , LOCAL_SERIAL , LOCAL_ONE .
If you are talking about using other than these, I don't think there is a pluggable solution for that right now , where you define number of nodes that will participate to assert required consistency.
If you want to use any of the above defined consistencies ,
an example would be :

        Select s = QueryBuilder.select().from("employee");
        s.where(QueryBuilder.eq("id", "234"));
        s.setConsistencyLevel(ConsistencyLevel.QUORUM) ;
1
votes

I am not sure what do you mean by "If you are not using boot and want to set this globally, give this a shot" I used spring boot and this worked`

@Configuration     
public class CassandraConfig extends AbstractCassandraConfiguration {




@Override
public QueryOptions getQueryOptions() {
    QueryOptions queryOptions = new QueryOptions();
    queryOptions.setConsistencyLevel(ConsistencyLevel.TWO);
    return queryOptions;
}

@Override
protected String getKeyspaceName() {
    return keyspaceName;
}

@Override
public CassandraCqlClusterFactoryBean cluster() {
    CassandraCqlClusterFactoryBean cluster = super.cluster();
    cluster.setContactPoints(contactPoints);
    cluster.setPort(port);
    cluster.setUsername(username);
    cluster.setPassword(password);
    return cluster;
}
//Also added @Import to Main class

@Import(CassandraConfig.class)
@EnableJpaRepositories(basePackageClasses = Vexxxxxxy.class)
@EnableCassandraRepositories(basePackageClasses = Txxxxxry.class)
@EntityScan(basePackageClasses = { Entity.class, Vxxxxity.class })
@SpringBootApplication(scanBasePackages = "com.xx.xxx")
public class DrixxxxxxxxMain`
0
votes

One possibility is to set it in the application.properties file: spring.data.cassandra.consistency-level=LOCAL_QUORUM

0
votes

As of spring-data-cassandra release 1.5, consistency level is already configurable (see org.springframework.cassandra.core.QueryOptions#setConsistencyLevel(org.springframework.cassandra.core.ConsistencyLevel))

It may be specified as a part of QueryOptions either for a single query via org.springframework.cassandra.core.CqlOperations#execute(java.lang.String, org.springframework.cassandra.core.QueryOptions) , or for all types of queries via org.springframework.cassandra.core.CqlTemplate#addQueryOptions

Also it's possible to set consistency level via WriteOptions and then pass them to org.springframework.data.cassandra.core.CassandraOperations#insert(T, org.springframework.cassandra.core.WriteOptions)