【问题标题】:Best way to add two vertices and an edge atomically in Gremlin在 Gremlin 中以原子方式添加两个顶点和一条边的最佳方法
【发布时间】:2020-12-12 01:50:40
【问题描述】:

我将 Gremlin 与 AWS Neptune 一起使用,并尝试以原子方式添加 2 个顶点和 1 个边。我基本上使用了fold() + coalesce() 语义来遍历,这样我就可以继续创建尚未存在的元素。这是我想出的,想知道人们是否有更好的选择。

         g.V("America")
                .fold()
                .coalesce(unfold(), addV("Country")
                                        .property(T.id, "America")
                                        .property("Population", 300_000_000))
          .V("India")
                .fold()
                .coalesce(unfold(), addV("Country")
                                        .property(T.id, "India")
                                        .property("Population", 1_000_000_000))
           .V("America")
                .outE("connected")
                .inV()
                    .has(T.id, "India")
                .fold()
                .coalesce(unfold(), addE("connected")
                                        .property("distanceMiles", 8000)
                                        .from(V("America"))
                                        .to(V("India"))).next();

另外,是否有任何提示可以使查询更具可读性?我正在使用 JAVA,并且由于需要链接这些步骤,因此无法将部分查询提取到它自己的方法中,以便它可以重用。这种理解正确吗?

最后一个问题:IntelliJ 以Unchecked generics array creation for varargs parameter 的形式向我发出有关查询的警告,在这种情况下我无法理解。有人可以帮忙澄清一下吗?

谢谢!

【问题讨论】:

    标签: graph gremlin amazon-neptune


    【解决方案1】:

    我可能会为您提供其他变体,但折叠/合并/展开通常是执行此操作的方式,因此代码最终看起来大致相同。请注意,有current discussions 对此模式进行了重大改进。

    另外,是否有任何提示可以使查询更具可读性?我正在使用 JAVA,并且由于需要链接这些步骤,因此无法将部分查询提取到它自己的方法中,以便它可以重用。这种理解正确吗?

    就我个人而言,我喜欢你采用的形式,但没有什么可以说链接意味着如果你更喜欢这种风格,你就不能提取部分。怎么样:

    private Traversal createCountryTraversal(String id, int pop) {
        return __.addV("Country").property(T.id, "America").property("Population", 300_000_000);
    }
    
    ...
    
        g.V("America")
                    .fold()
                    .coalesce(unfold(), createCountryTraversal("America", 300_000_000))
              .V("India")
                    .fold()
                    .coalesce(unfold(), createCountryTraversal("India",1_000_000_000))
               .V("America")
                    .outE("connected")
                    .inV()
                        .has(T.id, "India")
                    .fold()
                    .coalesce(unfold(), addE("connected")
                                            .property("distanceMiles", 8000)
                                            .from(V("America"))
                                            .to(V("India"))).next();
    

    您也可以将遍历对象传递给不同的函数以在它们中进行链接,然后以一种元构建器模式返回遍历。或者,如果您想要更优雅的东西,请创建 Gremlin DSL

    最后一个问题:IntelliJ 以 Unchecked generics array creation for varargs 参数的形式给我关于查询的警告,在这种情况下我无法理解。有人可以帮忙澄清一下吗?

    我认为这是coalesce() 的问题,但 Gremlin 中的一些泛型有一种在 Java 中生成此警告的方法。我不确定是否有办法避免该警告。

    【讨论】:

      猜你喜欢
      • 2019-01-14
      • 2022-11-09
      • 2016-11-08
      • 1970-01-01
      • 2018-02-11
      • 1970-01-01
      • 1970-01-01
      • 2020-12-13
      • 1970-01-01
      相关资源
      最近更新 更多