Wednesday 12 February 2020

Cassandra: USING TTL: Set expiration time

TTL stands for ‘Time To Live’. 'USING TTL' clause is used to set time for data in a column to expire. You can use this column both in INSERT, UPDATE commands.

INSERT INTO cassandratutorial.employee JSON '{"id" : 1, "firstName" : "Krishna", "lastName" : "Gurram", "age" : 30}' USING TTL 300;

Insert statement sets the TTL to entire row.

UPDATE cassandratutorial.employee USING TTL 3600 SET firstname='RAMA' WHERE id=1;

How to get the TTL for a column value?
SELECT TTL(firstname) FROM cassandratutorial.employee WHERE id=1;

Sample CQL console log is given below.

cqlsh> CREATE keyspace cassandratutorial WITH REPLICATION = 
   ... { 
   ... 'class' : 'SimpleStrategy', 
   ... 'replication_factor' : 1 
   ... };
cqlsh> 
cqlsh> CREATE TABLE IF NOT EXISTS cassandratutorial.employee (
   ...   id INT PRIMARY KEY, 
   ...   firstName VARCHAR,
   ...   lastName VARCHAR,
   ...   age int,
   ... ) WITH comment = 'Table to store Employee information';
cqlsh> 
cqlsh> INSERT INTO cassandratutorial.employee JSON '{"id" : 1, "firstName" : "Krishna", "lastName" : "Gurram", "age" : 30}' USING TTL 300;
cqlsh>   
cqlsh> SELECT TTL(firstname) FROM cassandratutorial.employee WHERE id=1;

 ttl(firstname)
----------------
            228

(1 rows)
cqlsh>
cqlsh> UPDATE cassandratutorial.employee USING TTL 3600 SET firstname='RAMA' WHERE id=1;
cqlsh> 
cqlsh> SELECT TTL(firstname) FROM cassandratutorial.employee WHERE id=1;

 ttl(firstname)
----------------
           3597

(1 rows)
cqlsh> SELECT * FROM cassandratutorial.employee;

 id | age | firstname | lastname
----+-----+-----------+----------
  1 |  30 |      RAMA |   Gurram

(1 rows)
cqlsh> SELECT TTL(firstname) FROM cassandratutorial.employee WHERE id=1;

 ttl(firstname)
----------------
           3485

(1 rows)


Previous                                                    Next                                                    Home

No comments:

Post a Comment