How to auto generate uuid in cassandra CQL 3 command line
Asked Answered
L

4

53

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');
Logical answered 30/7, 2013 at 11:5 Comment(0)
C
55

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.

Cancroid answered 30/7, 2013 at 11:10 Comment(6)
Is there any disadvantage to using timeuuid vs uuid?Logical
The main disadvantage is it leaks the MAC address of the creator and the time. But the advantages are its uniqueness is deterministic and you can sort events by time.Cancroid
Thanks. I appreciate your detailed reply!Logical
Actually, a TimeUUID is a UUID. Just like a Braeburn apple is an apple. Read the Cassandra doc and my longer answer: #17946177Paulettapaulette
this is not the best answer, check out the one from ZhivkoTung
@Cancroid it seems to me that it is generated on the coordinator node so doesn't leak the local MAC address? Anyway this way is sortable by time, not sure why this way would be disdained...Danny
M
93

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');
Matchless answered 14/8, 2014 at 22:5 Comment(4)
Thanks for the updated answer for newer versions of cassandra!Logical
@gotch4 yes, they are unique. For all practical purposes.Christabella
@TorstenBronger would you be able to help me understand how uuid() is unique for a table?Dairen
“UUID” stands for “universally unique identifier”. It is supposed to be unique even beyond the table. Its Wikipedia article has the details.Christabella
C
55

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.

Cancroid answered 30/7, 2013 at 11:10 Comment(6)
Is there any disadvantage to using timeuuid vs uuid?Logical
The main disadvantage is it leaks the MAC address of the creator and the time. But the advantages are its uniqueness is deterministic and you can sort events by time.Cancroid
Thanks. I appreciate your detailed reply!Logical
Actually, a TimeUUID is a UUID. Just like a Braeburn apple is an apple. Read the Cassandra doc and my longer answer: #17946177Paulettapaulette
this is not the best answer, check out the one from ZhivkoTung
@Cancroid it seems to me that it is generated on the coordinator node so doesn't leak the local MAC address? Anyway this way is sortable by time, not sure why this way would be disdained...Danny
A
21

Actually there is a way to do that using the blob conversion functions - blobAsType() and typeAsBlob(). In your case this should be:

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

This converts timeuuid to blob and from the blob converts it to uuid.

Ator answered 2/4, 2014 at 10:14 Comment(2)
This actually answers the question. Nice.Meditation
Also believe this is the correct answer. The thing is we don't need to change the type from uuid to timeuuidMcdaniel
N
14

A UUID is a Universally Unique ID used to avoid collisions.

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.

The difference between Type 1 and Type 4 UUIDs is that a Type 1 UUID is generated using a timestamp and a Type 4 is generated using random numbers.

If you want to use a timeuuid as a uuid use something like blobAsUuid(timeuuidAsBlob(now())), since the value returned by now() is guaranteed to be unique.

References:

http://docs.datastax.com/en/cql/3.3/cql/cql_reference/uuid_type_r.html

http://docs.datastax.com/en/cql/3.3/cql/cql_reference/timeuuid_functions_r.html

http://docs.datastax.com/en/cql/3.3/cql/cql_reference/blob_r.html

Naumann answered 13/8, 2015 at 5:2 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.