Search code examples
pythonneo4jpy2neo

Is it possible to iterate through all nodes with py2neo


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.


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