best_of_man
best_of_man

Reputation: 738

What type should I use to store binary UUID values?

I have a binary 16 UUID like this:

<Buffer 11 ed a7 28 25 a2 57 40 80 61 81 ed 9d 30 12 8e> 

That comes from using this binary-uuid library in NodeJS.

What type should I use while creating my table schema in Cassandra?

Upvotes: 0

Views: 135

Answers (2)

clunven
clunven

Reputation: 1695

Cassandra works with UUID rather than sequences or integer auto increments. The reason comes from the distributed nature of Cassandra, you want a node to generate a new randomized number without having to lock other nodes.

So UUID is an official type of CQL you want to use in your design.

CREATE TABLE IF NOT EXISTS something_by_id (
   uid        uuid,
   name       text
   PRIMARY KEY ((uid))
);

How to use it with NodejS ?

  • Import the cassandra drivers: npm install cassandra-driver

  • Import the type in your file const Uuid = require('cassandra-driver').types.Uuid;

  • Use it const id = Uuid.random();

More details can be found here. Note the double parenthesis in the table definition. It is important to understand how a primary is built with Cassandra with its 2 parts partition keys and clustering clumns not serving the same purpose.

Upvotes: 1

Erick Ramirez
Erick Ramirez

Reputation: 16413

To answer your question directly, binary data can be stored in a CQL blob type.

However, it's not clear to me whether (a) your use case really stores binary UUIDs, or (b) you are using artificial IDs as the partition key.

If it's (b) then I would recommend using natural IDs. For example, if you have a table of users then use usernames or email addresses. They are much better than using artificial IDs. Cheers!

Upvotes: 1

Related Questions