Gremlin Python:如何使用合并获取顶点(如果存在),否则插入

时间:2019-12-04 17:33:08

标签: gremlin amazon-neptune gremlinpython

如果存在这样的顶点,是否可以使用Gremlin coalesce步骤通过id(或属性)选择一个顶点,否则插入该顶点?我尝试使用以下方法来执行此操作,但遇到一个'list' object has no attribute 'coalesce'错误,我可以看到是由.fold().next()返回一个python列表对象引起的:

    my_vertex = g.V(1).fold().next().coalesce(__.unfold(), g.addV(
         'my_label'
        ).property(
            T.id,
            1
        ).property(
            'test', 'foo'
        ).property(
            'name', 'my_vertex_name'
        ).property(
            'created_timestamp', 1575480467
        ).next()
    )

以这种方式执行对性能有好处吗?还是应该在初始顶点查询的hasNext()上简单地将其分解为if / else?

2 个答案:

答案 0 :(得分:2)

您需要从查询中删除next。接下来是终端步骤。如果没有next,查询应该按照您期望的方式工作,如果V(1)存在,它将不会创建它,否则它将创建它。除此之外,这是进行upsert查询的好方法。

如果您使用的是海王星,请记住ID是字符串,所以它是V('1')。

答案 1 :(得分:-1)

我能够使用以下命令完成“获取,如果存在,则插入”:

my_vertex = g.V(1).fold().next().coalesce(__.unfold(), g.addV(
         'my_label'
        ).property(
            T.id,
            1
        ).property(
            'test', 'foo'
        ).property(
            'name', 'my_vertex_name'
        ).property(
            'created_timestamp', 1575480467
        )
    ).next() //notice that .next was simply moved to the close of the .coalesce step