如果不存在则使用 gremlin 在 Python 中添加边

Add edge if not exist using gremlin In Python

现在我正在尝试使用 python 为 aws 的 neptunedb 实现一些 gremlin 逻辑。我想检查一条边是否存在,如果存在则忽略,否则添加边。

对于 gremlin 控制台,我们可以这样做:

g.V().has('people','name', 'somebody').as('v').V().has('software','name','ripple').coalesce(__.inE('Created').where(outV().as('v')), addE('created').from('v').property('weight',0.5))

但我有点不知道如何将其转换为 python。好像python不能识别为('v')? 有什么提示吗?或者我在哪里可以找到 gremlin python.

的参考文档

来自TinkerPop Gremlin documentation

The term as is a reserved word in Python, and therefore must be referred to in Gremlin with as_().

from也是如此。只需将 as 替换为 as_ 并将 from 替换为 from_ 即可。

只是为了更清楚地说明这一点,对于 python 您需要执行以下操作:

g.V().has('people','name', 'somebody').\
    as_('v').V().has('software','name','ripple').coalesce(
    __.inE('Created').where(__.outV().as_('v')), 
    __.addE('created').from_('v').\
    property('weight',0.5)).iterate()

最后的 iterate() 很重要,因为与 gremlin 控制台相比,在 python 中工作时需要提供终端步骤。您还可以使用其他终端步骤,例如 next(),但如果没有任何终端步骤,将不会创建边缘(至少在 Neptune 中)