understanding graph databases with neo4j and cypher

Click here to load reader

Post on 13-Jul-2015

305 views

Category:

Education

2 download

Embed Size (px)

TRANSCRIPT

  • Understanding Graph Databases with Neo4j and Cypher

    Group Members

    S.S. Niranga MS-14901836

    Nipuna Pannala MS-14902208

    Ruhaim Izmeth MS-14901218

  • Trends in Data

    Data is getting bigger:Every 2 days we create as much information as we did up to 2003 Eric Schmidt, Google

  • The History of Graph Theory

    1736: Leonard Euler writes a paper on the Seven Bridges of Konisberg

    1845: Gustav Kirchoff publishes his electrical circuit laws

    1852: Francis Guthrie poses the Four Color Problem

    1878: Sylvester publishes an article in Nature magazine that describes graphs

    1936: Dnes Knig publishes a textbook on Graph Theory

    1941: Ramsey and Turn define Extremal Graph Theory

    1959: De Bruijn publishes a paper summarizing Enumerative Graph Theory

    1959: Erdos, Renyi and Gilbert define Random Graph Theory

    1969: Heinrich Heesch solves the Four Color problem

    2003: Commercial Graph Database products start appearing on the market

  • What is Graph database?

    A traditional relational database may tell you the average age of everyone in this room..

    ..but a graph database will tell you who is most likely to buy you a beer!

  • What does a Graph database look like?

  • What is a Graph Database?

    A database with an explicit graph structure

    Each node knows its adjacent nodes

    As the number of nodes increases, the cost of a local

    step (or hop) remains the same

    Plus an Index for lookups

  • Compared to Relational Databases

    Optimized for aggregation Optimized for connections

  • Complexity Vs Size

  • What to Choose?

    http://db-engines.com/en/ranking/graph+dbms

  • What is Neo4j? Neo4j is an open-source graph database, implemented in Java.

    Neo4j version 1.0 was released in February, 2010.

    Neo4j version 2.0 was released in December, 2013

    Neo4j was developed by Neo Technology, Inc.

    Neo Technology board of directors consists of Rod Johnson, (founder of the Spring Framework), Magnus Christerson (Vice President of Intentional Software Corp), Nikolaj Nyholm (CEO of Polar Rose), Sami Ahvenniemi (Partner at Conor Venture Partners) and Johan Svensson (CTO of Neo Technology).

  • Entities in Graph DBs (Neo4j)

    Nodes

    Relationships

    Properties

    Labels

    Paths

    Traversal

    Schema (index and constraints)

  • Neo4j Properties

    Ex.

  • Ex.

    Neo4j Labels

  • Ex.

    Neo4j Nodes

  • Neo4j Relationships

    Ex.

  • Neo4j PathsEx.

  • Introducing - CypherQuery Language for Neo4j

  • Relational SchemaPerson

    p_namep_id

    Book

    b_titleb_id

    p_type

    Wrote

    b_idp_id

    Purchased

    b_id pur_datep_id

  • Cypher - Few KeywordsGeneral Clauses Return Order by Limit

    Writing Clauses Create Merge Set Delete Remove

    Reading Clauses Match Optional Match Where Aggregation

    See Full list at Cypher RefCardhttp://neo4j.com/docs/stable/cypher-refcard/

    Functions Predicates Scalar functions Collection functions Mathematical functions String functions

  • Cypher Demo

    http://console.neo4j.org/

    or if Neo4j is locally installed

    http://localhost:7474

  • Cypher

    Creating nodes

    CREATE (:Person)

    CREATE (:Person { name:"John Le Carre" })

    CREATE ({ name:"John Le Carre" })

    CREATE (:Person:Author { name:"John Le Carre" })

    CREATE (:Person:Author { name:"Graham Greene" }),

    (:Book { title:"Tinker, Tailor, Soldier, Spy" }),

    (:Book { title:"Our Man in Havana" }),

    (:Person { name:"Ian" }),

    (:Person { name:"Alan" })

  • Cypher

    Modifying nodes

    MATCH (p:Person { namme:"Alan" })SET p += {name2 : "Alan2"}

    MATCH (p:Person { namme:"Alan" })SET p.name = "Alan"

    MATCH (p:Person { namme:"Alan" })SET p = {name : "Alan"}

    CREATE (:Person { namme:"Alan" })

    MATCH (p:Person { name2:"Alan2" })DELETE p

    MATCH (p:Person { namme:"Alan" })REMOVE p.namme

  • Cypher Relationships

  • Cypher - Creating Relationships

    CREATE (john:Person:Author { name:"John Le Carre" }),(b:Book { title:"Tinker, Tailor, Soldier, Spy" }),(john)-[:WROTE]->(b)

    MATCH (p:Person { name:"Ian" }),(b:Book { title:"Our Man in Havana" })MERGE (p)-[:PURCHASED { date:"09-09-2011" }]->(b)

    MATCH(graham:Person:Author { name:"Graham Greene" }),(b:Book { title:"Our Man in Havana" })MERGE (graham)-[:WROTE]->(b)

    MATCH (t:Book { title:"Tinker, Tailor, Soldier, Spy" }),(i:Person { name:"Ian" }),(a:Person { name:"Alan" })MERGE (i)-[:PURCHASED { date:"03-02-2011" }]->(t)

  • Cypher - Modifying Relationships

    MATCH (graham:Person {name:"Graham Greene"})-[r]->(b:Book {title:"Our Man in Havana" })DELETE r

    MATCH (p:Person { name:"Ian" })-[r]->(b:Book { title:"Our Man in Havana" })SET r.date = "09-09-2012"

    MATCH (graham:Person:Author { name:"Graham Greene" }),(b:Book { title:"Our Man in Havana" })MERGE (graham)-[:WORTE]->(b)

  • Cypher - Querying DBs Find All Books

    SQL

    SELECT * FROM Books

    Cypher Query

    MATCH (b:Book)RETURN b

    Person (p_id, p_name, p_type)Wrote (p_id, b_id)Book (b_id, b_title )Purchased (p_id, b_id, pur_date)

    Cypher Result+-----------------------------------------------+| b |+-----------------------------------------------+| Node[2]{title:"Tinker, Tailor, Soldier, Spy"} || Node[3]{title:"Our Man in Havana"} |+-----------------------------------------------+2 rows2 ms

  • Cypher - Querying DBs Find All Authors

    SQL

    SELECT * FROM Person where p_type=Author

    Cypher Query

    MATCH (a:Author)RETURN a

    Person (p_id, p_name, p_type)Wrote (p_id, b_id)Book (b_id, b_title )Purchased (p_id, b_id, pur_date)

    Cypher Result+-------------------------------+| a |+-------------------------------+| Node[0]{name:"John Le Carre"} || Node[1]{name:"Graham Greene"} |+-------------------------------+2 rows8 ms

  • Cypher - Querying DBs Find All Authors and the Books written by them

    SQL

    SELECT p.p_name, b.b_title FROM Person p, Wrote w, Book b where p.p_type=Author and w.p_id = p.p_id andw.b_id = b.b_id

    Cypher Query

    MATCH (a:Author)-[:WROTE]->(b:Book)RETURN a,b

    Person (p_id, p_name, p_type)Wrote (p_id, b_id)Book (b_id, b_title )Purchased (p_id, b_id, pur_date)

    Cypher Result+-------------------------------------------------------------------------------+| a | b |+-------------------------------------------------------------------------------+| Node[0]{name:"John Le Carre"} | Node[2]{title:"Tinker, Tailor, Soldier, Spy"} || Node[1]{name:"Graham Greene"} | Node[3]{title:"Our Man in Havana"} |+-------------------------------------------------------------------------------+2 rows12 ms

  • Cypher - Querying DBs Find Books written by Graham Greene

    SQL

    SELECT b.b_title FROM Person p, Wrote w, Book b where p.p_type=Author and w.p_id = p.p_id andw.b_id = b.b_id andp.name = Graham Greene

    Cypher Query

    MATCH (a:Author)-[:WROTE]->(b:Book)WHERE a.name = 'Graham Greene'RETURN b

    Person (p_id, p_name, p_type)Wrote (p_id, b_id)Book (b_id, b_title )Purchased (p_id, b_id, pur_date)

    Cypher Result+------------------------------------+| b |+------------------------------------+| Node[3]{title:"Our Man in Havana"} |+------------------------------------+1 row13 ms

  • Cypher - Querying DBs Find names of all persons, the books they purchased and the date the purchase was made

    SQL

    SELECT p.p_name, pur.pur_date, b.b_titleFROM Person p, Book b, Purchased pur WHERE pur.p_id=p.p_id and b.b_id = pur.b_id

    Cypher Query

    MATCH (a)-[r:PURCHASED]->(b)RETURN a,r.date,b

    Person (p_id, p_name, p_type)Wrote (p_id, b_id)Book (b_id, b_title )Purchased (p_id, b_id, pur_date)

    Cypher Result+-------------------------------------------------------------------------------------+| a | r.date | b |+-------------------------------------------------------------------------------------+| Node[4]{name:"Ian"} | "09-09-2011" | Node[3]{title:"Our Man in Havana"} || Node[4]{name:"Ian"} | "03-02-2011" | Node[2]{title:"Tinker, Tailor, Soldier, Spy"} || Node[5]{name:"Alan"} | "05-07-2011" | Node[2]{title:"Tinker, Tailor, Soldier, Spy"} |+-------------------------------------------------------------------------------------+3 rows

  • Cypher - Querying DBs Find how Graham Greene is related to Ian

    SQL

    I wont attempt!!!

    Cypher Query

    MATCH (a:Author)-[r*]-(p:Person { name:'Ian' })WHERE a.name = 'Graham Greene'RETURN a,r,p

    Person (p_id, p_name, p_type)Wrote (p_id, b_id)Book (b_id, b_title )Purchased (p_id, b_id, pur_date)

    Cypher Result+--------------------------------------------------------------------------------------------------------+| a | r | p |+--------------------------------------------------------------------------------------------------------+| Node[1]{name:"Graham Greene"} | [:WROTE[1] {},:PURCHASED[0] {date:"09-09-2011"}] | Node[4]{name:"Ian"} |+--------------------------------------------------------------------------------------------------------+1 row38 ms

  • Support for Graph Algorithms shortestPath allSimplePaths allPaths

View more