我想沿着给定的边关系删除一组顶点及其所有后代。就我而言,图有一些顶点类型为会话,而另一些顶点类型为事件。从事件到会话都有弧线,其标签为in_session。
似乎没有快速的方法可以一次性完成此操作。这是我使用的代码:
// First, select all event descendants of sessions with r < 700
GraphTraversal<Vertex, Vertex> t = g.V()
.has("type", "session")
.has("r", P.lt(700))
.in("in_session");
// And remove those events
while (t.hasNext())
t.next().remove();
// Then, using the first part of the query again,
// select the same sessions as before
t = g.V().has("type", "session").has("r", P.lt(700));
// And delete them as well
while (t.hasNext())
t.next().remove();
对我来说似乎很笨拙。此外,如果我想删除较低级别的后代,则必须编写更多重复的步骤(一直向下到底部,然后删除,然后备份一个级别,然后删除,然后再次备份一个级别,依此类推。 ..)。我也注意到TitanGraph中没有removeVertex方法。
最佳答案
一种方法是使用sideEffect()
。假设我要从TinkerPop toy“现代”图中的“ marko”顶点中删除所有“ knows”顶点:
gremlin> graph = TinkerFactory.createModern()
==>tinkergraph[vertices:6 edges:6]
gremlin> g = graph.traversal()
==>graphtraversalsource[tinkergraph[vertices:6 edges:6], standard]
gremlin> g.V().has('name','marko').outE()
==>e[9][1-created->3]
==>e[7][1-knows->2]
==>e[8][1-knows->4]
gremlin> g.V().has('name','marko').sideEffect(out('knows').drop()).drop()
gremlin> g.V().has('name','marko')
gremlin> g.V(2)
gremlin> g.V(4)
gremlin> g.V(3)
==>v[3]
请注意,使用
drop()
而不是遍历遍历以在每个元素上调用remove()
。关于java - 在泰坦图中删除后代,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/39142336/