Open In App

Time To Live (TTL) for a column in Cassandra

Last Updated : 27 Nov, 2019
Improve
Improve
Like Article
Like
Save
Share
Report

In this article we will discuss how to insert and update using Time To Live (TTL) command and how to determine the expire time limit of an existing column.
In Cassandra Time to Live (TTL) is play an important role while if we want to set the time limit of a column and we want to automatically delete after a point of time then at the time using TTL keyword is very useful to define the time limit for a particular column.

  1. In Cassandra Both the INSERT and UPDATE commands support setting a time for data in a column to expire.
  2. It is used to set the time limit for a specific period of time. By USING TTL clause we can set the TTL value at the time of insertion.
  3. We can use TTL function to get the time remaining for a specific selected query.
  4. At the point of insertion, we can set expire limit of inserted data by using TTL clause. Let us consider if we want to set the expire limit to two days then we need to define its TTL value.
  5. By using TTL we can set the expiration period to two days and the value of TTL will be 172800 seconds. Let’s understand with an example.

Table : student_Registration
To create the table used the following CQL query.

CREATE TABLE student_Registration(
Id int PRIMARY KEY,
Name text,
Event text
);

Insertion using TTL :
To insert data by using TTL then used the following CQL query.

INSERT INTO student_Registration (Id, Name, Event) 
       VALUES (101, 'Ashish', 'Ninza') USING TTL 172800;
INSERT INTO student_Registration (Id, Name, Event) 
       VALUES (102, 'Ashish', 'Code') USING TTL 172800;
INSERT INTO student_Registration (Id, Name, Event) 
       VALUES (103, 'Aksh', 'Ninza') USING TTL 172800; 

Output:

Id Name Event
101 Ashish Ninza
102 Ashish Code
103 Aksh Ninza

Now, to determine the remaining time to expire for a specific column used the following CQL query.

SELECT TTL (Name) 
from student_Registration 
WHERE Id = 101; 

Output:

ttl(Name)
172700

It will decrease as you will check again for its TTL value just because of TTL time limit. Now, used the following CQL query to check again.

SELECT TTL (Name) 
from student_Registration 
WHERE Id = 101; 

Output:

ttl(Name)
172500

Updating using TTL:
Now, if we want to extend the time limit then we can extend with the help of UPDATE command and USING TTL keyword. Let’s have a look. To extend time limit with 3 days and also to update the name to ‘rana’ then used the following CQL query.

UPDATE student_Registration
USING TTL 259200 
SET Name = 'Rana' 
WHERE Id= 102 

Output:

Id Name Event
101 Ashish Ninza
102 Rana Code
103 Aksh Ninza

SELECT TTL (Name) 
from student_Registration 
WHERE Id = 102; 

Output:

ttl(Name)
259100

Deleting a column using TTL:
To delete the specific existing column used the following CQL query.

UPDATE student_Registration
USING TTL 0 
SET Name = 'Ashish' 
WHERE Id = 102; 

Note: We can set the default TTL for entire table using Default value of TTL.

Reference – https://docs.datastax.com/


Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads