Monday, March 28, 2011

UUID key generation

Many times when you're inserting data into a NoSql style data store, you need to provide your own keys. There are several ways this can be done in Java.

Java 5 has java.util.UUID
  • seems to be popular and is what I’m using. You need to provide the hardware ethernet address on your own for type 1 UUIDs.
  • seems like an interesting alternative as well. It uses some OS specific utilities to obtain the hardware ethernet address for use in the UUID.
UUID generation is pretty straight forward. These examples are for  JUG.

private UUIDGenerator uuidGen = UUIDGenerator.getInstance();
// Generate a type 1 UUID without a network interface reference.
String myUuid1 = uuidGen.generateTimeBasedUUID().toString();

That’s all that is needed to get your UUID to use as a key in your data store.
Next post we’ll look at using JDK 6 to get the network address to use along with the type 1 UUID.