cassandra 如何配置与Sping Boot 和AWS密钥空间的一致性级别?

jgovgodb  于 2022-11-23  发布在  Cassandra
关注(0)|答案(2)|浏览(165)

我希望你能帮助我,我正在做一个crud与 Spring Boot 和keyspace(cassandra aws),Spring有默认的一致性水平在一个级别,我一直无法写入数据,因为我得到以下错误:

"message": "Query; CQL [INSERT INTO tabledemoach (address,ciiu,creation_date,email,id,name,phone,state,user_activation_status) VALUES (?,?,?,?,?,?,?,?,?)]; Consistency level LOCAL_ONE is not supported for this operation. Supported consistency levels are: LOCAL_QUORUM; nested exception is com.datastax.oss.driver.api.core.servererrors.InvalidQueryException: Consistency level LOCAL_ONE is not supported for this operation. Supported consistency levels are: LOCAL_QUORUM",

我不知道如何配置一致性级别,我在互联网上尝试了几种解决方案,但没有一种对我有效。
我有以下代码

@Configuration
@EnableCassandraRepositories(basePackages = "com.demo")
public class AppConfig {

private final static String KEYSPACE = "demo";

@Primary
public @Bean CqlSession session() {
    return CqlSession.builder().withKeyspace(KEYSPACE).build();
}

}

@Table(value = "tabledemoach")
@Data
public class User {

@PrimaryKey
private int id;
private String phone;
private String name;
private String address;
private String email;
private int ciiu;
private String state;
private String user_activation_status;
private LocalDate creation_date;
} 

    @Override
    public void createUser(User user) {
    List<User> userFind = (List<User>) userRepository.findAll();

    var userList =userFind.stream().map(x -> x.getPhone());
    var repeated = (userList.filter(x -> 
    x.contains(user.getPhone()))).collect(Collectors.toList());

    if(repeated.size() <= 0){
        userRepository.save(user);
    }
}
pu3pd22g

pu3pd22g1#

配置一致性级别的方法有多种。可以使用以下命令在application.conf文件中定义默认一致性:

datastax-java-driver {
  basic {
    request {
      consistency = LOCAL_QUORUM
    }
  }
}

您也可以在使用InsertOptionsUpdateOptions时对其进行配置。例如:

InsertOptions insertOptions = 
    org.springframework.data.cassandra.core.InsertOptions.builder()
        .consistencyLevel(ConsistencyLevel.LOCAL_QUORUM)
        .build();

您也可以使用@Consistency注解,例如:

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

最后用QueryOptions

QueryOptions queryOptions = 
    newQueryOptions.setConsistencyLevel(ConsistencyLevel.LOCAL_QUORUM);
bttbmeg0

bttbmeg02#

以下是Sping Boot 和Amazon密钥空间的示例
https://github.com/aws-samples/amazon-keyspaces-examples/tree/main/java/datastax-v4/spring

package com.example.demo;

import com.datastax.oss.driver.api.core.CqlSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.data.cassandra.core.CassandraOperations;
import org.springframework.data.cassandra.core.cql.CqlTemplate;
import org.springframework.data.cassandra.repository.config.EnableCassandraRepositories;

@SpringBootApplication
@EnableCassandraRepositories(basePackages="com.example.model")
public class DemoApplication implements CommandLineRunner {

    @Autowired
    private CassandraOperations cassandraTemplate;

    public static void main(String[] args) {

        SpringApplication.run(DemoApplication.class, args);

    }

    @Override
    public void run(String... args) throws Exception {

        CqlTemplate cqlTemplate = (CqlTemplate) cassandraTemplate.getCqlOperations();

        CqlSession session = cqlTemplate.getSession();

        int count = session.execute ("SELECT * FROM system.peers").all().size();

        System.out.println("Number of hosts: "+  count);

        
    }


}

相关问题