Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to auto generate uuid in cassandra CQL 3 command line

Just learning cassandra, is there a way to insert a UUID using CQL, ie

create table stuff (uid uuid primary key, name varchar); insert into stuff (name) values('my name'); // fails insert into stuff (uid, name) values(1, 'my name'); // fails 

Can you do something like

insert into stuff (uid, name) values(nextuid(), 'my name'); 
like image 848
Jay Avatar asked Jul 30 '13 11:07

Jay


People also ask

Can Cassandra generate UUID?

Cassandra 2.0. 7 and later versions include the uuid() function that takes no parameters and generates a Type 4 UUID for use in INSERT or SET statements. You can also use a timeuuid type with a function like now() . They generate a Type 1 UUID.

What is UUID data type in Cassandra?

The UUID (universally unique id) comparator type for avoiding collisions in column names. The UUID (universally unique id) comparator type is used to avoid collisions in column names. Alternatively, you can use the timeuuid. Timeuuid types can be entered as integers for CQL input.

Why do we use UUID in Cassandra to uniquely identify records?

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.

What is a Timeuuid?

A TimeUUID is a plain old UUID according to the documentation. A UUID is simply a 128-bit value. Think of it as an unimaginably large number. The particular bits may be determined by any of several methods.


2 Answers

As of Cassandra 2.0.7 you can just use uuid(), which generates a random type 4 UUID:

INSERT INTO users(uid, name) VALUES(uuid(), 'my name'); 
like image 171
lumi Avatar answered Sep 20 '22 09:09

lumi


You can with time uuids (type 1 UUID) using the now() function e.g.

insert into stuff (uid, name) values(now(), 'my name'); 

Works with uid or timeuuid. It generates a "guaranteed unique" UID value, which also contains the timestamp so is sortable by time.

There isn't such a function for type 4 UUIDs though.


UPDATE: This note pertains to older versions of Cassandra. For newer versions, see below.

like image 26
Richard Avatar answered Sep 20 '22 09:09

Richard