【问题标题】:Graph/Gremlinpython: Upsert 2 vertices and an edge from one to the otherGraph / Gremlinpython:Upsert 2个顶点和从一个到另一个的边
【发布时间】:2020-06-15 00:08:31
【问题描述】:

我尝试添加(如果它们不存在)2 个顶点,并在单个查询中添加从第一个到另一个的边。 它看起来像:

g.V().has("label1", "property1", "value1").fold().coalesce(
        __.unfold(),
        g.addV("label1").property("property1", "value1")
    ).as_("a").V().has("label2", "property2", "value2").fold().coalesce(
        __.unfold(),
        g.addV("label2").property("property2", value2)
    ).coalesce(
        __.inE("link").where(__.outV().as_("a")),
        __.addE("link").from_("a")
    ).next()

但由于fold() 充当屏障,它会删除我放在第一个顶点上的标签“a”。 我的解决方法如下:

g.V().has("label1", "property1", "value1").fold().coalesce(
        __.unfold(),
        g.addV("label1").property("property1", "value1")
    ).as_("a").V().has("label2", "property2", "value2").fold().coalesce(
        __.unfold(),
        g.addV("label2").property("property2", value2)
    ).coalesce(
        __.inE("link").where(__.outV().has("label1", "property1", value1)),
        __.addE("link").from_(__.V().has("label1", "property1", value1))
    ).next()

我可以做得更好吗?这……丑吗?


好的,store解决方案有效:) 但是我对最后一个合并的第一部分有疑问。 我这样做了:

g.V().has("label1", "property1", "value1").fold().coalesce(
    __.unfold(),
    g.addV("label1").property("property1", "value1")
).as_("a").V().has("label2", "property2", "value2").fold().coalesce(
    __.unfold(),
    g.addV("label2").property("property2", value2)
).coalesce(
    __.inE("link").where(__.outV().where(within("a"))),
    __.addE("link").from_(select("a").unfold())
).next()

感谢 Kelvin Lawrence,它运行良好 :)

【问题讨论】:

    标签: graph gremlin tinkerpop amazon-neptune gremlinpython


    【解决方案1】:

    也许使用store 而不是as 可以满足您的需求:

    这是一个使用 Gremlin 控制台的虚构示例。

    gremlin> g.V().hasLabel('notyet').
    ......1>       fold().
    ......2>       coalesce(unfold(),addV('notyet')).
    ......3>       store('a').
    ......4>       V().
    ......5>       hasLabel('notyet2').
    ......6>       fold().
    ......7>       coalesce(unfold(),addV('notyet2')).
    ......8>       coalesce(__.in('link').where(within('a')),
                            addE('link').to(select('a').unfold())) 
    ==>e[61330][61329-link->61327]  
    

    【讨论】:

    • store 部分有效,但合并的第一部分无效。它会创建重复的边缘。你知道为什么吗?
    • 您能否更新您的问题以包含一些构建示例图的addVaddE 步骤?这将有助于为您提供最佳答案。在运行此查询之前,是否可能存在多个满足合并步骤标准的顶点?以下是创建测试图的简单方法示例:github.com/krlawrence/graph/blob/master/sample-code/…
    • 我能够找到适合我的环境的东西,谢谢:D
    猜你喜欢
    • 2019-08-09
    • 2021-05-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-12-23
    • 1970-01-01
    • 1970-01-01
    • 2011-06-30
    相关资源
    最近更新 更多