0

I have the following code to setup a graph inside AWS Neptune. How can I find all ancestors for vertext "a"?

Edges:

a -> b
b -> c
c -> d
d -> e

a -> f
a -> g

g -> h

Source Code:

from gremlin_python import statics
from gremlin_python.structure.graph import Graph
from gremlin_python.process.graph_traversal import __
from gremlin_python.process.strategies import *
from gremlin_python.driver.driver_remote_connection import DriverRemoteConnection

graph = Graph()
remoteConn = DriverRemoteConnection('http://localhost:9999','g')
g = graph.traversal().withRemote(remoteConn)

g.addV('a').property(id, '1').next()
g.addV('b').property(id, '2').next()
g.addV('c').property(id, '3').next()
g.addV('d').property(id, '4').next()
g.addV('e').property(id, '5').next()
g.addV('f').property(id, '6').next()
g.addV('g').property(id, '7').next()
g.addV('h').property(id, '7').next()

g.V('a').addE('parent').to(g.V('b')).next()
g.V('b').addE('parent').to(g.V('c')).next()
g.V('c').addE('parent').to(g.V('d')).next()
g.V('d').addE('parent').to(g.V('e')).next()

g.V('a').addE('parent').to(g.V('f')).next()
g.V('a').addE('parent').to(g.V('g')).next()
g.V('g').addE('parent').to(g.V('h')).next()

remoteConn.close()
Kelvin Lawrence
  • 14,674
  • 2
  • 16
  • 38
user1187968
  • 7,154
  • 16
  • 81
  • 152

1 Answers1

1

You can use the code below to create the graph and return the path from a to all the ancestors. I had to clean up the code above a little bit to fix some errors but what is below should work for you.

from gremlin_python import statics
from gremlin_python.structure.graph import Graph
from gremlin_python.process.graph_traversal import __
from gremlin_python.process.strategies import *
from gremlin_python.process.traversal import T
from gremlin_python.driver.driver_remote_connection import DriverRemoteConnection

graph = Graph()
remoteConn = DriverRemoteConnection('wss://<your server name here>:8182/gremlin','g')
g = graph.traversal().withRemote(remoteConn)

g.addV('a').property(T.id, '1').next()
g.addV('b').property(T.id, '2').next()
g.addV('c').property(T.id, '3').next()
g.addV('d').property(T.id, '4').next()
g.addV('e').property(T.id, '5').next()
g.addV('f').property(T.id, '6').next()
g.addV('g').property(T.id, '7').next()
g.addV('h').property(T.id, '8').next()

g.V('1').addE('parent').to(__.V('2')).next()
g.V('2').addE('parent').to(__.V('3')).next()
g.V('3').addE('parent').to(__.V('4')).next()
g.V('4').addE('parent').to(__.V('5')).next()

g.V('5').addE('parent').to(__.V('6')).next()
g.V('6').addE('parent').to(__.V('7')).next()
g.V('7').addE('parent').to(__.V('8')).next()

result = g.V().hasLabel('a').repeat(__.out('parent')).until(__.outE('parent').count().is_(0)).path().toList()
remoteConn.close()
print(result)

I am also not sure if you mean to but you are giving each vertex a different label. Not that you can't do that but the purpose of labels is to group items together so normally you would have something like this:

g.addV('person').property(id, '1').next()
g.addV('person').property(id, '2').next()
bechbd
  • 6,206
  • 3
  • 28
  • 47