如果顶点不存在则如何添加顶点并继续使用其他图形突变进行单次遍历?

时间:2016-12-24 14:25:10

标签: gremlin

目前我有这个gremlin / groovy代码:

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

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

我尝试了这个并且它不起作用(它不添加数字或用户顶点)

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的单个事务中添加另一个用户:

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

谢谢!

1 个答案:

答案 0 :(得分:2)

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

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]]