How to generate TimeUUID in Java / Scala

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

Here is the column family:

CREATE table col(ts timeuuid) 

I am using Cassandra 1.2.4

Appreciate your help!

+6
source share
3 answers

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

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

+9
source

I use the same path for cassandra cli, and for the column name I use

 System.currentTimeMillis().toString 

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

UPDATE

it mostly depends on your row key, if rowKey is your userId or something like that, then there is no way to send a duplicate entry in milliseconds, but if you think this might be repeated, use

 val timestamp = com.eaio.uuid.UUIDGen.newTime().toString 
+4
source

If you use Datastax drivers, you can use the UUIDs utility class to create one

 import com.datastax.driver.core.utils.UUIDs; .... UUID timeBasedUuid = UUIDs.timeBased(); 
+1
source

Source: https://habr.com/ru/post/972820/


All Articles