יום רביעי, 15 במאי 2013

Neo4j python interface py2neo

py2neo Is a great interface to Neo4j in python.
py2neo web site can be found here.

After installing and running the Neo4jserver download the latest py2neo extract it and install it using the :python setup.py install command.

The interface is very simple and trivial the following example:connect to the neo4j server , creates a simple db and perform a Cypher query.

Creating a simple graph DB
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),
)

Execute a simple Cypher query
def handle_row(row):
    node = row[0]
    print (node)

cypher.execute(graph_db, "START z=node(*) RETURN z", row_handler=handle_row)

The handle_row callback is called for every node it found.

The result:
(0)
(1 {"name":"Bruce Willis"})
(2 {"name":"John McClane"})
(3 {"name":"Alan Rickman"})
(4 {"name":"Hans Gruber"})
(5 {"name":"Nakatomi Plaza"})

אין תגובות:

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