2016-02-01 1019 views
1

如何遍歷圖上的所有關係,包括開始節點和結束節點?我想:py2neo:如何遍歷所有的關係採取開始節點和結束節點?

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` 

回答

2

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

+1

就看到了,我也可以用在文檔中:'對於R在graph.cypher.stream( 「MATCH(a) - [] - >(b)RETURN ID(a),id(b)」): print(r [0],r [1])'。你會知道效率方面的差異嗎? – montefuscolo

+2

我想說流對於大型結果集更好。 –