5

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?

Bob van Luijt
  • 7,153
  • 12
  • 58
  • 101

1 Answers1

5

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]
stephen mallette
  • 45,298
  • 5
  • 67
  • 135
  • Hello @Stephen, I'm using Gremlin Console-3.3.3, I want to set vertex identifier myself, and I tried `gremlin> g.addV(id, UUID.randomUUID())` but failed, Below is the error message: `No signature of method: org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.GraphTraversalSource.addV() is applicable for argument types: (org.apache.tinkerpop.gremlin.structure.T$2, java.util.UUID) ...` It seems that we could not set vertex identifier now, isn't it? – Shuping Oct 11 '18 at 10:38
  • 3
    That syntax is no longer valid. `g.addV()` takes a vertex label as its argument. to add `id` you need to do `g.addV().property(id, UUID.randomUUID())` (and optionally specify the vertex label to `addV()`) – stephen mallette Oct 11 '18 at 10:50
  • @stephenmallette - it does work if you use `g.addV(T.id, UUID.randomUUID())`. Slightly confusing, but this is not the addVertex step but the addVertex method of [Graph](http://tinkerpop.apache.org/javadocs/3.2.3/core/org/apache/tinkerpop/gremlin/structure/Graph.html#addVertex-java.lang.Object...-) – Valentin Jul 23 '19 at 17:44
  • if "g" is a `Graph` then, yes, it will work, however, users should not be interacting with `Graph` instances. Users should only interact with the `Graph` by way of Gremlin (i.e. where "g" is a `GraphTraversalSource`) – stephen mallette Jul 23 '19 at 18:24