4 Rubyist
Graph Database
why?
Similar things
------------------------------------------------------
*| |*
Students * ----- * Courses 1 ------ * Achievements
Students 1 ---- * UserCourses * ---- 1 Courses
Students 1 ---- * UserAchievements * ---- 1 UserAchievements
what?
In short
- Data stored as nodes and relationships
-
Index-free adjacency
- Examples:
TAO (Facebook),
Neo4j (Open-sourced),
Graphd (Freebase), etc
Neo4j
A graph database
http://www.neo4j.org/Other interesting things
-
REST API for db operations
- Graph query language: Cypher
-
Visualizer
try!
Scenario
Person 1 --- * PersonFriend * --- 1 Person
Create nodes & relationships
Nodes
CREATE (person {name : 'John'})
RETURN person;
Relationship
START a=node(9), b=node(10)
CREATE a-[rel:friend]->b
RETURN rel
Query
Some simple one :)
START n=node(*)
RETURN n
START n=node:nodes(name = "John")
RETURN n
Visualize
Live
why_again?
Expressiveness
Who'are John's friends
SELECT Person.name as PERSON, PersonFriend.friend_id as FRIEND
FROM Person
INNER JOIN PersonFriend
ON Person.id = PersonFriend.person_id
WHERE Person.name = 'John';
Cyper
START person=node:Person(name = 'John')
MATCH person-[:friend]->friend
RETURN friend
Expressiveness
John's friends of friends
SELECT Person.name as PERSON, pf1.friend_id as COMMON, pf2.friend_id as FoF
FROM Person
INNER JOIN PersonFriend pf1 ON Person.id = pf1.person_id
INNER JOIN PersonFriend pf2 ON pf1.friend_id = pf2.person_id
WHERE Person.name = 'John';
Cyper
START person=node:Person(name = 'John')
MATCH person-[:friend]->common_friend-[:friend]->friend
RETURN friend, common_friend
Performance
- Find friends-of-friends in a social network,
to a maximum depth of five - Social network containing 1,000,000 people,
each with approximately 50 friends - Result:
- Neo4j in Action
Jonas Partner, Aleksa Vukotic, and Nicki Watt
Jonas Partner, Aleksa Vukotic, and Nicki Watt
Other considerations
-
schema-less
-
open-sourced (free ^_^) & commercially supported, too
-
good community
more!
For Ruby / Ruby on Rails
Drivers
-
neo4j.rb (JRuby)
https://github.com/andreasronge/neo4j
-
neography (Ruby wrapper for neo4j REST API)
https://github.com/maxdemarzi/neography/
-
neoid (extends ActiveRecords)
https://github.com/elado/neoid
-
Other language drivers, from Java to Go
http://www.neo4j.org/develop/drivers
Learning resources
Books
-
Graph database by Ian Robinson, Jim Webber & Emil Eifrem
-
Neo4j in Action by Jonas Partner, Aleksa Vukotic, and Nicki Watt
Places
Github
Q_and_A?
Adapted from:
-
Graph database (Robinson, Webber & Eifrem)
-
Petabyte Scale Data at Facebook
Neo4j for Rubyist
By Tong Huu Khiem (Mark)
Neo4j for Rubyist
- 1,092