在 Gremlin 中以原子方式添加两个顶点和一条边的最佳方法

Best way to add two vertices and an edge atomically in Gremlin

我将 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 的形式向我发出有关查询的警告,在这种情况下我无法理解。有人可以帮忙澄清一下吗?

谢谢!

我可能会为您提供其他变体,但 fold/coalesce/unfold 通常是这样做的方式,因此代码最终看起来大致相同。请注意,此模式有 current discussions 个重大改进。

Also, are there any tips to make queries more readable? I'm using JAVA, and since the steps need to be chained, parts of the query cannot be extracted out to it's own method so that it's re-usable. Is this understanding correct?

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

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();

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

And a final question: IntelliJ gives me warnings about the query in the form of Unchecked generics array creation for varargs parameter which I am not being able to understand in this context. Can someone help clarify?

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