Question

Is there a way to iterate through every node in a neo4j database using py2neo?

My first thought was iterating through GraphDatabaseService, but that didn't work. If there isn't a way to do it with py2neo, is there another python interface that would let me?

Edit: I'm accepting @Nicholas's answer for now, but I'll update it if someone can give me a way that returns a generator.

Was it helpful?

Solution

I would suggest doing that with asynchronous Cypher, something like:

    from py2neo import neo4j, cypher

    graph_db = neo4j.GraphDatabaseService()

    def handle_row(row):
        node = row[0]
        # do something with `node` here

    cypher.execute(graph_db, "START z=node(*) RETURN z", row_handler=handle_row)

Of course you might want to exclude the reference node or otherwise tweak the query.

Nige

OTHER TIPS

One of two solutions come to mind. Either do a cypher query

START n=node(*) return n

The other, and I'm not familiar with python so I'm going to give the example in Java is

GlobalGraphOperations.at(graphDatabaseService).getAllNodes()

which is the way the the old deprecated graphDatabaseService.getAllNodes() recommends.

For newer versions of py2neo the accepted version no longer works. Instead use:

from py2neo import Graph

graph = Graph("http://user:pass@localhost:7474/db/data/")

for n in graph.cypher.stream("START z=node(*) RETURN z"):
    //do something with node here
    print n
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top