首页 > 解决方案 > 使用 gremlin-python 以列表作为属性值过滤边缘

问题描述

我将列表存储为图表中某些边的属性值,类似于此处提出的问题。这个问题的解决方案是在 JavaScript 中给出的,但我正在寻找一种在 Python 中做同样事情的方法。

另请注意,Amazon Neptune 不支持 Lambda步骤,因此该解决方案不能使用 lambda。

标签: pythongremlintinkerpop3amazon-neptune

解决方案


我不确定这对您来说是否仍然是一个问题,但我想知道这是否对您不起作用:

gremlin> g = TinkerGraph.open().traversal()
==>graphtraversalsource[tinkergraph[vertices:0 edges:0], standard]
gremlin> g.addV('person').as('a').addE('self').to('a').property('x',[1,2,3]).select('a').addE('self').to('a').property('x',[10,20,30]).iterate()
gremlin> g.E().valueMap(true)
==>[x:[1,2,3],id:3,label:self]
==>[x:[10,20,30],id:4,label:self]
gremlin> g.V().outE('self').filter(local(values('x').unfold().is(2))).valueMap(true)
==>[x:[1,2,3],id:3,label:self]

基本上,您可以local()展开您的列表属性,然后对其进行过滤。Neptune 可能不会优化此过滤器(例如使用索引),但如果您不过滤大量边,它可能是一个足够的解决方法(如果它有效)。

请注意,我在 Groovy 中编写了上述内容以便轻松测试它。在 Python 中,您必须注意围绕命名冲突的某些小的语法修改。因此,最后一次遍历在python中会这样写(因为is是python中的保留字):

g.V().outE('self').filter(local(values('x').unfold().is_(2))).valueMap(true)

推荐阅读