如何生成基于时间的UUID? [英] How to generate time based UUIDs?

查看:1040
本文介绍了如何生成基于时间的UUID?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在java中生成基于时间的UUID - java.util.UUID.randomUUID()生成UUID版本4.如何生成版本1(基于时间)的UUID?是否有一个单独的库,或者它是Java 7 API中提供的一些,我错过了它。

I want to generate time based UUID in java - java.util.UUID.randomUUID() genrates a UUID version 4. How to generate a version 1 (time based) UUID ? Is there a separate library for that or is it some how provided in the Java 7 API and I am missing it.

推荐答案

Be非常小心使用com.eaio.uuid.UUID,如果您需要按当前时间以外的时间生成GUID,因为这将始终在给定时间内生成相同的UUID。因此,如果您的数据需要生成UUID,其中记录可能具有相同的时间,这将生成相同的UUID。

Be very careful using com.eaio.uuid.UUID, if you need to generate GUID by time other than current time, as this will always generate the same UUID for given time. So if your data has requirement to generate UUID, where records may have same time, this will generate same UUID.

我们需要这个为前面创建的记录生成UUID,同时还有多个记录,我们试图通过尝试使用UUIDGen.createTime()来调整它。我们发现它有副作用来改变记录乱序的时间。如果创建 UUID 的后续请求的时间戳早于先前的请求,则最终会得到 UUID ,其中基于先前请求的时间戳。

We needed this to generate UUID for records, which are created ahead and also multiple records at same time, we tried to tweak it by trying to use UUIDGen.createTime(). We found that it has a side effect to mutate the time for records out of order. If a subsequent request to create UUID has a time stamp older than previous request, you end up having a UUID, which is based on previous request's time stamp.

在完成所有分析后,我们只需返回创建 UUID 在创建时,我们不必将其作为参数传递。无论我们在以后生成 UUID 的时候,每个人都有一个缺点。

After all the analysis, we had to simply go back to create UUID at the time of creation, so that we don't have to pass it as an argument. Whichever solution we come up with to generate UUID at a later time, every one has a drawback.

这篇关于如何生成基于时间的UUID?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆