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');
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 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.
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.
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.
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');
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With