Set TTL per column at insert time

15 views
Skip to first unread message

nilanjan sarkar

unread,
Aug 21, 2016, 10:10:33 AM8/21/16
to DataStax Java Driver for Apache Cassandra User Mailing List
Hi Guys,

I have a requirement, where i need to set TTL on only one column of a table.

What is the cql sematic for the particular insert statement?

Currently I see that the TTL on insert statement is only supported at row level, but not at a column level.

Thanks,
Nilanjan

Kevin Gallardo

unread,
Aug 22, 2016, 10:19:04 AM8/22/16
to java-dri...@lists.datastax.com
Hi, 

TTL doesn't really work per row but more per column. Although while I think there is no syntax for specifying a TTL per column during an INSERT, you can do an UPDATE query on the desired column with a specified TTL, it would affect only the modified column. Here's an example:

cqlsh> create table ttltest(id int primary key, val text, val2 text);
cqlsh> insert into ttltest(id, val, val2) values (1, '1', '11');
cqlsh> select id, ttl(val), ttl(val2) from ttltest;

 id | ttl(val) | ttl(val2)
----+----------+-----------
  1 |     null |      null

(1 rows)
cqlsh> update ttltest using ttl 7000 set val = '1' where id = 1;
cqlsh> select id, ttl(val), ttl(val2) from ttltest;

 id | ttl(val) | ttl(val2)
----+----------+-----------
  1 |     6996 |      null

(1 rows)

Or do 2 inserts, one with the columns that don't need a TTL, and another one, on the same primary key that has the other column, with the TTL:

cqlsh> create table ttltest(id int primary key, val text, val2 text);
cqlsh> insert into ttltest(id, val) values (1, '1');
cqlsh> select id, ttl(val), ttl(val2) from ttltest;

 id | ttl(val) | ttl(val2)
----+----------+-----------
  1 |     null |      null

(1 rows)
cqlsh> insert into ttltest(id, val2) values (1, '11') using ttl 7000;
cqlsh> select id, ttl(val), ttl(val2) from ttltest;

 id | ttl(val) | ttl(val2)
----+----------+-----------
  1 |     null |      6995

(1 rows)

Hope that helps.

--
You received this message because you are subscribed to the Google Groups "DataStax Java Driver for Apache Cassandra User Mailing List" group.
To unsubscribe from this group and stop receiving emails from it, send an email to java-driver-user+unsubscribe@lists.datastax.com.



--
Kévin Gallardo.
Software Engineer in Drivers and Tools Team at DataStax.
 

nilanjan sarkar

unread,
Aug 22, 2016, 3:22:53 PM8/22/16
to DataStax Java Driver for Apache Cassandra User Mailing List
Hi Kevin,

Thanks for your reply. That is how we are planning to do it :)
To unsubscribe from this group and stop receiving emails from it, send an email to java-driver-us...@lists.datastax.com.
Reply all
Reply to author
Forward
0 new messages