如何在 Java/Scala 中生成 TimeUUID

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24952066/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-14 15:56:37  来源:igfitidea点击:

How to generate TimeUUID in Java/Scala

javascalacassandratimeuuid

提问by Avis

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

有谁知道如何在 Java/Scala 中生成基于时间的 UUID?

Here is the column family:

这是列族:

CREATE table col(ts timeuuid)

I'm using Cassandra 1.2.4

我正在使用 Cassandra 1.2.4

Appreciate your help!

感谢你的帮助!

采纳答案by mikea

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

Cassandra 有用于生成 Timeuuid 的 UUIDGen。这个的来源在这里:

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

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

回答by Govind Singh

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

我对 cassandra cli 使用相同的方式,对于我正在使用的列名

System.currentTimeMillis().toString


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

UPDATE

更新

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

主要取决于您的行键,如果 rowKey 是您的userId或其他东西,则没有机会在毫秒内提交重复记录但如果您认为它可以重复然后使用

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

回答by Eduardo Dennis

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

如果您使用的是Datastax驱动程序可以使用的工具类,的UUID,以产生一个

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