如何在 Python Gremlin 变体中添加边

Posted

技术标签:

【中文标题】如何在 Python Gremlin 变体中添加边【英文标题】:How to add an edge in Python Gremlin variant 【发布时间】:2017-04-25 11:03:05 【问题描述】:

我正在尝试使用 gremlin-python 创建一个图形,但我似乎不知道如何添加边。

使用标准 Gremlin 控制台,我可以执行以下操作:

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> 

但是当尝试通过连接到 gremlin 服务器的 python 执行相同操作时,我似乎无法执行相同操作:

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

我尝试了各种咒语,但似乎无法解决,并且在任何地方都找不到任何示例。另外,我在 Gremlin 文档中看到了对 addEaddEdge 的引用,但无法弄清楚它们的区别是什么(上面似乎都不起作用)。

编辑:更进一步,但仍然没有运气。似乎GraphTraversal.addE() 存在,所以如果我不调用next(),那么我可以调用addE...但我似乎仍然无法获得它喜欢的参数。

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

【问题讨论】:

【参考方案1】:

据我所知,addEdge() 作用于图对象,addE() 作用于图遍历对象。由于您使用的是g(),后者是后者,因此您需要addE()

【讨论】:

【参考方案2】:

似乎以下语法有效:

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

我仍然不清楚 addEaddEdge 之间的区别,但我猜后者在 python 中不可用,我混淆了它们的签名。

【讨论】:

这不起作用并引发错误vertex has no attribute called addE a 是一个 'Vertex' ,但是你需要从一个 ~traversal step~ 调用 .addE() 来表示一个顶点: gV(a).addE('foo').to(b) .toList() g.addE('foo').from_(a).to(b).next() 为我工作。

以上是关于如何在 Python Gremlin 变体中添加边的主要内容,如果未能解决你的问题,请参考以下文章

如果 gremlin 中存在边,则获取布尔值

如何在单个 gremlin 查询中将两个顶点详细信息和边属性详细信息合并在一起

如何从顶点scala gremlin获取所有传入/传出边的所有顶点

Gremlin 从与单个顶点相关的边列表中获取最高值

Gremlin 组所有返回的边及其顶点

如何使用BindingsGremlinPlugin类为嵌入式gremlin-server添加绑定?