Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use a UUID as id in Gremlin?

I'm adding verticles like this:

g.addV("foobar").property("id", 1).property(...etc...

How can I set a property with a uuid instead of an integer id?

like image 397
Bob van Luijt Avatar asked Dec 25 '17 13:12

Bob van Luijt


1 Answers

An "id" can have multiple meanings. If you simply mean that you want to use a UUID as a unique identifier to lookup your vertices then taking the approach you have is fine, when used in conjunction with the underlying indexing functionality of your chosen graph database. In other words, as long as you have an index on "id" then you will quickly find your vertex. In this sort of usage, "id" is really just a property of the vertex obviously and you may find that for certain graph databases that "id" is actually a reserved term and can't be used as a property key. It is likely best to choose a different key name.

If instead of using "id" as a property key, you mean that you wish to set the actual vertex identifier, referred to by T.id, as in:

g.addV(T.id, uuid)

then you first need to use a graph database implementation that allows the assignment of identifiers. TinkerGraph is one such implementation. In this way, you natively assign the identifier of the vertex rather than allowing the graph database to create it for you.

gremlin> graph = TinkerGraph.open()
==>tinkergraph[vertices:0 edges:0]
gremlin> g = graph.traversal()
==>graphtraversalsource[tinkergraph[vertices:0 edges:0], standard]
gremlin> g.addV(id, UUID.randomUUID())
==>v[c2d673de-2425-4b42-bc1e-68ff20e3b0a8]
gremlin> g.V(UUID.fromString("c2d673de-2425-4b42-bc1e-68ff20e3b0a8"))
==>v[c2d673de-2425-4b42-bc1e-68ff20e3b0a8]
like image 161
stephen mallette Avatar answered Oct 03 '22 08:10

stephen mallette