gremlin - 有人能解释一下 Gremlin 中的这个图遍历在做什么吗?
问题描述
我在理解这些 Gremlin 查询时遇到了一些麻烦:
from os import getenv
from gremlin_python.structure.graph import Graph
from gremlin_python.process.graph_traversal import __
from gremlin_python.driver.driver_remote_connection import DriverRemoteConnection
pmap = g.V().has(name, value) \
.union(__.hasLabel('UID'),
__.hasLabel('OID').outE('attached').inV()) \
.union(__.propertyMap(),
__.inE('attached').outV().hasLabel('OID') \
.propertyMap()).toList()
所以我理解g.V().has(name, value) is looking for a vertex with the key
name = value . What is the union doing here? Is it unioning vertices with a label "OID" with edges that go outward with a label "attached"?
What is the
inV()` 以及为什么联合的两个参数?
解决方案
该union()
步骤只是合并作为参数提供给它的子遍历流。举一个更简单的例子:
gremlin> g = TinkerFactory.createModern().traversal()
==>graphtraversalsource[tinkergraph[vertices:6 edges:6], standard]
gremlin> g.V().has('person','name','marko').union(has('age',29),bothE())
==>v[1]
==>e[9][1-created->3]
==>e[7][1-knows->2]
==>e[8][1-knows->4]
gremlin> g.V().has('person','name','marko').union(has('age',30),bothE())
==>e[9][1-created->3]
==>e[7][1-knows->2]
==>e[8][1-knows->4]
在第一个示例中,我们将“marko”顶点作为和union()
的起点。正如我们在输出中看到的值为“29”的“年龄”属性。我们还看到 in 的所有边都合并到该输出流中。在第二次遍历中,我们看到由于“年龄”不等于“30”而被过滤掉,所以我们得到的只是边缘。has('age',29)
bothE()
v[1]
v[1]
v[1]
v[1]
考虑到这种解释,请考虑您在问题中包含的遍历正在做什么。它找到一个带有“名称”的顶点和该键的一些值。这成为第一个的起点union()
。如果顶点具有“UID”标签,则它通过。如果顶点的标签为“OID”,那么它会遍历输出的“附加”边到相邻顶点并返回这些边。
奇怪的是 aVertex
只能有一个标签(至少按照 TinkerPop 的定义——一些图支持多个元素标签)。所以,假设一个标签,你真的只能得到一个或另一个流。就个人而言,我不认为使用union()
是一个好的选择。我认为使用合并会更直观,因为只能返回一个流,因此从上面扩展了我的示例:
gremlin> g.V().has('person','name','marko').coalesce(has('age',30),has('age',29).bothE())
==>e[9][1-created->3]
==>e[7][1-knows->2]
==>e[8][1-knows->4]
gremlin> g.V().has('person','name','marko').coalesce(has('age',29),has('age',29).bothE())
==>v[1]
在我看来,使用coalesce()
使意图更加清晰。继续使用原始代码到第二个union()
- 此时,您要么拥有原始Vertex
或一个或多个“附加”顶点,遍历组合了一个propertyMap()
和/或一个propertyMap()
具有“OID”的附加“附加”顶点“ 标签。
鉴于提供的信息,很难准确地说出这种遍历的意图。根据数据结构是什么以及意图是什么,我想事情可以简化。希望我至少已经解释了union()
正在做什么并为您澄清了这一点,因为这似乎是您问题的核心。
推荐阅读
- bash - 为什么此 YAD 列表代码无法正常工作?
- unix - Vim:在普通模式下切换键盘布局
- angular - Spring Gateway 如何路由深度 SPA 链接
- asp.net-core - 将 blazor 组件添加到现有 Asp.net 核心项目的问题
- r - 设置种子在我的 Windows 上不起作用,因为我使用 R 从不同的示例中复制
- wpf - 为什么在我的 XAML 文件中找不到该成员?
- python - 我将如何修复此功能?
- spring - Spring:PathMatcher 拆分变量
- java - 如何在单元测试中忽略 @PreAuthorize("isAuthenticated()") 注释?
- node.js - 身体解析器行为怪异