简体   繁体   English

是否可以使用py2neo迭代所有节点

[英]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? 有没有办法使用py2neo迭代neo4j数据库中的每个节点?

My first thought was iterating through GraphDatabaseService , but that didn't work. 我的第一个想法是迭代GraphDatabaseService ,但这不起作用。 If there isn't a way to do it with py2neo, is there another python interface that would let me? 如果没有办法用py2neo做,那么还有另一个python接口可以让我吗?

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. 编辑:我现在正在接受@Nicholas的答案,但如果有人能给我一个返回发电机的方法,我会更新它。

I would suggest doing that with asynchronous Cypher, something like: 我建议使用异步Cypher,例如:

    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 的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 另一个,我不熟悉python所以我打算用Java来举例说明

GlobalGraphOperations.at(graphDatabaseService).getAllNodes()

which is the way the the old deprecated graphDatabaseService.getAllNodes() recommends. 这是旧的不推荐使用的graphDatabaseService.getAllNodes()推荐的方式。

For newer versions of py2neo the accepted version no longer works. 对于较新版本的py2neo,接受的版本不再有效。 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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM