如何仅在不存在顶点的情况下添加顶点,并与其他图形突变一起继续此遍历? [英] how to add a vertex only if it doesn't exist and continue this single traversal with other graph mutations?

查看:64
本文介绍了如何仅在不存在顶点的情况下添加顶点,并与其他图形突变一起继续此遍历?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

目前,我有以下gremlin/groovy代码:

Currently I have this gremlin/groovy code:

if(!g.V().has("Number","number","3").hasNext()) {
   g.addV("Number").property("number","3")
}

在不使用多次遍历的情况下是否有可能获得相同的结果?

Is it possible to have the same result without using multiple traversals?

我尝试了此操作,但它不起作用(它不添加Number或User顶点)

I tried this and it doesn't work (it doesn't add either Number or User vertices)

g.V().choose(has("Number","number", "3"),
        addV("Number").property("number", "3"),
        has("Number","number", "3")
    ).as("number")
   .addV("User").property("uuid","test uuuid")
   .forEachRemaining(System.out::println);

我尝试了此处的建议( https://stackoverflow.com/a/33965737/986160 ),但是它不允许我在一次交易中为DSE添加另一个用户来继续我的遍历:

I tried what it was suggested here (https://stackoverflow.com/a/33965737/986160) but it doesn't allow me to continue my single traversal with adding another user in a single transaction for DSE:

g.V()
 .has("Number","number", "3")
.tryNext()
.orElseGet(
   () -> g.addV("Number")
          .property("number", "3").next()
 );

谢谢!

推荐答案

很遗憾,我们还没有g.coalesce(),但是有一种解决方法:

Unfortunately we don't have g.coalesce() yet, but there's a workaround:

gremlin> g = TinkerGraph.open().traversal()
==>graphtraversalsource[tinkergraph[vertices:0 edges:0], standard]
gremlin> g.inject(1).coalesce(V().has("Number", "number", 3), addV("Number").property("number", 3))
==>v[0]
gremlin> g.inject(1).coalesce(V().has("Number", "number", 3), addV("Number").property("number", 3))
==>v[0]
gremlin> g.inject(1).coalesce(V().has("Number", "number", 3), addV("Number").property("number", 3))
==>v[0]
gremlin> g.V().valueMap()
==>[number:[3]]

这篇关于如何仅在不存在顶点的情况下添加顶点,并与其他图形突变一起继续此遍历?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆