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.

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


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

Need Your Help

Android Jenkins Builds

android jenkins

I am developing Android apps on Windows platform. I can create and build android apps using command-line tools successfully on the local machine without a problem.

Weka Classification

machine-learning classification weka

I was trying to data model a Classification Machine Learning algorithm on a data set which has 32 attributes,the last column being Target class.I refined the attributes number in to 6 from 32 ,whic...