How to add an edge in Python Gremlin variant
Asked Answered
U

2

6

I'm trying to create a graph using gremlin-python, but I can't seem to work out how to add an edge.

Using the standard Gremlin console I can do the following:

gremlin> a = g.addV().next()
==>v[0]
gremlin> b = g.addV().next()
==>v[1]
gremlin> g.V()
==>v[0]
==>v[1]
gremlin> a.addEdge('conn', b)
==>e[2][0-conn->1]
gremlin> g.E()
==>e[2][0-conn->1]
gremlin> 

But when trying to do the same via python connected to gremlin server, I can't seem to do the same:

>>> a = g.addV().next()
>>> b = g.addV().next()
>>> g.V().toList()
[v[1519], v[1520]]
>>> a.addEdge('conn', b)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: 'Vertex' object has no attribute 'addEdge'

I've tried various incantations, but can't seem to work it out, and can't find any examples anywhere. Also, I see reference in the Gremlin docs to both addE and addEdge but can't work out what the difference is (neither appear to work above).

Edit: Getting a bit further, but still no luck. It seems GraphTraversal.addE() exists, so if I don't call next() then I can call addE... but still I don't seem to be able to get the arguments something it likes.

>>> a = g.addV()
>>> b = g.addV()
>>> a.addE('foo', b).toList()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/Development/matt/lib/python2.7/site-packages/gremlin_python/process/traversal.py", line 52, in toList
    return list(iter(self))
  File "/Development/matt/lib/python2.7/site-packages/gremlin_python/process/traversal.py", line 70, in next
    return self.__next__()
  File "/Development/matt/lib/python2.7/site-packages/gremlin_python/process/traversal.py", line 43, in __next__
    self.traversal_strategies.apply_strategies(self)
  File "/Development/matt/lib/python2.7/site-packages/gremlin_python/process/traversal.py", line 284, in apply_strategies
    traversal_strategy.apply(traversal)
  File "/Development/matt/lib/python2.7/site-packages/gremlin_python/driver/remote_connection.py", line 95, in apply
    remote_traversal = self.remote_connection.submit(traversal.bytecode)
  File "/Development/matt/lib/python2.7/site-packages/gremlin_python/driver/driver_remote_connection.py", line 53, in submit
    traversers = self._loop.run_sync(lambda: self.submit_traversal_bytecode(request_id, bytecode))
  File "/Development/matt/lib/python2.7/site-packages/tornado/ioloop.py", line 457, in run_sync
    return future_cell[0].result()
  File "/Development/matt/lib/python2.7/site-packages/tornado/concurrent.py", line 237, in result
    raise_exc_info(self._exc_info)
  File "/Development/matt/lib/python2.7/site-packages/tornado/gen.py", line 1021, in run
    yielded = self.gen.throw(*exc_info)
  File "/Development/matt/lib/python2.7/site-packages/gremlin_python/driver/driver_remote_connection.py", line 73, in submit_traversal_bytecode
    traversers = yield self._execute_message(message)
  File "/Development/matt/lib/python2.7/site-packages/tornado/gen.py", line 1015, in run
    value = future.result()
  File "/Development/matt/lib/python2.7/site-packages/tornado/concurrent.py", line 237, in result
    raise_exc_info(self._exc_info)
  File "/Development/matt/lib/python2.7/site-packages/tornado/gen.py", line 1021, in run
    yielded = self.gen.throw(*exc_info)
  File "/Development/matt/lib/python2.7/site-packages/gremlin_python/driver/driver_remote_connection.py", line 149, in _execute_message
    recv_message = yield response.receive()
  File "/Development/matt/lib/python2.7/site-packages/tornado/gen.py", line 1015, in run
    value = future.result()
  File "/Development/matt/lib/python2.7/site-packages/tornado/concurrent.py", line 237, in result
    raise_exc_info(self._exc_info)
  File "/Development/matt/lib/python2.7/site-packages/tornado/gen.py", line 1024, in run
    yielded = self.gen.send(value)
  File "/Development/matt/lib/python2.7/site-packages/gremlin_python/driver/driver_remote_connection.py", line 236, in receive
    "{0}: {1}".format(status_code, recv_message["status"]["message"]))
gremlin_python.driver.driver_remote_connection.GremlinServerError: 599: Could not locate method: DefaultGraphTraversal.addE([foo, [AddVertexStep({})]])
Unilocular answered 9/12, 2016 at 21:43 Comment(0)
C
5

As far as I know, addEdge() works on the graph object and addE() works on the graph traversal object. Since you were using g() which is the latter, you need addE().

Credit answered 23/2, 2017 at 15:36 Comment(0)
U
2

Seems the following syntax works:

>>> a = g.addV()
>>> b = g.addV()
>>> a.addE('foo').to(b).toList()
[e[1534][1532-foo->1533]]

I'm still not clear on the difference between addE and addEdge but I guess the latter is not available in python and I was confusing the signature of them.

Unilocular answered 9/12, 2016 at 22:14 Comment(3)
This is not working and throws error vertex has no attribute called addEPinochle
a is a 'Vertex' , but you need to call .addE() from a ~traversal step~ representing a vertex: g.V(a).addE('foo').to(b).toList()Q
g.addE('foo').from_(a).to(b).next() worked for me.Hipparch

© 2022 - 2024 — McMap. All rights reserved.