6

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!

Charlie Parker
  • 5,884
  • 57
  • 198
  • 323

3 Answers3

3

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.

Community
  • 1
  • 1
Martin Serrano
  • 3,727
  • 1
  • 35
  • 48
  • sorry to be snarky. datastax is a great place to go for cassandra doc. – Martin Serrano Apr 03 '14 at 22:30
  • don't worry, you don't have to apologize. I was just so surprised/annoyed/shocked that the source I was looking at didn't have a clear explanation of the syntax. But reckoned that if I had the problem, probably someone else would have it in the future and it wouldn't hurt to help future people. Thanks anyway! :) – Charlie Parker Apr 03 '14 at 22:33
  • I am not sure why its not working do you have an idea?, INSERT INTO commits (hash, keyframe, tid) VALUES (0x000000001bfdeb0a304b372dd8dda123b3fd1a00, true, "2012-01-01"); – Charlie Parker Apr 03 '14 at 22:46
  • Bad Request: line 1:100 no viable alternative at input '2012-01-01' – Charlie Parker Apr 03 '14 at 22:46
  • huh. what does `describe table commits` return? – Martin Serrano Apr 03 '14 at 22:50
  • CREATE TABLE commits ( hash blob, keyframe boolean, tid timeuuid, PRIMARY KEY (hash) ) – Charlie Parker Apr 03 '14 at 22:52
  • WITH bloom_filter_fp_chance=0.010000 AND caching='KEYS_ONLY' AND comment='' AND dclocal_read_repair_chance=0.000000 AND gc_grace_seconds=864000 AND index_interval=128 AND read_repair_chance=0.100000 AND replicate_on_write='true' AND populate_io_cache_on_flush='false' AND default_time_to_live=0 AND speculative_retry='99.0PERCENTILE' AND memtable_flush_period_in_ms=0 AND compaction={'class': 'SizeTieredCompactionStrategy'} AND compression={'sstable_compression': 'LZ4Compressor'}; – Charlie Parker Apr 03 '14 at 22:53
  • my bad, i was writing about timestamp type. i have updated my answer – Martin Serrano Apr 03 '14 at 22:57
  • so what does d2177dd0-eaa2-11de-a572-001b779c76e3 stand for? – Charlie Parker Apr 03 '14 at 23:00
  • updated with an explanation and how you could do it. i have not done this before but it should work – Martin Serrano Apr 03 '14 at 23:35
3

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.

Alex Popescu
  • 3,982
  • 18
  • 20
  • Hi @AlexPopescu, Is there an updated documentation for this? `minTimeuuid` and `maxTimeuuid` no longer accept serialized dates as arguments from what I am able to tell. – flavian Feb 18 '16 at 17:48
  • @flavian: no longer as in what Cassandra version, what cqlsh, etc. etc. – Alex Popescu Feb 19 '16 at 08:11
  • Cassandra 2.2.0. I created a full spec issue here: https://datastax-oss.atlassian.net/projects/JAVA/issues/JAVA-1084?filter=allopenissues – flavian Feb 19 '16 at 08:55
2

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

Mikhail Stepura
  • 3,374
  • 20
  • 16
  • doing minTimeuuid('2014-10-12') worked for me in cqlsh during an insert query to the cassandra database. Apparently, single vs double quotes might make a difference (did for me). Careful with that. – Charlie Parker Apr 06 '14 at 19:42
  • Could we possibly have a little more detail at to why it is a "bad idea" except in testing? – JGleason Sep 10 '19 at 15:34
  • @JGleason Because we will have the same id from call `minTimeuuid` or `maxTimeuuid` with the same data. It's not true UUID, it's just top and bottom border values. – Max Nov 09 '19 at 18:17