gedanziger
gedanziger

Reputation: 77

Spring Data Cassandra Counter update

In the spring data documentation, it says:

CassandraTemplate is the place to look for accessing functionality such as incrementing counters or ad-hoc CRUD operations.

I am trying to update a counter using the CassandraTemplate in spring-data-cassandra, but the documentation on this subject is very sparse. Is there a good example of this?

Upvotes: 3

Views: 2319

Answers (2)

wsl
wsl

Reputation: 10325

You can also use custom @Query to update the counter value.

For example, supposing you have some repository:

public interface SomeRepository extends CassandraRepository<SomeEntity> {

@Query("update some_table SET counter=counter+1 WHERE value1 = ?0 AND value2= ?1;")
Object updateCounterValue(String value1, String value2);}

Of course you can also parametrize the value being updated.

Upvotes: 3

npenkov
npenkov

Reputation: 131

I haven't found any good example neither, but from the source of CassandraTemplate/CqlTempleate, code should look like:

Update updateOp = QueryBuilder.update(cassandraOperations.getTableName(MyMapped.class).toCql());
        updateOp.with(QueryBuilder.incr("my_count_column"))
                        .where(QueryBuilder.eq(...) ); // Primary key clause
cassandraOperations.execute(updateOp);

Upvotes: 3

Related Questions