Expiring data example

Using the INSERT and UPDATE commands for setting the expire time for data in a column.

Both the INSERT and UPDATE commands support setting a time for data in a column to expire. The expiration time (TTL) is set using CQL.

Procedure

  1. Use the INSERT command to set a password column in the users table to expire in 86400 seconds, or one day.
    
    cqlsh:demodb> INSERT INTO users
                    (user_name, password)
                    VALUES ('cbrown', 'ch@ngem4a') USING TTL 86400;
  2. Extend the expiration period to five days by using the UPDATE command/
    
    cqlsh:demodb> UPDATE users USING TTL 432000 SET password = 'ch@ngem4a'
                    WHERE user_name = 'cbrown';

    You can set a default TTL for an entire table by setting the table's default_time_to_live property. If you try to set a TTL for a specific column that is longer than the time defined by the table TTL, Apache Cassandra returns an error.