在 Java 中使用 gremlin 遍历图形时如何收集 属性 值?
How can I collect property values while traversing a graph with gremlin in Java?
我图中的每个顶点都至少有一个名称 属性。我有一个名称值的标签 L 集 S。现在我想收集所有顶点的名称 属性 的值,这些顶点可以通过带有边标签 EL 的特定传出边(递归地)从集合 S.
中名称的顶点收集
我当前对名称为 S1 的单个起始节点的解决方案如下所示:
g.traversal().V().hasLabel(L)
.has("name", S1)
.repeat(__.optional(__.out(EL)))
.until(__.out(EL).count().is(0))
.path()
.forEachRemaining(path -> {
path.forEach(e -> System.out.println(((Vertex)e).property("name").value()));});
println
只是为了看到这样产生了预期的结果,通常我会把名字收集在一个集合中。
是否有更好的方法来收集通过带有标签 EL 的出边可达的所有顶点的名称 属性 的值?
从多个顶点开始的最佳方式是什么(其中只有名称来自集合 S)?
目前结构是一棵树,但是如果有by循环,上面的代码能防止死循环吗?如果不行,怎么办?
您的方法是一个好的开始。
要从一组多个顶点开始,请使用 P.within()
predicate. TinkerPop provides several other predicates。
使用 simplePath()
防止循环重复。
使用store()
to keep track of items as it traverses the graph. The by("name")
调制器将存储"name" 属性而不是顶点。
要得到结果,使用cap()
to output the items it stored during the traversal. The result at this point is a Set
which potentially contains duplicates. Use unfold()
to turn the Set
into an iterator that we can dedup()
then finish with toSet()
。
graph.traversal().V().hasLabel(L).has("name", P.within(S)).
repeat( __.out(EL).simplePath().store("x").by("name") ).
until( __.outE(EL).count().is(0) ).
cap("x").unfold().dedup().toSet()
我图中的每个顶点都至少有一个名称 属性。我有一个名称值的标签 L 集 S。现在我想收集所有顶点的名称 属性 的值,这些顶点可以通过带有边标签 EL 的特定传出边(递归地)从集合 S.
中名称的顶点收集我当前对名称为 S1 的单个起始节点的解决方案如下所示:
g.traversal().V().hasLabel(L)
.has("name", S1)
.repeat(__.optional(__.out(EL)))
.until(__.out(EL).count().is(0))
.path()
.forEachRemaining(path -> {
path.forEach(e -> System.out.println(((Vertex)e).property("name").value()));});
println
只是为了看到这样产生了预期的结果,通常我会把名字收集在一个集合中。
是否有更好的方法来收集通过带有标签 EL 的出边可达的所有顶点的名称 属性 的值?
从多个顶点开始的最佳方式是什么(其中只有名称来自集合 S)?
目前结构是一棵树,但是如果有by循环,上面的代码能防止死循环吗?如果不行,怎么办?
您的方法是一个好的开始。
要从一组多个顶点开始,请使用 P.within()
predicate. TinkerPop provides several other predicates。
使用 simplePath()
防止循环重复。
使用store()
to keep track of items as it traverses the graph. The by("name")
调制器将存储"name" 属性而不是顶点。
要得到结果,使用cap()
to output the items it stored during the traversal. The result at this point is a Set
which potentially contains duplicates. Use unfold()
to turn the Set
into an iterator that we can dedup()
then finish with toSet()
。
graph.traversal().V().hasLabel(L).has("name", P.within(S)).
repeat( __.out(EL).simplePath().store("x").by("name") ).
until( __.outE(EL).count().is(0) ).
cap("x").unfold().dedup().toSet()