【问题标题】:py2neo: How to iterate over all relationships taking the start node and the end node?py2neo:如何遍历所有开始节点和结束节点的关系?
【发布时间】:2016-02-01 23:23:27
【问题描述】:

如何遍历取开始节点和结束节点的图上的所有关系?我试过了:

import sys
import time
import json
from py2neo import Graph, Node, authenticate, Relationship
graph =Graph()
cypher = graph.cypher

def handle_row(row):
    a,b = row
    ... do some stuff with a,b

cypher.execute("match (a)-[]->(b) return a, b", row_handler=handle_row)

但我得到了错误:

`typeError: <function handle_row at ...> is not JSON serializable`

【问题讨论】:

    标签: graph neo4j py2neo


    【解决方案1】:

    cypher.execute() 函数不将结果处理程序作为参数。它将查询参数作为字典或关键字参数。然后将这些参数作为 JSON 发送到 neo4j。您的 handle_row 函数不是 JSON 可序列化的,因此是 TypeError

    要对所有节点执行操作,请尝试以下操作:

    result = graph.cypher.execute('MATCH (a)-[]->(b) RETURN a, b')
    for row in result:
        print(row)
        print(row[0])
        print(row[2])
    

    在此处查看示例:http://py2neo.org/2.0/cypher.html#api-cypher

    【讨论】:

    • 刚刚在我也可以使用的文档中看到:for r in graph.cypher.stream("MATCH (a)-[]-&gt;(b) RETURN id(a), id(b)"): print (r[0],r[1])。你知道效率方面的区别吗?
    • 我会说流更适合大型结果集。
    猜你喜欢
    • 1970-01-01
    • 2012-06-20
    • 1970-01-01
    • 1970-01-01
    • 2015-01-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多