Search code examples
pythongremlin

How do I access the id and label values from gremlin-python valueMap


Problem

I am using gremlin-python and When I perform:

edge = g.E().valueMap(True).limit(1).toList()

I get, for example,

print(edge[0]) would give {<T.id: 1>: 68, <T.label: 4>: 'belongsTo'....

My own fields have string keys but for id and label fields, I do not know how to specify the keys. Supplying a string version "<T.id: 1>" does not work.


Solution

  • In gremlin-python id and label fields are keyed by T in gremlin_python.process.traversal

    Therefore, use T.id and T.label as keys, so for example,

    from gremlin_python.process.traversal import T
    
    ....
    
    edge = self.g.E().valueMap(True).toList()[0]
    print(edge[T.id], edge[T.label]) 
    

    Will provide the access to the id and the label fields' values.

    An important side note

    The element id value for Vertex and Edge should generally be treated as an opaque value by your code. Do not assume it is an integer on all graph database platforms. For example, neo4j uses an integer and Amazon Neptune uses a string value for id.