Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 739
beardc Avatar asked Jun 18 '12 02:06

beardc


2 Answers

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

like image 175
Nigel Small Avatar answered Oct 22 '22 07:10

Nigel Small


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.

like image 4
Nicholas Avatar answered Oct 22 '22 09:10

Nicholas