简体   繁体   English

在 Gremlin 的一次遍历中插入顶点和边

[英]Upsert of Vertices and Edge in a single Traversal in Gremlin

I've been trying for several hours to write a gremlin statement to handle upserting 2 vertices and 1 edge between them, without much luck.我已经尝试了几个小时来编写一个 gremlin 语句来处理它们之间的插入 2 个顶点和 1 个边缘,但运气不佳。

in pseudo-gremlin what I want to do is pretty straightforward and is the following:在伪gremlin中,我想做的非常简单,如下所示:

g.V()
.hasLabel("person")
.has("custom_id", "123")
.fold()
.coalesce(
  __.unfold().property(single, "name", "Tom"), 
  __.addV("person").property(single, "custom_id", "123").property(single, "name", "Tom"))
.as("fromStep")
.V()
.hasLabel("person")
.has("custom_id", "654")
.fold()
.coalesce(
  __.unfold().property(single, "name", "Sally"),
  __.addV("person").property(single, "custom_id", "654").property(single, "name", "Sally"))
.as("toStep")
.E()
.hasLabel("knows")
.where(__.inV().is("fromStep"))
.where(__.outV().is("toStep"))
.fold()
.coalesce(
  __.unfold().property("since", "2020-01-01"),
  __.addE("knows").property("since", "2020-01-01").from("fromStep").to("toStep")

The problem with this code is that each fold step, being a barrier step "removes" the value of the previous as steps.这段代码的问题在于, as障碍步骤的每个fold步骤都会“删除”前一个步骤的值。

Is there a way to do this properly in a single statement?有没有办法在一个语句中正确地做到这一点?

SOLUTION解决方案

Thanks to Kelvin answer's here's the solution:)感谢开尔文的回答,这是解决方案:)

g.V()
.hasLabel("person")
.has("custom_id", "123")
.fold()
.coalesce(
    __.unfold().property(single, "name", "Tom"), 
    __.addV("person").property(single, "custom_id", "123").property(single, "name", "Tom"))
.store("a")
.V()
.hasLabel("person")
.has("custom_id", "654")
.fold()
.coalesce(
    __.unfold().property(single, "name", "Sally"),
    __.addV("person").property(single, "custom_id", "654").property(single, "name", "Sally"))
.store("b")
.fold()
.select("a").unfold().as("from")
.select("b").unfold().coalesce(
    __.inE("knows").where(__.outV().as("from")).property("since", "2020-01-01"),
    __.addE("knows").property("since", "2020-01-01").from("from")
)

As you noted, labels applied using an as step are lost after a barrier/map step like fold is used.如您所述,使用as步骤应用的标签在使用像fold这样的屏障/地图步骤后会丢失。 However, store and aggregate bulk sets will survive intact.但是, storeaggregate批量集将完好无损。 Here is a simple pattern that works after a fold that you should be able to adapt for your use case.这是一个在fold后工作的简单模式,您应该能够适应您的用例。 As we discussed in the comments, if you knew the ID of the vertices you were searching for and if the ID was not found you created it that would give you another way to approach this.正如我们在评论中讨论的那样,如果您知道要搜索的顶点的 ID,并且如果找不到 ID,则您创建了它,这将为您提供另一种解决方法。 Anyway, here is one way that works in the presence of a fold step.无论如何,这是一种在fold步骤存在的情况下起作用的方法。

gremlin> g.V('3').store('a').
......1>   V('4').store('b').
......2>   fold().
......3>   select('a').unfold().as('from').
......4>   select('b').unfold().as('to').
......5>   addE('test').
......6>     from('from').
......7>     to('to')    

==>e[60876][3-test->4] 

One other note, in general mid traversal E steps are not supported.另请注意,一般不支持中间遍历E步骤。 It really should be written as V().outE()它真的应该写成V().outE()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM