问题
I'm using Java to access Gremlin. I have the following lines of code for creating a path between two vertices in an already created graph:
//begin simple path finding
Vertex fromNode = g.V().has("name", "i2").next();
Vertex toNode = g.V().has("name", "state1").next();
ArrayList list = new ArrayList();
g.V(fromNode).repeat(both().simplePath()).until(is(toNode)).limit(1).path().fill(list);
However, at this point I"m stuck. I can print out the list using list.toString() and I get the vertices that (I believe) are on the path. However, I now want to get those vertices themselves, access and potentially mutate their data. For instance, it isn't enough to know that v[32] is on the path if I can't access the ID 32 itself.
I should mention that I'm a bit new to this so please let me know if I'm using outdated/incorrect practices.
回答1:
You ended up with a list filled with Path
objects. I think what you're looking for is the unfold()
step (see the docs) so you can unroll/flatten out the elements in path to put into the list. Here's a Gremlin Console session showing it:
gremlin> graph = TinkerGraph.open()
==>tinkergraph[vertices:0 edges:0]
gremlin> g = graph.traversal()
==>graphtraversalsource[tinkergraph[vertices:0 edges:0], standard]
gremlin> g.addV("name", "i2").as("a").addV("name", "state1").addE("to").from("a").toList()
==>e[4][0-to->2]
gremlin> fromNode = g.V().has("name", "i2").next()
==>v[0]
gremlin> toNode = g.V().has("name", "state1").next()
==>v[2]
gremlin> l = new ArrayList()
gremlin> g.V(fromNode).repeat(both().simplePath()).until(is(toNode)).limit(1).path().fill(l)
==>[v[0],v[2]]
gremlin> l = []; g.V(fromNode).repeat(both().simplePath()).until(is(toNode)).limit(1).path().fill(l)
==>[v[0],v[2]]
gremlin> l = []; g.V(fromNode).repeat(both().simplePath()).until(is(toNode)).limit(1).path().unfold().fill(l)
==>v[0]
==>v[2]
gremlin> l[0].getClass()
==>class org.apache.tinkerpop.gremlin.tinkergraph.structure.TinkerVertex
gremlin> l[0].property("name", "j3") // mutate the vertex's name
==>vp[name->i3]
gremlin> g.V(fromNode).valueMap(true).toList()
==>[id:0,name:[j3],label:vertex]
回答2:
You should have access to these through list you're using on the fill
step. The toString
method won't print out the properties. If you just want to see those, you can call valueMap()
instead of fill
at the end of your traversal. If you'd like to iterate over all of the vertices and get the properties, you can use the properties
method. If you want to get a specific property, you can use the property(key)
method. The rest of what you'd need is in the API docs
来源:https://stackoverflow.com/questions/45373680/how-to-access-the-vertices-of-a-path-in-gremlin-through-java