Open In App

UUID and Timeuuid functions in Cassandra

Last Updated : 08 Sep, 2022
Like Article

Prerequisite – Cassandra 
In this article, we are going to discuss uuid() function which is very important to insert value and to uniquely generates “guaranteed unique” UID value Universally. 

One of the reason of using uuid() function to generate Unique ID which helps in avoiding collisions. The uuid() function is suitable for use in insert or update statements and uuid() function takes no parameter value to generate a unique random Type 4 UUID value which is guaranteed unique value. Let’s take an example to understand the uuid() function. 

Create table function4(Id uuid primary key, name text); 

This CQL query is NOT correct to insert Id value using uuid() function. 

Insert into function4 (Id, name) 
values (1, ‘Ashish’); // fails  



This CQL query is correct to insert Id value using uuid() function. 

Insert into function4(Id, name) 
values (now(), ‘Ashish’);  //correct 



Some additional timeuuid() functions: 

  1. dateof() : 
    This function returns the extracted timestamp as a date.
  2. now() : 
    In Cassandra Query Language now() function can be used for UTC (Universal Time) standard. Now() method is useful to insert value which is guaranteed to be unique. 

    To insert the current time as a value then we can use timeuuid functions now() and dateof(). 

CREATE TABLE function_test1(Id uuid primary key,
                                        name text,
                           modified_date timestamp );

INSERT INTO function_test1(Id, name, modified_date) 
       VALUES (now(), 'Rana', '2019-10-29 00:05+0000');
INSERT INTO function_test1(Id, name, modified_date) 
       VALUES (now(), 'Rana', '2019-10-30 00:05+0000');

select * 
from function_test1; 
  1. Output: 


  1. Now, let’s check how todate() function works. 

select todate(modified_date) 
from function_test1; 
  1. Output: 


  1. For later version >2.2.0 in Cassandra we can used toTimestamp(now()) function. 

CREATE TABLE function_test2(Id uuid primary key,
                                        name text,
                         modified_date timestamp );

INSERT INTO function_test2 (Id, name, modified_date) 
       VALUES (now(), 'Rana', toTimestamp(now()));

Select * 
from function_test2; 
  1. Output: 



  2. minTimeuuid() and maxTimeuuid() : 
    Both function is used to find minimum and maximum timeuuid respectively. minTimeuuid() function returns minimum Timeuuid value and maxTimeuuid() function returns maximum Timeuuid value. 
  3. unixTimestampOf() : 
    In Cassandra Query Language unixTimestampOf() functions the timestamp in ms(milliseconds)of a timeuuid column in a result set. unixTimestampOf() functions returns 64 bit integer timestamp.

In the new version of Cassandra to manipulate date support some additional timeuuid and timestamp functions. It can be used for insert, update, and select statements. 


Additional timeuuid and timestamp functions Name Convert
toDate(timeuuid) From timeuuid to date[YYYY-MM-DD] format.
toTimestamp(timeuuid) From timeuuid to timestamp format.
toUnixTimestamp(timestamp) From timeuuid to UNIX timestamp format.
toDate(timestamp) From timestamp to date[YYYY-MM-DD] format.
toUnixTimestamp(timestamp) From timestamp to UNIX timestamp format.
toTimestamp(date) From date to timestamp format.
toUnixTimestamp(date) From date to UNIX timestamp format.

Let’s understand with an example, 


CREATE TABLE function3( Col1 int, Col2 timestamp, 
                             Col3 timeuuid, Col4 bigint, 
                  PRIMARY KEY(Col1, Col2, Col3, Col4)); 


Insert into function3(Col1, Col2, Col3, Col4) 
            Values (9, toUnixTimestamp(now()), 


from function3; 



Figure – Output of function3 table

Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads