【问题标题】:gremlinpython - get IDs from edges as simple list instead of dictionarygremlinpython - 从边缘获取 ID 作为简单列表而不是字典
【发布时间】:2020-05-09 17:30:45
【问题描述】:

使用 gremlinpython 时,是否可以返回边的 ID 列表,而不是返回这个冗长的字典?

所以,目前g.E().limit(10).id().toList() 返回这个:

[{'@type': 'janusgraph:RelationIdentifier',
  '@value': {'relationId': '4g09-20qw-2dx-1l1c'}},
 {'@type': 'janusgraph:RelationIdentifier',
  '@value': {'relationId': '5hxx-9x9k-2dx-4qo8'}},
 {'@type': 'janusgraph:RelationIdentifier',
  '@value': {'relationId': 'cljk-qikg-2dx-pzls'}},
 {'@type': 'janusgraph:RelationIdentifier',
  '@value': {'relationId': '4vth-1xns-2dx-8940'}},
 {'@type': 'janusgraph:RelationIdentifier',
  '@value': {'relationId': '5f61-bex4-2dx-sgw'}},
 {'@type': 'janusgraph:RelationIdentifier',
  '@value': {'relationId': '5xc3-ag48-2dx-a6og'}},
 {'@type': 'janusgraph:RelationIdentifier',
  '@value': {'relationId': '5xc6-4awg-2dx-f6v4'}},
 {'@type': 'janusgraph:RelationIdentifier',
  '@value': {'relationId': 'bwnk-k0ow-2dx-7dio'}},
 {'@type': 'janusgraph:RelationIdentifier',
  '@value': {'relationId': '5lhi-pbk-2dx-2wfc'}},
 {'@type': 'janusgraph:RelationIdentifier',
  '@value': {'relationId': '5d6x-avyg-2dx-7gns'}}]

但我希望它返回这个:

['4g09-20qw-2dx-1l1c', '5hxx-9x9k-2dx-4qo8', 'cljk-qikg-2dx-pzls', '4vth-1xns-2dx-8940', '5f61-bex4-2dx-sgw', '5xc3-ag48-2dx-a6og', '5xc6-4awg-2dx-f6v4', 'bwnk-k0ow-2dx-7dio', '5lhi-pbk-2dx-2wfc', '5d6x-avyg-2dx-7gns']

这在 gremlin 控制台中按预期工作。

Python3.7、gremlinpython==3.4.2

【问题讨论】:

  • 我认为 JanusGraph 出于某种原因必须以这种方式返回它。我已经尝试使用 GremlinPython 与 TinkerGraph/GremlinServer 和 Amazon Neptune 进行查询,我得到了预期的 ID 列表。
  • 谢谢凯文,我也确定问题出在 JanusGraph 上。我想知道他们为什么决定以这种方式返回 ID。
  • 您使用的是什么后端商店? - 不确定这是否重要,但使用“内存”可能值得快速测试以排除一些后端奇怪。奇怪的是控制台给了你你期望的东西,但 Python 没有。

标签: gremlin janusgraph gremlinpython


【解决方案1】:

JanusGraph 将RelationIdentifier 序列化为Map - 你可以看到代码here。此结果与您在 Gremlin 控制台中获得的结果不同,因为控制台使用了一个特殊的“ToString”序列化程序,它只对从服务器发送回它的每个结果项调用 toString() 方法。

我能想到的最简单的解决方法是在 Python 中为“janusgraph:RelationIdentifier”编写自己的反序列化程序,然后将其添加到您正在使用的 GraphSON 版本的list of deserializers。我没有对此进行测试,但我想代码看起来像:

class RelationIdentifierJanusDeserializer(_GraphSONTypeIO):
    graphson_type = "janusgraph:RelationIdentifier"

    @classmethod
    def objectify(cls, d, reader):
        return str(d)

这是一个演示如何添加自定义序列化程序以及如何覆盖的测试:

https://github.com/apache/tinkerpop/blob/5c91324afeedf7e233c93181423fea285a76d1d1/gremlin-python/src/main/python/tests/structure/io/test_graphsonV3d0.py#L255-L286

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-03-08
    • 1970-01-01
    • 1970-01-01
    • 2021-11-30
    • 2022-12-01
    • 1970-01-01
    • 2022-01-13
    • 1970-01-01
    相关资源
    最近更新 更多