gremlintinkerpopgremlinpython

Gremlin Python "name 'V' is not defined" when using coalesce to chain insert edges


I am trying to batch insert edges using coalesce . And I was trying to follow this format for batch edge insert that I found on Neptune DB documentation:

g.V('v-1')
 .outE('KNOWS')
 .hasId('e-1')
 .fold()
 .coalesce(unfold(),
           V('v-1').addE('KNOWS')
                   .to(V('v-2'))
                   .property(id, 'e-1'))
 .V('v-3')
 .outE('KNOWS')
 .hasId('e-2').fold()
 .coalesce(unfold(),
           V('v-3').addE('KNOWS')
                   .to(V('v-4'))
                   .property(id, 'e-2'))
 .V('v-5')
 .outE('KNOWS')
 .hasId('e-3')
 .fold()
 .coalesce(unfold(),
           V('v-5').addE('KNOWS')
                   .to(V('v-6'))
                   .property(id, 'e-3'))
 .next()

When I used this format in my Python script, I get the error that name 'V' is not defined. I know Gremlin Python is slightly different to the Gremlin language. But I couldn't find documentation on this.

I tried adding double underscore in front of V like this:

g.V('v-1')
 .outE('KNOWS')
 .hasId('e-1')
 .fold()
 .coalesce(unfold(),
           __.V('v-1').addE('KNOWS')
                   .to(__.V('v-2'))
                   .property(id, 'e-1'))
...
 .next()

but I got this error Received error message '{'requestId': 'None', 'status': {'code': 499, 'message': '{"detailedMessage":"Invalid OpProcessor requested [null]","code":"UnsupportedOperationException"}', 'attributes': {}}, 'result': {'meta': {}, 'data': None}}'

I connect to gremlin like this:

def get_connection(endpoint): 
   return DriverRemoteConnection(endpoint, 'g')

def get_graph(connection): 
   return traversal().withRemote(connection) 
   
connex = get_connection(neptune_endpoint) 
g = get_graph(connex)

Edit The solution was to have __.V instead of V and change id to T.id. Thanks @Kelvin Lawrence for the help and prompt response!


Solution

  • When using the Python client, to work around reserved word conflicts and other issues, a few things are different. As you noticed, an anonymous traversal (a traversal nested inside another one) needs to use the "double underscore" notation as in __.V('1').

    Similarly, there are several built in functions in Python that collide with Gremlin names. One such is id. To work around this you can just specify T.id instead.