有人能解释一下Gremlin中的graph travesal在做什么吗?
我很难理解这些小精灵的疑问:有人能解释一下Gremlin中的graph travesal在做什么吗?,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, valu
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)正在用键name=value查找顶点。工会在这里干什么?它是否将顶点与标签“OID”合并,并将边与标签“附加”一起向外延伸?
什么是
inV()`以及为什么union的两个参数是?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]
在第一个示例中,我们得到union()
将一个“marko”顶点作为has('age',29)
和的起点。由于v[1]
还有一个值为“29”的“age”属性,我们可以在输出中看到v[1]
。我们还可以看到v[1]
的所有边都合并到该输出流中。在第二次遍历中,我们看到v[1]
被过滤掉,因为“年龄”不等于“30”,所以我们得到的只是边缘
考虑到这个解释,考虑你在你的问题中所包含的遍历是什么。它会找到一个带有“名称”和某个关键点值的顶点。这将成为第一个union()
的起点。如果顶点有一个“UID”标签,那么它将通过。如果顶点有一个“OID”标签,则它将向外的“附加”边遍历到相邻顶点并返回这些边
奇怪的是,顶点
只能有一个标签(至少根据TinkerPop的定义,有些图支持多个元素标签)。因此,假设有一个标签,实际上只能得到一个或另一个流。就我个人而言,我认为使用union()
不是一个好的选择。我认为使用coalesce更直观,因为只能返回一个流,因此从上面扩展了我的示例:
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()
-此时,您要么拥有原始顶点
,要么拥有一个或多个“附加”顶点,对于这些顶点,遍历组合了具有“OID”标签的附加“附加”顶点的propertyMap()
和/或propertyMap()
在提供的信息中,很难说这个遍历的目的是什么。根据数据结构是什么以及意图是什么,我认为事情可以简化。希望我至少已经解释了union()
在做什么,并澄清了这一点,因为这似乎是你问题的核心。在你的第一个例子中,union会返回4件事:一个顶点(1)和3条边(9,7,8),因为union将它们连接在一起?我明白你所说的“接受”marko顶点是什么意思了。等等,我认为第一个并集参数意味着如果带有名称和值的顶点也有label('UID'),那么返回它。但是它是否也会传递到下一个union参数:“hasLabel(“OID”).outE…?如果顶点只有一个标签“UID”,那么顶点会返回吗?第一个union()
产生的任何内容都会成为第二个union()的输入
。因此,为了回答您在注释中的具体问题,如果带有名称/值的顶点也有一个“UID”标签,那么它将成为下一个union()的输入
。我明白了,但是进入并集的顶点被传递给所有的并集参数,对吗?它是union()
的每个子遍历的起始对象。因此第二个union()
应该返回一个propertyMap()
,如果有传入的“attached”满足第二个子遍历的顶点的边,那么它也应该返回这些边。