简体   繁体   English

生成唯一ID

[英]Generate unique ID

I need to generate unique ID's for my application. 我需要为我的应用程序生成唯一的ID。 When I used (UUID.randomUUID()).toString() , I am getting a code (thinking this will be unique), which is very lengthy. 当我使用(UUID.randomUUID()).toString() ,我得到一个代码(认为这将是唯一的),这是非常冗长的。

I am not sure how unique it will be, when we generate codes with the help of Java Timestamp or randomstring. 当我们在Java Timestamp或randomstring的帮助下生成代码时,我不确定它有多独特。

I need to generate unique codes which is only of 8-10 characters in length (alpha-numeric). 我需要生成长度仅为8-10个字符的唯一代码(字母数字)。 How to get so? 怎么这样? I am using MySQL database. 我正在使用MySQL数据库。

Is generating unique code on database side is the best way or can we generate such short (but unique) codes in Java? 在数据库端生成唯一代码是最好的方法还是我们可以在Java中生成这样的短(但唯一)代码?

Any suggestions with example code will be very helpful. 任何带有示例代码的建议都会非常有用。

I use RandomStringUtils.randomAlphanumeric() method from commons-lang to achieve this: 我使用commons-lang RandomStringUtils.randomAlphanumeric()方法来实现这个目的:

import org.apache.commons.lang.RandomStringUtils;

public static final int ID_LENGTH = 10;

public String generateUniqueId() {
    return RandomStringUtils.randomAlphanumeric(ID_LENGTH);
}

If you using Maven, ensure that you have added commons-lang to project's dependencies: 如果您使用Maven,请确保已将commons-lang添加到项目的依赖项:

<dependency>
    <groupId>commons-lang</groupId>
    <artifactId>commons-lang</artifactId>
    <version>2.6</version>
</dependency>

Is generating unique code on database side is the best way or can we generate such short (but unique) codes in java? 在数据库端生成唯一代码是最好的方法还是我们可以在java中生成这样的短(但唯一)代码?

It's up to you and your project. 这取决于您和您的项目。 Is id-generation part of business logic? id生成是业务逻辑的一部分吗? If yes and all logic written on Java, so write it on Java. 如果是,所有逻辑都写在Java上,那么在Java上编写它。 If all or some part of logic delegated to database, so generate id there (but in this case you will have strong dependency to particular database). 如果将逻辑的全部或部分委托给数据库,那么在那里生成id(但在这种情况下,您将对特定数据库具有强依赖性)。

Do you have any specific limitation you need to take into account? 您是否有任何特定的限制需要考虑? Such as cross-application uniqueness? 如跨应用的独特性? Because otherwise, MySQL is quite capable of generating IDs by itself, all you need to do is define an autoincrement column and not specify it at insert time (meaning, inserting a NULL value for it) - that will make MySQL fill it with the next available ID, unique and requiring no work from you. 因为否则,MySQL完全能够自己生成ID,所有你需要做的就是定义一个autoincrement列而不是在插入时指定它(意味着,为它插入一个NULL值) - 这将使MySQL填充下一个可用的ID,独一无二,无需您的工作。

It won't be an alphanumerical string (which I'm not sure if you specified as a requirement or restriction), but if all you require is uniqueness, it's more than enough. 它不是一个字母数字字符串(我不确定你是否指定为要求或限制),但如果你需要的只是唯一性,那就足够了。 8 - 10 alphanumeric characters aren't enough to guarantee uniqueness in a randomly-generated string, so you'd have to perform an insert check on the database. 8-10个字母数字字符不足以保证随机生成的字符串的唯一性,因此您必须对数据库执行插入检查。

Is generating unique code on database side is the best way or can we generate such short (but unique) codes in Java? 在数据库端生成唯一代码是最好的方法还是我们可以在Java中生成这样的短(但唯一)代码?

Databases are designed to be able to generate unique IDs where needed. 数据库旨在能够在需要时生成唯一ID。 I doubt anything you (or I) could code would be a 'better' variant of that. 我怀疑你(或我)可以编码什么是“更好”的变体。

I have written a simple service which can generate semi-unique non-sequential 64 bit long numbers. 我编写了一个简单的服务,可以生成半唯一的非连续64位长数字。 It can be deployed on multiple machines for redundancy and scalability. 它可以部署在多台计算机上,以实现冗余和可扩展性。 It uses ZeroMQ for messaging. 它使用ZeroMQ进行消息传递。 For more information on how it works look at github page: zUID 有关其工作原理的更多信息,请参阅 github页面: zUID

Take look at: UIDGenerator.java 看看: UIDGenerator.java

You can customize it (unique to process only, or world), it is easy to use and fast: 您可以自定义它(仅限处理或世界),它易于使用且快速:

    private static final UIDGenerator SCA_GEN = new UIDGenerator(new ScalableSequence(0, 100));
.......
    SCA_GEN.next();

You can change the implementation to reduce the size of the ID (and add other tradeoffs) 您可以更改实现以减小ID的大小(并添加其他权衡)

see my benchmarking results at: 看我的基准测试结果:

http://zoltran.com/roller/zoltran/entry/generating_a_unique_id http://zoltran.com/roller/zoltran/entry/generating_a_unique_id

or run them yourself. 或者自己运行它们。

The question if id generation part be done in database or java end: This question has to be answered by you depending on requirements of your application: 如果id生成部分在数据库或java端完成的问题:这个问题必须由您根据应用程序的要求来回答:

1) One way is to go by System.currenTimeMillis() . 1)一种方法是通过System.currenTimeMillis()。 But if your applicaation will work in multi clustered env, then you may end up with duplicate values. 但是,如果您的应用程序可以在多集群环境中运行,那么您可能会得到重复的值。

http://www2.sys-con.com/itsg/virtualcd/java/archives/0512/Westra/index.html http://www2.sys-con.com/itsg/virtualcd/java/archives/0512/Westra/index.html

2) Another way is to use UUID Generator .It will help you in case you have different databases that need to be merged. 2)另一种方法是使用UUID Generator。如果您有不同的数据库需要合并,它将帮助您。 Using this mehtod you don't have to worry about duplication of id when merging databases. 使用这个mehtod,您不必担心合并数据库时重复id。

https://marketplace.informatica.com/solutions/mapping_uuid_using_java https://marketplace.informatica.com/solutions/mapping_uuid_using_java

There may be other factors you may want to consider. 您可能还需要考虑其他因素。 As per your question UUID method will go. 根据你的问题UUID方法将会去。

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

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