Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

gremlinpython - return id and label as string

Tags:

python

gremlin

I'm using gremlinpython 3.4.1 on Python 3.7.2, and when get vertex/edge responses it is providing <T.id: > for id and <T.label: 3> for the label. How would I get it to provide the string value for id and label in the response instead? My aim is take the output and generate JSON

The output:

python3 stackoverflow.py
[{'name': ['USA'], 'parentname': ['USA'], 'shortname': ['US'], <T.id: 1>: 'country-us', 'parentid': ['country-us'], <T.label: 3>: 'Country'}]

The code:

from gremlin_python import statics
from gremlin_python.process.anonymous_traversal import traversal
from gremlin_python.process.graph_traversal import __
from gremlin_python.process.strategies import *
from gremlin_python.driver.driver_remote_connection import DriverRemoteConnection
from gremlin_python.process.traversal import T
from gremlin_python.process.traversal import Order
from gremlin_python.process.traversal import Cardinality
from gremlin_python.process.traversal import Column
from gremlin_python.process.traversal import Direction
from gremlin_python.process.traversal import Operator
from gremlin_python.process.traversal import P
from gremlin_python.process.traversal import Pop
from gremlin_python.process.traversal import Scope
from gremlin_python.process.traversal import Barrier
from gremlin_python.process.traversal import Bindings
from gremlin_python.process.traversal import WithOptions

CLUSTER_ENDPOINT = "removed"
PORT = "8182"
g = traversal().withRemote(DriverRemoteConnection('wss://' + CLUSTER_ENDPOINT + ':' + PORT + '/gremlin','g'))

response = g.V().has('name', 'USA').limit(1000).hasLabel('Country').valueMap(True).toList()
print(response)

BTW - I have attemped to use .with_(WithOptions.ids) for example:

response = g.V().has('name', 'USA').limit(1000).hasLabel('Country').valueMap(True).with_(WithOptions.ids).toList()

for which I get the following error:

gremlin_python.driver.protocol.GremlinServerError: 599: {"requestId":"bf74df44-f064-4411-a1cb-78b30f9d2cf6","code":"InternalFailureException","detailedMessage":"Could not locate method: NeptuneGraphTraversal.with([1])"}

like image 920
MrDB Avatar asked Oct 31 '25 09:10

MrDB


1 Answers

Additionally, to the project() example that was already given, you could do something like the following if you can't or don't want to specify the property names:

g.V().has('name', 'USA').limit(1000).hasLabel('Country').
  map(union(project('id','label').
              by(id).
              by(label),
            valueMap()).unfold().
      group().
        by(keys).
        by(select(values))) // select(values).unfold() if you only have single values
like image 63
Daniel Kuppitz Avatar answered Nov 02 '25 23:11

Daniel Kuppitz



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!