[英]Upsert of Vertices and Edge in a single Traversal in Gremlin
我已经尝试了几个小时来编写一个 gremlin 语句来处理它们之间的插入 2 个顶点和 1 个边缘,但运气不佳。
在伪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")
这段代码的问题在于, as
障碍步骤的每个fold
步骤都会“删除”前一个步骤的值。
有没有办法在一个语句中正确地做到这一点?
感谢开尔文的回答,这是解决方案:)
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
步骤应用的标签在使用像fold
这样的屏障/地图步骤后会丢失。 但是, store
和aggregate
批量集将完好无损。 这是一个在fold
后工作的简单模式,您应该能够适应您的用例。 正如我们在评论中讨论的那样,如果您知道要搜索的顶点的 ID,并且如果找不到 ID,则您创建了它,这将为您提供另一种解决方法。 无论如何,这是一种在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]
另请注意,一般不支持中间遍历E
步骤。 它真的应该写成V().outE()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.