1

right now I am trying to use python to implement some gremlin logic for neptunedb of aws. I want to check if one edge exist or not, if exist, ignore, otherwise add the edge.

for gremlin console, we can do it like this:

g.V().has('people','name', 'somebody').as('v').V().has('software','name','ripple').coalesce(__.inE('Created').where(outV().as('v')), addE('created').from('v').property('weight',0.5))

but I kind of lost how to convert this to python. It seems python cannot recoginize as('v')? any hints? or where can i find reference documentation for gremlin python.

Hongli Bu
  • 461
  • 12
  • 37

2 Answers2

4

From the TinkerPop Gremlin documentation:

The term as is a reserved word in Python, and therefore must be referred to in Gremlin with as_().

It's the same case for from. Just replace your as with as_ and from with from_ and it should work.

Rob Streeting
  • 1,675
  • 3
  • 16
  • 27
  • 1
    In addition, ```g.V().has('people','name', 'somebody')``` should changed to ```g.V().hasLabel(people).has('name', 'somebody')```, right? – Hongli Bu Oct 02 '19 at 18:10
  • One this, if I wanna to use id, should I do it using T? like ```g.V().hasLabel(people).has(T.id, 'somebody')``` – Hongli Bu Oct 02 '19 at 18:11
  • 1
    For your first comment, that isn't necessary - has() will accept a label as the first argument: "has(label, key, value): Remove the traverser if its element does not have the specified label and provided key/value property.". I think you may want `hasIds()` for checking id, take a closer look at the docs for has() http://tinkerpop.apache.org/docs/current/reference/#has-step – Rob Streeting Oct 02 '19 at 18:34
  • 1. Are these two queries same thing? If so, do they have any difference in performance? 2. Actually I want to ask how to check a vertex exist, if I dump the vertices through csv with header ```"~id, ~label"```. Can I check it like ```g.V().has('people', id, 'somebody')``` or ```g.V().has('people',T.id, 'somebody')``` or ```g.V().has('people','id', 'somebody')```? Actually, I am a little confused about id in neptunedb – Hongli Bu Oct 02 '19 at 19:18
2

Just to say this a bit more clearly, for python you need to do the following:

g.V().has('people','name', 'somebody').\
    as_('v').V().has('software','name','ripple').coalesce(
    __.inE('Created').where(__.outV().as_('v')), 
    __.addE('created').from_('v').\
    property('weight',0.5)).iterate()

The final iterate() at the end is important as you need to provide a terminal step when working in python, compared with the gremlin console. You can also use other terminal steps like next(), but without any terminal step the edge won't be created (at least in Neptune)

cts
  • 1,790
  • 1
  • 13
  • 27