Neo4j Tutorial

Download Report

Transcript Neo4j Tutorial

Neoj4 in 1 Hour
By Zhendong Wang ([email protected])
 Installation
 A short introduction for graph database and Neo4j
 Neo4j in action
Agenda




Setup your environment
Neo4j graph model
Using Cypher
A sample project
 Where to go next?
Installation
 Download it here: https://neo4j.com/download/communityedition/
Short Introduction
Graph database and Neo4j
 Graph database is a NoSQL DB design for
modeling entity with COMPLEX
RELATIONSHIPs between them by
Graph
Database
using graph model
Example (social network)
If you want to find your friends’ friends
which is not your friends, how to do it
in a relational DB?
Why graph
database?
My friend A
Me
My friend B
My friend’s
friend not my
firend
We have the follwing table:
Person (id, name)
Person_Friend_Relationship(id, friend_id)
The query for our question is:
A Relational
DBVersion
SELECT other_person.name
FROM Person as me
JOIN Person_Friend_Relationship as friends
on me.id = friends.id
AND me.name = ‘<Your Name Here>’
JOIN Person_Friend_Relatoionship as friends_of_friends
on friends.firend_id= friends_of_friends.id
AND friends_of_friends.friend_id NOT IN
(
# select your friend’s id … here
)
JOIN Person as other_person
on friend_of_friend.friend_id = other_person.id
Match
(me:Person{name:“<Your Name Here>”})-[:FRIEND]->
(my_friend:Person)-[:FRIEND]>(other:Person)
Return other.name
A Neo4j
Version
 A graph database implemented with JAVA
 Shipped with a SQL like query language Cypher
What’s Neo4j?
 Provide high level abstraction and API for graph model (we won’t
talk about it today)
 Relatively good performance (comparing with table join)
 Open source !!!
Setup your
environment
 Create a database
 Click the “Choose…”
Setup your
environment
 Create a database
 Create and select a folder for it (here we create ‘my_db’ folder)
 Click ‘Open’
Setup your
environment
 Open database
 Click the ‘Start’ and wait for ‘Status’ bar into green
Setup your
environment
 Open your Neo4j management browser
Setup your
environment
 Give username / password for your db
Setup your
environment
Finished!
Hello World!
 create (tony:Person {name:”Tony”}) –[r:LOVE]->
(hanna:Person {name:”Hanna”})
return tony,hanna,r
 match (n)->[r]->(other)
return n,r,other
Hello world
Neo4j graph model
Node
Node
Attributes
Relationship
Label
Relationship
KNOWS
Person
Person
When:
2015-02-02
Name: Tony
Age: 23
Name: Jenny
Age: 25
Label
Attributes
 Relationship can also have attributes
Node
Attributes
Relationship
Label
 Usually, the label on node is capitalized and label on relationship
is all uppercase
 Label Count:
 Node can have MORE THAN 1 label
 Relationship can have ONLY 1 label
 Neo4j is schemaless
Cypher
Neo4j’s SQL
 CRUD operations
What we will
learn





Create node (along with attributes and label)
Create relationship (along with attributes)
Read
Update
Delete
 How to query





Aggregation
Distinct
Sort
Limit/Skip
With (pipeline your query)
 Create plain node:
create (n)
 Create plain node with attributes:
create ( n {name:”tony”, age:25} )
Create Node
 Create node with attribute and label:
create (n :Person {name:”tony”})
 Create node with attribute and MORE THAN 1
label:
create (n :Person:Student {name:”tony”})
 Tips: use ‘return n’ in your query to see what you
have created
 Create relatipnship with Label (You must give a
label):
create (person)-[r:KNOWS]->(other_person)
Create
Relationship
 Create relatipnship with Attributes:
create (person)-[r:KNOWS {at_year:2015}]>(other_person)
 Tips
 You can create as many relationship as you
want between 2 given nodes
 Create Relationship and Node in one time
create (person:Person {name:”hanna”})[r:KNOWS]->(other_person)
Advanced
Create
 Split your query in several sub clause
create
(p1:Person {name:”Tony”}),
(p2:Person {name:”Hanna”}),
(p2)-[r:KNOWS]->(p1)
 Tips
 Split your query into readable sub clauses to
make it more clear
Read (Node)
 Match a single node
match (n) return n
 Match node with Label
match (n:Person) return n
 Match node with attribute
match (n {name:"tony"}) return n
 Match node with filter (WHERE )
match (n)
where n.name = 'tony' and n.age > 10
return n
 Select fields
…
return n.name, n.age
 Delete Node
match (n {name:'tony'})
delete n
Delete
 Delete Relationship
match (n)-[r:KNOWS]-(o)
delete r
 Truncate the DB
MATCH (n)
DETACH DELETE n
 add label:
match (n)
set n:Person
 update attributes:
match (n:Person)
set n.name ="new Tony”
Update
 also you can use filter:
match (n:Person)
where n.name = "hanna”
set n.name ="new Tony”
 Tips
 To update relationship, same idea
CRUD
Finished
 Aggregation is implicit in Neo4j (We DON”T
HAVE GROUP BY clause)
Aggregation
 We put aggregation criteria and aggregation
functions together
match (n:Person)
return n.name, count(1)
 Tips
 Check Neo4j’s document for more aggregation
functions
Distinct
 Distinct:
match (n)
return distinct n.name
 Order by
match (n:Person)
return n.name, n.age
order by n.age
Order by
 Order by desc
match (n:Person)
return n.name, n.age
order by n.age desc
 Tips
 Where will NULL appear in the Sort result
Skip/Limit
 For pagination:
match (n:Person)
return n.name, n.age
order by n.age desc
skip 2 limit 2
 In cypher we don’t have sub query, but we have
pipeline with “Wind” keyword, which pass the
result of one query as input to the next query.
WITH
match (n:Person)
with n.name as name, count(1) as count
where count > 1
return name, count
A demo project
Using Movie database
 Run “:play movie graph” in your query
 Select the second part of the result
 Click the query and then run it
Let’s create it
 Get all the actors ?
 Get all movies starred by ‘Tom Cruise’ ? How
many?
Use case
 Find the actor appear in the same movie with
‘Tom Cruise’ more than 1 time?
 check if there is any movie which starred by
more than 3 big movie star ?
 A big movie star means the actor acts in at least 5
movies
build a simple
RESTful API
(python+neo4j
-pythondriver+flask)
 Install python 3+
 pip install neo4j-driver flask
 Let’s do it by hand




Create DB connection
Run query with parameters
Extract the result
Build RESTful API
Where to go next?
 The best resources should be the Neo4j’s official document:
https://neo4j.com/docs/
 Cypher syntax
 Client for different programming language
 Performance tuning
Resources
 Books: http://www.allitebooks.com/?s=neo4j
 Dive into the code: https://github.com/neo4j