Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Just set the TTL on a row

Tags:

cassandra

Using Java, can I scan a Cassandra table and just update the TTL of a row? I don't want to change any data. I just want to scan Cassandra table and set TTL of a few rows.

Also, using java, can I set TTL which is absolute. for example (2016-11-22 00:00:00). so I don't want to specify the TTL in seconds, but specify the absolute value in time.

like image 860
Knows Not Much Avatar asked Nov 21 '16 22:11

Knows Not Much


People also ask

How do I select TTL?

To set the TTL for data, use the USING TTL keywords. The TTL function may be used to retrieve the TTL information. The USING TTL keywords can be used to insert data into a table for a specific duration of time. To determine the current time-to-live for a record, use the TTL function.

How does TTL work in Cassandra?

In Cassandra Both the INSERT and UPDATE commands support setting a time for data in a column to expire. 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. We can use TTL function to get the time remaining for a specific selected query.

What happens when TTL expires in Cassandra?

Cassandra provides functionality by which data can be automatically expired. During data insertion, you have to specify 'ttl' value in seconds. 'ttl' value is the time to live value for the data. After that particular amount of time, data will be automatically removed.


3 Answers

Cassandra doesn't allow to set the TTL value for a row, it allows to set TTLs for columns values only.

In the case you're wondering why you are experiencing rows expiration, this is because if all the values of all the columns of a record are TTLed then the row disappears when you try to SELECT it.

However, this is only true if you perform an INSERT with the USING TTL. If you INSERT without TTL and then do an UPDATE with TTL you'll still see the row, but with null values. Here's a few examples and some gotchas:


Example with a TTLed INSERT only:

CREATE TABLE test (
    k text PRIMARY KEY,
    v int,
);

INSERT INTO test (k,v) VALUES ('test', 1) USING TTL 10;

... 10 seconds after...

SELECT * FROM test ;

 k             | v
---------------+---------------

Example with a TTLed INSERT and a TTLed UPDATE:

INSERT INTO test (k,v) VALUES ('test', 1) USING TTL 10;
UPDATE test USING TTL 10 SET v=0 WHERE k='test';

... 10 seconds after...

SELECT * FROM test;

 k             | v
---------------+---------------

Example with a non-TTLed INSERT with a TTLed UPDATE

INSERT INTO test (k,v) VALUES ('test', 1);
UPDATE test USING TTL 10 SET v=0 WHERE k='test';

... 10 seconds after...

SELECT * FROM test;

 k             | v
---------------+---------------
          test |          null

Now you can see that the only way to solve you problem is to rewrite all the values of all the columns of your row with a new TTL.

In addition, there's no way to specify an explicit expiration date, but you can get a simple TTL value in seconds with simple math (as other suggested).

Have a look at the official documentation about data expiration. And don't forget to have a look at the DELETE section for updating TTLs.

HTH.

like image 85
xmas79 Avatar answered Oct 17 '22 04:10

xmas79


You can't only update TTL of a row. You have to update or re-insert all the column.

You can select all the regular column and the primary keys column then update the regular columns with primary keys or re-insert using TTL in second

In Java you can calculate TTL in second from a date using below method.

public static long ttlFromDate(Date ttlDate) throws Exception {
    long ttl = (ttlDate.getTime() - System.currentTimeMillis()) / 1000;
    if (ttl < 1) {
        throw new Exception("Invalid ttl date");
    }
    return ttl;
}
like image 39
Ashraful Islam Avatar answered Oct 17 '22 03:10

Ashraful Islam


Alternatively, you can set a TTL value on the entire table while creating it.

CREATE TABLE test (
    k text PRIMARY KEY,
    v int,
) WITH default_time_to_live = 63113904;

Above example will create a table whose rows will disappear after 2 years.

like image 1
bekce Avatar answered Oct 17 '22 03:10

bekce