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]->bRETURN 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 FRIENDFROM PersonINNER JOIN PersonFriendON Person.id = PersonFriend.person_idWHERE Person.name =  'John';

Cyper

START person=node:Person(name = 'John')MATCH person-[:friend]->friendRETURN friend

Expressiveness


John's friends of friends
SELECT Person.name as PERSON, pf1.friend_id as COMMON,  pf2.friend_id as FoFFROM Person
INNER JOIN PersonFriend pf1 ON Person.id = pf1.person_idINNER JOIN PersonFriend pf2 ON pf1.friend_id = pf2.person_idWHERE Person.name =  'John'; 
Cyper
START person=node:Person(name = 'John')MATCH person-[:friend]->common_friend-[:friend]->friendRETURN 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

Other considerations


  • schema-less
  • open-sourced (free ^_^) & commercially supported, too
  • good community



more!

For Ruby / Ruby on Rails


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)