Search code examples
gremlinsubgraphgremlinpython

gremlin python subgraph always empty


I use gremlin python to connect the gremlin server like this:

g = traversal().withRemote(DriverRemoteConnection('ws://localhost:8182/gremlin', 'g'))

But when I work with subgraph(referenced here http://tinkerpop.apache.org/docs/current/reference/#subgraph-step):

g.V().has(*vertex).repeat(__.inE().subgraph('subGraph').outV()).times(1).cap('subGraph').toList()

The result always is empty like this: [{}]

Where am I wrong? How can I get a subgraph using gremlin-python?


Solution

  • The tree() and subgraph() steps are not yet supported in Gremlin Language Variants (GLVs) (TINKERPOP-2063). The main problem is that GLVs are not full Gremlin Virtual Machine implementations and therefore do not have a Graph instance to deserialize a subgraph into.

    To work around this limitation you would need to capture the subgraph as a side-effect yourself to gather up its data. For example, you might use store() for this purpose as shown in the following example where I grab a "knows" subgraph:

    gremlin> g.V().hasLabel('person').store('v').by(elementMap()).
    ......1>   outE('knows').store('e').by(elementMap()).
    ......2>   inV().store('v').by(elementMap()).
    ......3>   cap('v').dedup().
    ......4>   project('vertices','edges').
    ......5>     by().
    ......6>     by(cap('e')).next()
    ==>vertices={{id=1, label=person, myid=1, name=marko, age=29}=1, {id=2, label=person, myid=2, name=vadas, age=27}=2, {id=4, label=person, myid=4, name=josh, age=32}=2, {id=6, label=person, myid=6, name=peter, age=35}=1}
    ==>edges={{id=7, label=knows, IN={id=2, label=person}, OUT={id=1, label=person}, weight=0.5}=1, {id=8, label=knows, IN={id=4, label=person}, OUT={id=1, label=person}, weight=1.0}=1}
    

    If there is some reason that you absolutely must use subgraph() then the only other option would be to send a Gremlin script (rather than bytecode). The script would first execute the traversal with subgraph() as in your example but then in the same script write the subgraph to a GraphSON string (or the like). Python would then get a string representation of the graph. Of course, you would then have to process that format in some way on your client side.