gautam suddapalli
gautam suddapalli

Reputation: 65

How do you change a column value after a certain time interval - Cassandra

I have a column family in Cassandra in which one of the column name is status with value Y. This status has to be updated to a new value - X after 30 minutes if it wasn't updated to some other value say N within the 30 minutes.

Basically, I want to mark the column to Expired - X if there wasn't any update within the specified time interval.

Also, once this is expired, it needs to trigger off an event to notify another system(API) saying that it was expired.

Upvotes: 2

Views: 689

Answers (2)

mp911de
mp911de

Reputation: 18129

Agree with Ashraful's answer. If you use Spring Data for Apache Cassandra 2.0 (currently milestone), then you would use the following code to implements Ashraful's example:

@Table("users")
public class User {

    @PrimaryKey("user_name")
    String userName;
    String password;
}

WriteOptions options = WriteOptions.builder().ttl(1800).build();

template.update(query(where("userName").is("cbrown")).queryOptions(options), 
                update("password", "ch@ngem4a"), 
                User.class);

Upvotes: 1

Ashraful Islam
Ashraful Islam

Reputation: 12840

You can try Cassandra's TTL (Time to Live) Feature

You can set an optional expiration period called TTL (time to live) for data in a column, other than a counter column. The TTL value for a column is a number of seconds. After the number of seconds since the column's creation exceeds the TTL value, TTL data is considered expired and is included in results

Every time you update the field, use TTL of 30 min or 1800 seconds

Example :

UPDATE users USING TTL 1800 SET password = 'ch@ngem4a' WHERE user_name = 'cbrown';

For first time insert, also use the above query.

So for above example, the password field will mark as deleted after 30 min, if it is not updated within 30 min.

But it is not possible to trigger off an event to notify your system.

Note : TTL or delete generate tombstone, Huge amount of tombstone can slow down your read performance

Read More about tombstone

Upvotes: 2

Related Questions