יום שבת, 18 במאי 2013

cypher cont

The neo4j graph creation program for this post :

from py2neo import neo4j
from py2neo import node, rel
from py2neo import  cypher

graph_db = neo4j.GraphDatabaseService("http://localhost:7474/db/data/")

die_hard = graph_db.create(
        node(name="Bruce Willis"),
        node(name="John McClane"),
        node(name="Alan Rickman"),
        node(name="Hans Gruber"),
        node(name="Nakatomi Plaza"),
        rel(0, "PLAYS", 1),
        rel(2, "PLAYS", 3),
        rel(1, "VISITS", 4),
        rel(3, "STEALS_FROM", 4),
        rel(1, "KILLS", 3),
    )

#the callback function for the Cypher query 
def handle_row(row):
    node = row[0]
    print (node)
    print (type (node))

Simple Cypher queries :
Find the related nodes of the related nodes of a node:

theCypherCode = "START n=node(1) MATCH (n)--(x)--(t) RETURN t"
cypher.execute(graph_db, theCypherCode, row_handler=handle_row)

The results:

(5 {"name":"Nakatomi Plaza"})
<class 'py2neo.neo4j.Node'>
(4 {"name":"Hans Gruber"})
<class 'py2neo.neo4j.Node'>

Note the return type  is  'py2neo.neo4j.Node

Find the related relationship of the related nodes of a node:

theCypherCode = "START n=node(1) MATCH (n)--(x)-[r]->() RETURN r”

cypher.execute(graph_db, theCypherCode, row_handler=handle_row)

The results:

(2)-[:VISITS]->(5)
<class 'py2neo.neo4j.Relationship'>
(2)-[:KILLS]->(4)
<class 'py2neo.neo4j.Relationship'>

Note the return type  is  'py2neo.neo4j.Relationship'

Get the relationsship object of a specific relationship

theCypherCode = "START n=node(1) MATCH (n)--(u)-[r:KILLS]->() RETURN r”

אין תגובות:

הוסף רשומת תגובה