简体   繁体   English

如何在Java中生成一个固定长度的唯一标识?

[英]How to generate a unique identifier of a fixed length in Java?

I am trying to generate a unique identifier of a fixed length such as the IDs that are generated by Megaupload for the uploaded files.我正在尝试生成固定长度的唯一标识符,例如 Megaupload 为上传的文件生成的 ID。

For example:例如:

  • ALGYTAB5 ALGYTAB5
  • BCLD23A6 BCLD23A6

In this example using from AZ and 0-9 and with a fixed length of 8 the total different combinations are 2,821,109,907,456.在此示例中,使用从 AZ 和 0-9 以及固定长度为 8 的总不同组合为 2,821,109,907,456。

What if one of the generated id is already taken.如果生成的 id 之一已被占用怎么办。 Those ids are going to be stored in a database and it shouldn't be used more than once.这些 id 将存储在数据库中,并且不应多次使用。

How can I achieve that in Java?如何在 Java 中实现这一点?

Thank you.谢谢你。

Hmm... You could imitate a smaller GUID the following way.嗯...您可以通过以下方式模仿较小的GUID Let first 4 bytes of your string be the encoded current time - seconds passed after Unix.让字符串的前 4 个字节为编码的当前时间 - Unix 之后经过的秒数。 And the last 4 just a random combination.最后4个只是随机组合。 In this case the only way two ID's would coincide is that they were built at the same second.在这种情况下,两个 ID 一致的唯一方法是它们是同时构建的。 And the chances of that would be very veeery low because of the other 4 random characters.由于其他 4 个随机字符,这种可能性非常低。

Pseudocode:伪代码:

get current time (4 byte integer
id[0] = 1st byte of current time (encoded to be a digit or a letter)
id[1] = 2nd
id[2] = 3rd
id[3] = 4th
id[4] = random character
id[5] = random character
id[6] = random character
id[7] = random character

I have tried @Armen's solution however I would like to give another solution我已经尝试过@Armen 的解决方案,但是我想提供另一个解决方案

    UUID idOne = UUID.randomUUID();
UUID idTwo = UUID.randomUUID();
UUID idThree = UUID.randomUUID();
UUID idFour = UUID.randomUUID();

String time = idOne.toString().replace("-", "");
String time2 = idTwo.toString().replace("-", "");
String time3 = idThree.toString().replace("-", "");
String time4 = idFour.toString().replace("-", "");

StringBuffer data = new StringBuffer();
data.append(time);
data.append(time2);
data.append(time3);
data.append(time4);

    SecureRandom random = new SecureRandom();
int beginIndex = random.nextInt(100);       //Begin index + length of your string < data length
int endIndex = beginIndex + 10;            //Length of string which you want

String yourID = data.substring(beginIndex, endIndex);

Hope this help!希望这有帮助!

Follow these below steps:请按照以下步骤操作:

  1. generateId()生成标识()
  2. Check if it is already issued by querying database.通过查询数据库检查是否已经发出。
  3. If yes, call generateId() again until you do not find it in db如果是,请再次调用 generateId() 直到在 db 中找不到它

We're using the database to check whether they already exist.我们正在使用数据库来检查它们是否已经存在。 If the number of IDs is low compared to the possible number you should be relatively safe.如果 ID 的数量与可能的数量相比较低,那么您应该相对安全。

You might also have a look at the UUID class (although it's 16-byte UUIDs).您还可以查看UUID class(尽管它是 16 字节 UUID)。

Sounds like a job for a hash function .听起来像是hash function的工作。 You're not 100% guaranteed that a hash function will return a unique identifier, but it works most of the time.您不能 100% 保证 hash function 将返回唯一标识符,但它在大多数情况下都有效。 Hash collisions must be dealt with separately, but there are many standard techniques for you to look into. Hash 碰撞必须单独处理,但有许多标准技术可供您研究。

Specifically how you deal with collisions depends on what you're using this unique identifier for.具体而言,您如何处理冲突取决于您使用此唯一标识符的目的。 If it's a simple one-way identifier where you give your program the ID and it returns the data, then you can simply use the next available ID in the case of a collision.如果它是一个简单的单向标识符,您为程序提供 ID 并返回数据,那么您可以在发生冲突时简单地使用下一个可用 ID。

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

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