wrongusername
wrongusername

Reputation: 18918

Best way to store UUIDs in Neo4j?

If I try the simple

thingNode.setProperty("uuid", thing.getId());

I get

java.util.UUID] is not a supported property value

Yet it would be incredibly wasteful to store a 128-bit UUID as a 36-character string. If I store the UUID split apart into separate properties

thingNode.setProperty("uuid-begin", thing.getId().getMostSignificantBits());
thingNode.setProperty("uuid-end", thing.getId().getLeastSignificantBits());

It appears I can only create an index on a single property, and would have to concatenate the two bits of the UUID somehow into one property. As mentioned above, strings are undesirable due to highly inefficient storage space. Any ideas?

Upvotes: 1

Views: 796

Answers (1)

Stefan Armbruster
Stefan Armbruster

Reputation: 39925

I've used the following snippet for this in the past. Since a UUID is "one thing" it's a good idea to store it into one property. As you've already mentioned a index is always build on one property.

final StringBuilder sb = new StringBuilder();
sb.append(Long.toHexString(uuid.getMostSignificantBits()))
  .append(Long.toHexString(uuid.getLeastSignificantBits()));
String uuidAsString = sb.toString();
node.setProperty("uuid", uuidAsString);

Please note that there is a ready-to-go solution for managing uuids in Neo4j: https://github.com/graphaware/neo4j-uuid

Upvotes: 5

Related Questions