Michael Torres Ospina
Michael Torres Ospina

Reputation: 33

How do I configure consistency level with Spring Boot and AWS Keyspaces?

I hope you can help me, I am doing a crud with spring boot and keyspaces (cassandra aws), spring has the default level of Consistency level in ONE and I have not been able to write the data since I get the following error:

"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",

I don't know how to configure the consistency level, I have tried several solutions on the internet and none have worked for me.

i have the following code

@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);
    }
}

Upvotes: 0

Views: 808

Answers (2)

MikeJPR
MikeJPR

Reputation: 812

Here is an example of Spring Boot and Amazon Keyspaces

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);

        
    }



}

Upvotes: -2

Erick Ramirez
Erick Ramirez

Reputation: 16353

There are several ways to configure the consistency level. You can define the default consistency in an application.conf file with:

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

You can also configure it when using InsertOptions and UpdateOptions. For example:

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

You can also use the @Consistency annotation, for example:

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

Finally with QueryOptions:

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

Upvotes: 3

Related Questions