How to get current timestamp with CQL while using Command Line?

后端 未结 3 1414
一个人的身影
一个人的身影 2020-12-28 12:54

I am trying to insert into my CQL table from the command line. I am able to insert everything. But I am wondering if I have a timestamp column, then how can I insert into ti

相关标签:
3条回答
  • 2020-12-28 13:14

    There are actually 2 different ways for different purposes to insert the current timestamp. From the docs:

    Inserting the current timestamp

    Use functions to insert the current date into date or timestamp fields as follows:

    Current date and time into timestamp field: toTimestamp(now()) sets the timestamp to the current time of the coordinator.

    Current date (midnight) into timestamp field: toTimestamp(toDate(now())) sets the timestamp to the current date beginning of day (midnight).

    0 讨论(0)
  • 2020-12-28 13:31

    You can use the timeuuid functions now() and dateof() (or in later versions of Cassandra, toTimestamp()), e.g.,

    INSERT INTO TEST (ID, NAME, VALUE, LAST_MODIFIED_DATE)
                      VALUES ('2', 'elephant',  'SOME_VALUE', dateof(now()));
    

    The now function takes no arguments and generates a new unique timeuuid (at the time where the statement using it is executed). The dateOf function takes a timeuuid argument and extracts the embedded timestamp. (Taken from the CQL documentation on timeuuid functions).

    Cassandra >= 2.2.0-rc2

    dateof() was deprecated in Cassandra 2.2.0-rc2. For later versions you should replace its use with toTimestamp(), as follows:

    INSERT INTO TEST (ID, NAME, VALUE, LAST_MODIFIED_DATE)
                      VALUES ('2', 'elephant',  'SOME_VALUE', toTimestamp(now()));
    
    0 讨论(0)
  • 2020-12-28 13:34

    In new version of cassandra could use toTimestamp(now()), and note that function dateof is deprecated.

    e.g

    insert into dummy(id, name, size, create_date) values (1, 'Eric', 12, toTimestamp(now()));
    
    0 讨论(0)
提交回复
热议问题