Ian
Ian

Reputation: 3908

gremlinpython - get IDs from edges as simple list instead of dictionary

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

Upvotes: 0

Views: 402

Answers (1)

stephen mallette
stephen mallette

Reputation: 46226

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

Upvotes: 2

Related Questions