Generating a Namespace / Name Based UUID in Go and Java
Recently I needed to generate a new id during migration of some data of a Golang application. Another application, written in Java, contained links to the first application’s data. So somehow I needed to update those links to point to that new id. I decided to use a “namespace name based UUID”, which is version 3 and 5 of the UUID spec. They generate a reproducable id based on given data. Since the UUID class of Java only supports v3, that was the way to go.
So first I generated the UUID in Go, that was straight forward using the
someOldData2 is a logical unique key of the data, which is available in both applications:
Next was to do the same in Java using the UUID class… but wow, that was more work than I expected:
- Java’s UUID class has no constants for the predefined namespaces (like
nameUUIDFromBytes(byte name)method does not even take 2 parameters (namespace + name), but only one byte array.
It was hard to find any documentation on how to actually use this method, but finally I found out:
Happy coding :)