A corner where you can try Neo4J for a long time.
If you master the recently opened query language Cypher for graph databases, you will be able to create and search graph structures. So let's get started with Cypher.
I tried using py2neo, which I put in the last time, as a companion. Installation via pip:
sudo pip install py2neo $ python -V Python 2.7.10
A subtle non-DRY example (and it doesn't work) that was on the web was copied and corrected for the time being so that batch processing was possible:
test.py
from py2neo import Graph
graph = Graph("http://neo4j:yourpassword@:7474/db/data/")
statement = """
MERGE (person:Person {name:{person_name}}) ON CREATE SET
person.age = {person_age},
person.sex = {person_sex}
MERGE (pet:Pet {name:{pet_name}}) ON CREATE SET
pet.type = {pet_type}
MERGE (person)-[:owns]->(pet)
RETURN person
"""
tx = graph.cypher.begin()
persons = [
{'name': 'Homer', 'age': 32, 'sex': 'Male', 'pet_name': 'Buller', 'pet_type': 'Dog'},
{'name': 'Lucy', 'age': 12, 'sex': 'Male', 'pet_name': 'Buller', 'pet_type': 'Dog'},
{'name': 'Lucy', 'age': 12, 'sex': 'Male', 'pet_name': 'Betty', 'pet_type': 'Cat'}
]
for person in persons:
print person
tx.append(statement, {
'person_name': person['name'],
'person_age': person['age'],
'person_sex': person['sex'],
'pet_name': person['pet_name'],
'pet_type': person['pet_type']
})
tx.process()
tx.commit()
You can see the flow by extracting only the transaction part:
tx = graph.cypher.begin() tx.append(statement, { ... }) tx.process() tx.commit()
Also, if you master the cypher syntax MERGE ~ ON CREATE in the statement, it looks pretty good.
See the example of py2neo committing JSON below, which could be written differently. http://jexp.github.io/blog/html/load_json.html
Next, try playing with cypher from the command line (environment is Mac).
$ neo4j-shell
Unable to find any JVMs matching version "1.7".
Welcome to the Neo4j Shell! Enter 'help' for a list of commands
NOTE: Remote Neo4j graph database service 'shell' at port 1337
neo4j-sh (?)$ MATCH (p)-[:owns]-(pet) WHERE p.name = "Lucy" RETURN p, pet;
+------------------------------------------------------------------------------+
| p | pet |
+------------------------------------------------------------------------------+
| Node[11]{name:"Lucy",age:12,sex:"Male"} | Node[12]{name:"Betty",type:"Cat"} |
| Node[11]{name:"Lucy",age:12,sex:"Male"} | Node[10]{name:"Buller",type:"Dog"} |
+------------------------------------------------------------------------------+
2 rows
46 ms
Something was complained when neo4j-shell was launched, but I was able to make a query.
Run the same query in your browser:
MATCH (p)-[:owns]-(pet) WHERE p.name = "Lucy" RETURN p, pet;
Press the Graph button on the right and you'll see the graph screen you expected: http://localhost:7474/browser/
Below on the original site: http://neo4j.com/docs/stable/cypher-refcard/
I can't find a good introduction to Japanese on the web.
Hurry up and look forward to "study notes" with updates from time to time! http://qiita.com/Keech/items/6bd5b667e935a20be018
Let's do this at our own pace ^^;
Recommended Posts