montefuscolo
montefuscolo

Reputation: 269

py2neo: How to iterate over all relationships taking the start node and the end node?

How to iterate over all relationships on a graph taking the start node and the end node?. I Tried:

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)

But I am getting the error:

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

Upvotes: 1

Views: 1271

Answers (1)

Martin Preusse
Martin Preusse

Reputation: 9369

The cypher.execute() function does not take a result handler as argument. It takes query parameters either as a dictionary or as keyword arguments. These parameters are then sent to neo4j as JSON. Your handle_row function is not JSON serializable, hence the TypeError.

To do something with all your nodes try this:

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

See examples here: http://py2neo.org/2.0/cypher.html#api-cypher

Upvotes: 3

Related Questions