![]() |
VOOZH | about |
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:
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;
select todate(modified_date) from function_test1;
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;
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()), 49d59e61-961b-11e8-9854-134d5b3f9cf8, toTimestamp(now()));
SELECT * from function3;
Output:
Figure - Output of function3 table