Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you insert custom timeuuid's to cassandra without the now() function?

I was trying to insert a specific timeuuid to cassandra and the only way I managed to insert one was using the now() function, because I assume, the now function knows what format the database likes it.

How do I create cqlsh command for this?

Currently I have this:

INSERT INTO my_table (tid) VALUES ( now() )

But I would like to be able to have 100% control of what date I insert for testing purposes when I am debugging my node.js or whatever program interfacing cassandra.

It would be nice to have something like:

INSERT INTO my_table (tid) VALUES ( 12/OCTOBER/2014 )

without it crashing

Thanks!

like image 509
Charlie Parker Avatar asked Apr 03 '14 22:04

Charlie Parker


3 Answers

timeuuid's are a complex format. Valid values are described here.

timeuuid

Uses the time in 100 nanosecond intervals since 00:00:00.00 UTC (60 bits), a 
clock sequence number for prevention of duplicates (14 bits), plus the IEEE 801 MAC
address (48 bits) to generate a unique identifier. For example: 
d2177dd0-eaa2-11de-a572-001b779c76e3 

This has a good discussion of timeuuid. I do not know what order these bits appear in, but if it is from left to right you could concat:

Time (first 15 digits = 60 bits): 00000000-0000-000

Sequence (next 3 digits = 12 bits, ignores last 2 bits of sequence): 0-00

Last 2 bits sequence + MAC: 000-000000000000

Then increment the Time or the Sequence as needed for entries. But it would probably be a lot easier to just work with timestamps.

If you want to do this:

INSERT INTO my_table (tid) VALUES ( "2014-10-12" )

you need to use the timestamp type.

like image 86
Martin Serrano Avatar answered Jan 04 '23 05:01

Martin Serrano


I you have 2 options:

  1. use one of the minTimeuuid or maxTimeuuid described here
  2. Implement a Timeuuid value in Node.js by using the details in UUIDGen

I'd say the first approach would be easier for testing purposes.

like image 26
Alex Popescu Avatar answered Jan 04 '23 04:01

Alex Popescu


Take a look at minTimeuuid and maxTimeuuid functions. It's a bad idea to insert their result, but for testing purposes it can be OK.

http://cassandra.apache.org/doc/cql3/CQL.html#timeuuidFun

like image 21
Mikhail Stepura Avatar answered Jan 04 '23 04:01

Mikhail Stepura