Spring -data-cassandra sets the level of consistency

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

Thanks!

+4
source share
7 answers

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

My solution : I wrote a custom crudrepository implementation, in this I can set the level of consistency myself.

+2
source

Now you can set this level in spring boot by setting the following properties in the application.properties file.

spring.data.cassandra.consistency-level=quorum
spring.data.cassandra.serial-consistency-level=quorum
+2
source

, :
  , ,    ,   ,   QUORUM,   ,   LOCAL_QUORUM,   EACH_QUORUM,   SERIAL,   LOCAL_SERIAL,   LOCAL_ONE.
, , , , , .
,
:

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

- application.properties: spring.data.cassandra.consistency = LOCAL_QUORUM

0

spring-data-cassandra 1.5, (. org.springframework.cassandra.core.QueryOptions#setConsistencyLevel(org.springframework.cassandra.core.ConsistencyLevel))

QueryOptions org.springframework.cassandra.core.CqlOperations#execute(java.lang.String, org.springframework.cassandra.core.QueryOptions), org.springframework.cassandra.core.CqlTemplate#addQueryOptions

WriteOptions, org.springframework.data.cassandra.core.CassandraOperations#insert(T, org.springframework.cassandra.core.WriteOptions)

0

,

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

, https://docs.spring.io/spring-data/cassandra/docs/current/reference/html/#cassandra.repositories.queries.options.

IMO, , . :

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())
0
source

Source: https://habr.com/ru/post/1617478/


All Articles