how to add a vertex only if it doesn't exist and continue this single traversal with other graph mutations?

给你一囗甜甜゛ 提交于 2019-12-24 07:10:03

问题


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?

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

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

Thanks!


回答1:


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


来源:https://stackoverflow.com/questions/41314091/how-to-add-a-vertex-only-if-it-doesnt-exist-and-continue-this-single-traversal

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!