user1955255
user1955255

Reputation: 229

How to set consistency level on a query basis in Java Spring Data Cassandra Reactive Library

I am using Spring Data Cassandra reactive library in Java and using ReactiveCqlTemplate to query ScyllaDB.

On an application level, we can set by specifying property as below in the application.properties file

spring.data.cassandra.consistency-level=quorum

My question is how do I specify consistency level on a per-query level using ReactiveCqlTemplate? I am using ReactiveCqlTemplate to query my Scylla DB. The code sample is as below:

@Autowired
private ReactiveCqlTemplate template;

@Override
public Mono<Template> findTemplate(Channel channel, String id) {
    String query = "select * from task where id = ? and channel = ?";
    
    return template.queryForObject(query,  new RowMapper<Template>() {
        public Template mapRow(Row row, int rowNum) {
          Template template = new Template();
          template.setContent(row.getString("content"));
          template.setMessageType(row.getString("message_type"));
          template.setSource(row.getString("source"));
          template.setSubject(row.getString("subject"));
          template.setTemplateKey(new TemplateKey(row.getString("id"), Channel.fromString(row.getString(("channel")))));
          return template;
        }}, UUID.fromString(id), channel.toString());
}

There is an option to set ConsistencyLevel in using the template variable above but that would be for the whole object. I would like to specify on each-query I am shooting to Scylla DB.

Upvotes: 0

Views: 1220

Answers (1)

Toerktumlare
Toerktumlare

Reputation: 14783

If you insist on using ReactiveCqlTemplate you are forced to use QueryBuilder to build queries and then setConsistency level on your SimpleStatements.

Otherwise you can use ReactiveCassandraTemplate and you will have an option to pass in QueryOptions for each statement.

Or even better if you use the repository you can just annotate your queries with @Consistency( ... )

You have selected the lowest level so then you need to use the drivers classes and get no abstraction help from spring.

Choosing An Approach

Upvotes: 1

Related Questions