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

Charlie Parker picture Charlie Parker · Apr 4, 2014 · Viewed 14.9k times · Source

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!

Answer

Martin Serrano picture Martin Serrano · Apr 4, 2014

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.