Generate unique ID
Asked Answered
N

6

17

I need to generate unique ID's for my application. When I used (UUID.randomUUID()).toString(), I am getting a code (thinking this will be unique), which is very lengthy.

I am not sure how unique it will be, when we generate codes with the help of Java Timestamp or randomstring.

I need to generate unique codes which is only of 8-10 characters in length (alpha-numeric). How to get so? I am using MySQL database.

Is generating unique code on database side is the best way or can we generate such short (but unique) codes in Java?

Any suggestions with example code will be very helpful.

Noctambulous answered 30/1, 2012 at 7:1 Comment(5)
do you want to create your own method for generating unique id using timestamp?Hexamethylenetetramine
You can get one unique number and then can increment it by 1 every time while storing records into the DB.Alcoholize
@Sathish : Why not get the substring() according to your desired length and keep checking if the new one is equal to any previously assigned. UUID is the best way :-) RegardsWag
@Hemant Metalia: I never mind to create a new method to generate codes by timestamp, but my question is, whether it will be unique?Noctambulous
@Gagandeep Bali: I feel substring() and checking that with the previous values (will have many id's) will take a long time.. will lead a performance issue !!!Noctambulous
R
21

I use RandomStringUtils.randomAlphanumeric() method from commons-lang to achieve this:

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:

<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?

It's up to you and your project. Is id-generation part of business logic? If yes and all logic written on Java, so write it on 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).

Respondence answered 30/1, 2012 at 7:24 Comment(3)
Hi.. Thanks a lot.. By using randomAlphanumeric(), we are getting codes as per the length we wish.. but unique??Noctambulous
randomAlphanumeric() generates string with random characters and in most cases its unique (you also may try to run it in a loop and generate new unless its doesn't exists).Respondence
Thanks.. finally I have used RandomStringUtils.randomAlphanumeric(10) to generate the code and check that code existence in database. If not exists, I am using it as my unique code..Noctambulous
B
7

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.

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.

Blankbook answered 30/1, 2012 at 8:12 Comment(0)
F
5

Is generating unique code on database side is the best way or can we generate such short (but unique) codes in Java?

Databases are designed to be able to generate unique IDs where needed. I doubt anything you (or I) could code would be a 'better' variant of that.

Frisch answered 30/1, 2012 at 7:27 Comment(2)
Thank you. If so, Can you give me some examples on how to do generate unique IDs for MySQL databases.Noctambulous
Try any of these. I don't use DBs on a day to day basis, and have never used MySQL.Frisch
H
1

I have written a simple service which can generate semi-unique non-sequential 64 bit long numbers. It can be deployed on multiple machines for redundancy and scalability. It uses ZeroMQ for messaging. For more information on how it works look at github page: zUID

Hypertrophy answered 12/6, 2014 at 16:51 Comment(0)
O
0

Take look at: 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)

see my benchmarking results at:

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

or run them yourself.

Ogata answered 26/10, 2014 at 18:10 Comment(1)
In case I use UID Generator will it be unique in multi clustered java application , Will that id be unique even if server is restarted??Dolora
D
0

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:

1) One way is to go by 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

2) Another way is to use UUID Generator .It will help you in case you have different databases that need to be merged. Using this mehtod you don't have to worry about duplication of id when merging databases.

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.

Dolora answered 18/10, 2016 at 10:58 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.