Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
782 views
in Technique[技术] by (71.8m points)

groovy - Gremlin - only add a vertex if it doesn't exist

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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]

Also see this StackOverflow question for more information on upsert/"get or create" patterns.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...