Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to generate TimeUUID in Java/Scala

Does anyone know how to generate TimeBased UUIDs in Java/Scala?

Here is the column family:

CREATE table col(ts timeuuid)

I'm using Cassandra 1.2.4

Appreciate your help!

like image 362
Avis Avatar asked Jul 25 '14 09:07

Avis


3 Answers

If you are using the Datastax drivers you can use the utility class, UUIDs, to generate one

import com.datastax.driver.core.utils.UUIDs;
....
UUID timeBasedUuid = UUIDs.timeBased();
like image 75
Eduardo Dennis Avatar answered Oct 21 '22 22:10

Eduardo Dennis


Cassandra has UUIDGen for generating Timeuuids. The source for this is here:

https://github.com/apache/cassandra/blob/cassandra-2.1/src/java/org/apache/cassandra/utils/UUIDGen.java

like image 9
mikea Avatar answered Oct 21 '22 22:10

mikea


i am using the same way for cassandra cli, and for column name i am using

System.currentTimeMillis().toString

scala>     val timestamp = System.currentTimeMillis().toString
timestamp: String = 1406279679674

UPDATE

mainly it depends on your row key if rowKey is your userId or something, then there is no chance of submission of duplicate record in miliseconds but if you think it can be repeat then use

val timestamp = com.eaio.uuid.UUIDGen.newTime().toString
like image 4
Govind Singh Avatar answered Oct 21 '22 23:10

Govind Singh