Gremlin - 只添加一个顶点,如果它不存在 [英] Gremlin - only add a vertex if it doesn't exist
问题描述
我有一组用户名(如 ['abc','def','ghi']
)被添加到图的'user'标签下。
现在我首先要检查用户名是否已经存在( gV()。hasLabel('user')。has('username' ,'def')
),然后只添加那些username属性在'user'标签下不匹配的。
另外,
我使用的是Titan图形数据库,tinkerpop3和gremlin REST服务器。
这可以在单个gremlin查询或groovy脚本中完成吗?使用脚本,您总是可以将多行/命令脚本传递到服务器进行处理,以获得您想要的结果。然后这个问题用普通的编程技术使用变量,if / then语句等来回答: t = gV()。has ('person','name','bill')
t.hasNext()?下一个()
或者也许:
gV()。has('person','name',' (''')。property('name','bill')。next()}
但是这些都是groovy脚本,最终TinkerPop建议避免使用脚本和闭包来支持纯粹的遍历。在一次遍历中处理获取或创建的一般方法是执行如下操作:
gremlin> g.V()。有(人,名,法案)。折()。
...... 1> coalesce(unfold(),
...... 2> addV('person')。property('name','bill'))
==> v [18]
I have an array of usernames (eg. ['abc','def','ghi']
) to be added under 'user' label in the graph.
Now I first want to check if the username already exists (g.V().hasLabel('user').has('username','def')
) and then add only those for which the username property doesn't match under 'user' label.
Also, can this be done in a single gremlin query or groovy script?
I am using titan graph database, tinkerpop3 and gremlin REST server.
With "scripts" you can always pass a multi-line/command script to the server for processing to get what you want done. This question is then answered with normal programming techniques using variables, if/then statements, etc:
t = g.V().has('person','name','bill')
t.hasNext() ? t.next() : g.addV('person').property('name','bill').next()
or perhaps:
g.V().has('person','name','bill').tryNext().orElseGet{
g.addV('person').property('name','bill').next()}
But these are groovy scripts and ultimately TinkerPop recommends avoiding scripts and closures in favor of a pure traversal. The general way to handle a "get or create" in a single traversal is to do something like this:
gremlin> g.V().has('person','name','bill').fold().
......1> coalesce(unfold(),
......2> addV('person').property('name','bill'))
==>v[18]
这篇关于Gremlin - 只添加一个顶点,如果它不存在的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!