Search code examples
cassandracqlcql3

How to manipulate timestamp columns in Apache Cassandra


I have a table with a timestamp column, and I'd like to manipulate the values of that column. For instance, I need to do something along the line:

UPDATE mytable SET datetimecolumn = datetimecolumn + 10mins

How is it done in Apache Cassandra?

UPDATE: The answer seems to be "you can't". But the selected answer is the closest we can get apparently.


Solution

  • You can query similar this one, only if the data type is counter.

    Using Counter :

    A counter is a special column used to store a number that is changed in increments. For example, you might use a counter column to count the number of times a page is viewed.

    Define a counter in a dedicated table only and use the counter data type. You cannot index, delete, or re-add a counter column. All non-counter columns in the table must be defined as part of the primary key.

    Example :

    CREATE TABLE mytable (
       pk1 int PRIMARY KEY,
       datetimecolumn counter
    );
    

    Here you have to use datetimecolumn value in millisecond.
    For the first time, you have to use update query with the time in millisecond value let's say 1487686182403

    UPDATE mytable SET datetimecolumn = datetimecolumn + 1487686182403 where pk1 = 1
    

    Now mytable with pk = 1 contains datetimecolumn = 1487686182403 value.

    If you want to increment datetimecolumn by 10mins (600000 millisecond)

    UPDATE mytable SET datetimecolumn = datetimecolumn + 600000 where pk1 = 1
    

    Source : https://docs.datastax.com/en/cql/3.1/cql/cql_using/use_counter_t.html