UUID and Timeuuid functions in Cassandra

19 May, 2021
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  
Output: 
 
This CQL query is correct to insert Id value using uuid() function.  
Insert into function4(Id, name) 
values (now(), ‘Ashish’);  //correct 
Output: 
 
Some additional timeuuid() functions:  
dateof() : This function returns the extracted timestamp as a date.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().  
dateof() : This function returns the extracted timestamp as a date.
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().  
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; 
Output:  
Output:  
Output: 
 
Now, let’s check how todate() function works.  
Now, let’s check how todate() function works.  
Now, let’s check how todate() function works.  
select todate(modified_date) 
from function_test1; 
Output:  
Output:  
Output: 
 
For later version >2.2.0 in Cassandra we can used toTimestamp(now()) function.  
For later version >2.2.0 in Cassandra we can used toTimestamp(now()) function.  
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; 
Output:  
Output:  
Output: 
 
 minTimeuuid() and maxTimeuuid() : Both function is used to find minimum and maximum timeuuid respectively. minTimeuuid() function returns minimumm Timeuuid value and maxTimeuuid() function returns maximum Timeuuid value.  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.
 
 
minTimeuuid() and maxTimeuuid() : Both function is used to find minimum and maximum timeuuid respectively. minTimeuuid() function returns minimumm Timeuuid value and maxTimeuuid() function returns maximum Timeuuid value.  
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. 
 
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()), 
                     49d59e61-961b-11e8-9854-134d5b3f9cf8, 
                       toTimestamp(now()));  
 
SELECT * 
from function3; 
Output: 
 
Figure – Output of function3 table 
nidhi_biet
gabaa406
Apache
DBMS
DBMS
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.