0

When using gremlinpython, is it possible to only return a list of IDs for edges, instead of returning this long winded dictionary?

So, currently g.E().limit(10).id().toList() returns this:

[{'@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'}}]

But instead I want it to return this:

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

This works as expected in the gremlin console.

Python3.7, gremlinpython==3.4.2

Ian
  • 3,605
  • 4
  • 31
  • 66
  • 1
    I think JanusGraph must be returning it that way for some reason. I have tried your query using GremlinPython with TinkerGraph/GremlinServer and Amazon Neptune and I get back the expected list of IDs. – Kelvin Lawrence May 11 '20 at 00:10
  • Thanks Kevin, I'm also certain the issue is with JanusGraph. I wonder why they decided to return the IDs this way. – Ian May 11 '20 at 13:27
  • What back end store are you using? - not sure if that will matter but using "inmemory" might be worth a quick test to rule out some back end oddity. It's odd the console gives you what you expect but Python does not. – Kelvin Lawrence May 11 '20 at 20:51

1 Answers1

2

JanusGraph serializes the RelationIdentifier to a Map - you can see the code here. This result differs from what you get in Gremlin Console because the console uses a special "ToString" serializer which simply calls the toString() method on each result item sent back to it from the server.

The easiest workaround I can think of would be to write your own deserializer for "janusgraph:RelationIdentifier" in Python then get it added to the list of deserializers for the version of GraphSON you are using. I've not tested this but I imagine the code would look something like:

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

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

Here's a test that demonstrates how to add a custom serializer and how to override one:

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

stephen mallette
  • 45,298
  • 5
  • 67
  • 135