Database Reference
In-Depth Information
Expiring columns with TTL
We first encountered the CQL DELETE command, which allows us to remove the value of
a column or an entire row, in Chapter 5 , Establishing Relationships . Another way to re-
move data from Cassandra is to give it a time-to-live ( TTL ). This is equivalent to deleting
data in the future: when we write data with a TTL, it is marked as deleted after the amount
of time we specify has elapsed.
CQL allows us to attach a TTL to any INSERT or UPDATE statement. Let's experiment
with setting an expiring location value:
UPDATE "users"
USING TTL 30
SET "location" = 'Vancouver'
WHERE "username" = 'bob';
TTL values are given in seconds, so what we've done here is tell Cassandra that bob 's loc-
ation should be Vancouver for the next thirty seconds, after which it should be deleted.
CQL also allows us to introspect the TTL attached to any data column using the TTL func-
tion:
SELECT "username", "location", TTL("location")
FROM "users"
WHERE "username" = 'bob';
We'll see that the TTL column in the result tells us how many seconds remain until the
location field expires:
If we now wait the full thirty seconds and then check bob 's user record again, we'll see that
the location value has disappeared.
Search WWH ::




Custom Search