简体   繁体   English

如何在gremlin-python中为现有和新顶点添加边缘?

[英]How to add an edge to existing and new vertices in gremlin-python?

I've already seen this answer: Gremlin, How to add edge to existing vertex in gremlin-python and it wasn't really helpful. 我已经看到了这个答案: Gremlin,如何在gremlin-python中为现有顶点添加边线 ,但这并没有真正的帮助。 As suggested in one of the comments I did try to update gremlinpython 3.3.0 but then I get key error. 正如其中一项评论中所建议,我确实尝试更新gremlinpython 3.3.0,但随后出现关键错误。

Stack: JanusGraph 0.2.0, gremlinpython3.2.3 堆栈: JanusGraph 0.2.0,gremlinpython3.2.3

This is my code 这是我的代码

from gremlin_python import statics
from gremlin_python.structure.graph import Graph
from gremlin_python.process.graph_traversal import __
from gremlin_python.process.strategies import *
from gremlin_python.driver.driver_remote_connection import DriverRemoteConnection

graph = Graph()
g = graph.traversal().withRemote(DriverRemoteConnection('ws://localhost:8182/gremlin','g'))
martha = g.V().has('name','martha').next()
jack = g.V().has('name','jack').next()
#e_id = g.addE(jack,'likes',martha).next()
e_id = g.V(martha).as_('to').V(jack).addE("Likes").to('to').toList()
print e_id.toList()

StackTrace with gremlinpython 3.3.0 带有gremlinpython 3.3.0的StackTrace

Traceback (most recent call last):
  File "gremlin-py.py", line 9, in <module>
    martha = g.V().has('name','martha').next()
  File "/Users/arvindn/.virtualenvs/gremlinenv/lib/python2.7/site-packages/gremlin_python/process/traversal.py", line 70,in next
    return self.__next__()
  File "/Users/arvindn/.virtualenvs/gremlinenv/lib/python2.7/site-packages/gremlin_python/process/traversal.py", line 43,in __next__
    self.traversal_strategies.apply_strategies(self)
  File "/Users/arvindn/.virtualenvs/gremlinenv/lib/python2.7/site-packages/gremlin_python/process/traversal.py", line 352, in apply_strategies
    traversal_strategy.apply(traversal)
  File "/Users/arvindn/.virtualenvs/gremlinenv/lib/python2.7/site-packages/gremlin_python/driver/remote_connection.py", line 143, in apply
    remote_traversal = self.remote_connection.submit(traversal.bytecode)
  File "/Users/arvindn/.virtualenvs/gremlinenv/lib/python2.7/site-packages/gremlin_python/driver/driver_remote_connection.py", line 54, in submit
    results = result_set.all().result()
  File "/Users/arvindn/.virtualenvs/gremlinenv/lib/python2.7/site-packages/concurrent/futures/_base.py", line 429, in result
    return self.__get_result()
  File "/Users/arvindn/.virtualenvs/gremlinenv/lib/python2.7/site-packages/concurrent/futures/_base.py", line 381, in __get_result
    raise exception_type, self._exception, self._traceback
KeyError: None

In my case, 3.3.0 is throwing error for all queries including gV().next() . 就我而言, 3.3.0会为所有查询(包括gV().next()抛出错误。 Now going back to 3.2.3, addvertex and other queries are working absolutely fine, but I couldn't figure out how to add edges. 现在回到3.2.3,addvertex和其他查询工作正常,但我不知道如何添加边。 The same code when run with 3.2.3 produces, 与3.2.3一起运行时,相同的代码会产生,

StackTrace with gremlinpython 3.2.3 带有gremlinpython 3.2.3的StackTrace

Traceback (most recent call last):  File "gremlin-py.py", line 12, in <module>
    e_id = g.V(martha).as_('to').V(jack).addE("Likes").to('to').toList()
  File "/Users/arvindn/.virtualenvs/gremlinenv/lib/python2.7/site-packages/gremlin_python/process/traversal.py", line 52, in toList    return list(iter(self))
  File "/Users/arvindn/.virtualenvs/gremlinenv/lib/python2.7/site-packages/gremlin_python/process/traversal.py", line 70, in next
    return self.__next__()  File "/Users/arvindn/.virtualenvs/gremlinenv/lib/python2.7/site-packages/gremlin_python/process/traversal.py", line 43, in __next__
    self.traversal_strategies.apply_strategies(self)  File "/Users/arvindn/.virtualenvs/gremlinenv/lib/python2.7/site-packages/gremlin_python/process/traversal.py", line 284, in apply_strategies
    traversal_strategy.apply(traversal)
  File "/Users/arvindn/.virtualenvs/gremlinenv/lib/python2.7/site-packages/gremlin_python/driver/remote_connection.py", line 95, in apply    remote_traversal = self.remote_connection.submit(traversal.bytecode)  File "/Users/arvindn/.virtualenvs/gremlinenv/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 "/Users/arvindn/.virtualenvs/gremlinenv/lib/python2.7/site-packages/tornado/ioloop.py", line 457, in run_sync
    return future_cell[0].result()  File "/Users/arvindn/.virtualenvs/gremlinenv/lib/python2.7/site-packages/tornado/concurrent.py", line 237, in result
    raise_exc_info(self._exc_info)
  File "/Users/arvindn/.virtualenvs/gremlinenv/lib/python2.7/site-packages/tornado/gen.py", line 285, in wrapper
    yielded = next(result)
  File "/Users/arvindn/.virtualenvs/gremlinenv/lib/python2.7/site-packages/gremlin_python/driver/driver_remote_connection.py", line 69, in submit_traversal_bytecode
    "gremlin": self._graphson_writer.writeObject(bytecode),
  File "/Users/arvindn/.virtualenvs/gremlinenv/lib/python2.7/site-packages/gremlin_python/structure/io/graphson.py", line 72, in writeObject
    return json.dumps(self.toDict(objectData), separators=(',', ':'))
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/json/__init__.py", line 250, in dumps
    sort_keys=sort_keys, **kw).encode(obj)
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/json/encoder.py", line 207, in encode
    chunks = self.iterencode(o, _one_shot=True)
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/json/encoder.py", line 270, in iterencode
    return _iterencode(o, 0)
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/json/encoder.py", line 184, in default
    raise TypeError(repr(o) + " is not JSON serializable")
TypeError: v[4184] is not JSON serializable

It says v[x] is not JSON serializable. 它说v[x]不可JSON序列化。 I'm not sure what causes this error. 我不确定是什么原因导致此错误。 It'll be awesome if someone can help. 如果有人可以帮助,那就太好了。 If any more info is needed, I shall update the question accordingly. 如果需要更多信息,我将相应地更新问题。

JanusGraph 0.2.0 uses Apache TinkerPop 3.2.6 . JanusGraph 0.2.0使用Apache TinkerPop 3.2.6 You should use the 3.2.6 version of the gremlinpython driver. 您应该使用gremlinpython驱动程序的3.2.6版本。

pip uninstall gremlinpython
pip install gremlinpython==3.2.6

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM