谢谢!
现在您可以通过在application.properties文件中设置以下属性来设置spring boot中的此级别。
spring.data.cassandra.consistency-level=quorum
spring.data.cassandra.serial-consistency-level=quorum
文档中提到可以将其设置为注释 https://docs.spring.io/spring-data/cassandra/docs/current/reference/html/#cassandra.repositories.queries.options 。
我认为,每个请求控制一致性级别总是比应用程序控制更好。
存储库:
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())
我不确定你的意思是什么,当你没有使用 boot 且想要全局设置时,请尝试以下方法。如果你使用的是 spring boot,则这个方法可以奏效。
@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`
Select s = QueryBuilder.select().from("employee");
s.where(QueryBuilder.eq("id", "234"));
s.setConsistencyLevel(ConsistencyLevel.QUORUM) ;
如果有其他人遇到相同或类似的问题,我希望您能重新启动这个旧的线程。
对于使用DataStax
的spring-data-cassandra:3.4.0
,我已经成功地使用以下方法:
@Configuration
public class CassandraConfig extends AbstractCassandraConfiguration {
@Override
public CassandraAdminTemplate cassandraTemplate() {
CassandraAdminTemplate adminTemplate = super.cassandraTemplate();
CqlTemplate cqlTemplate = (CqlTemplate) adminTemplate.getCqlOperations();
cqlTemplate.setConsistencyLevel(DefaultConsistencyLevel.valueOf("LOCAL_QUORUM"));
cqlTemplate.setSerialConsistencyLevel(DefaultConsistencyLevel.valueOf("LOCAL_SERIAL"));
return adminTemplate;
} }
从 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)