简体   繁体   English

Java通用唯一标识符

[英]Java Universally unique identifier

In java, we have UUID (Universally unique identifier) for generating unique IDs. 在Java中,我们具有用于生成唯一ID的UUID(通用唯一标识符)。 I am using this to get unique identifiers. 我正在使用它来获取唯一的标识符。

So, I am getting an unique identifier as: 35ffa200-aaa5-40ec-ab6f-20bb36632f86 (5 groups separated by hyphens). 因此,我得到的唯一标识符为:35ffa200-aaa5-40ec-ab6f-20bb36632f86(由连字符分隔的5组)。

I dont need such lengthy identifiers. 我不需要这么长的标识符。 My identifier must be of short value, say the code must be of only 3 groups. 我的标识符必须是短值,例如代码只能是3组。 How can I get such small unique identifiers. 如何获得如此小的唯一标识符。 Is there any limits in UUID class? UUID类是否有限制? I am afraid to use string limits for UUID. 恐怕对UUID使用字符串限制。 Is it a correct way 这是正确的方法吗

or is there any way to generate such small unique identifiers? 还是有什么方法可以生成如此小的唯一标识符?

Thank you. 谢谢。

If you are using a unique ID for private purposes, perhaps something as simple as the current time in milliseconds would be good enough? 如果您将唯一ID用于私人目的,那么像当前时间(以毫秒为单位)这样简单的内容就足够了吗? That's just 8 bytes. 只有8个字节。 Or, you can combine some kind of counter in your process (is your application distributed?) with the current time if millisecond granularity will not do it for you. 或者,如果毫秒级的粒度对您而言不适合,则可以将过程中的某种计数器(应用程序是否已分发?)与当前时间结合使用。 You can have your ID be the time of the start of your process suffixed the counter for example. 例如,您可以在进程开始时给ID加上计数器的后缀。

The UUID algorithm was intended to be globally unique across all time by including a network address in conjunction with a time stamp. 通过将网络地址与时间戳结合使用,可以使UUID算法在所有时间内都具有全局唯一性。

UUID stands for Universally Unique IDentifier. UUID代表通用唯一IDentifier。 Most of the bits of a typical UUID are dedicated to the problem of making the identifiers universally unique, both in time and in the "space" of the internet. 典型的UUID的大多数位都专用于使标识符在时间上和Internet“空间”上通用唯一的问题。 In fact there are a number of different UUID formats that address this problem in different ways, but they all depend on having a lot of bits (~128) for the UU characteristics. 实际上,有许多不同的UUID格式以不同的方式解决了这个问题,但是它们都依赖于UU特性具有很多位(〜128)。 The Wikipedia page on UUIDs gives more details and has links to relevant standards and specifications. UUID上的Wikipedia页面提供了更多详细信息,并具有指向相关标准和规范的链接。

You seem to be asking if you can use a substring of a UUID. 您似乎在询问是否可以使用UUID的子字符串。 The answer is that if you do this you will probably end up with an identifier that doesn't have strong enough guarantees of uniqueness. 答案是,如果执行此操作,则可能会得到一个标识符,该标识符没有足够强的唯一性保证。

If you want to generate identifiers for private purposes, they probably don't need to be universally unique. 如果您想为私人目的生成标识符,则它们可能不必是唯一的。 Your identifiers' uniqueness requirements (and other characteristics) will depend on the use to which you intend to put them, and without you telling us what they are, it is hard to suggest an appropriate solution. 标识符的唯一性要求(和其他特征)将取决于您打算将其使用的用途,并且如果您不告诉我们它们是什么,那么很难提出适当的解决方案。 But here are some examples: 但是这里有一些例子:

  • For simple cases, a timestamp or some other monotonically increasing integer sequence will be sufficient. 对于简单的情况,时间戳或其他单调递增的整数序列就足够了。
  • If you want weak or stronger non-guessability you could use some kind of random or pseudo-random number generator. 如果您想要弱或强的不可猜测性,则可以使用某种随机或伪随机数生成器。
  • If you need to generate these numbers at multiple places, either incorporate the location into the id, or split the id-space into subspaces in some way. 如果需要在多个位置生成这些数字,请将该位置合并到id中,或以某种方式将id空间拆分为子空间。

It is also necessary to do your sums on the likely rate of identifier generation / use, and consider what happens when your id generator runs out. 还需要对标识符生成/使用的可能速率进行求和,并考虑当id生成器用尽时会发生什么。 (Is wrap-around acceptable? Can you reclaim and reissue ids that are no longer used? Does your chosen id representation allow you to expand / extend the id space?) (环绕式可接受吗?您可以收回并重新发布不再使用的ID吗?您选择的ID表示形式是否允许您扩展/扩展ID空间?)

如果您的应用程序正在连接到数据库,则可以使用数据库表的主键或序列来生成唯一ID。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM