Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Handling Highly Connected Data with the Neo4j Graph Database

Handling Highly Connected Data with the Neo4j Graph Database

Traditional relational databases — ironically — are not that good at the complex relationships some modern applications need. Multiple joins and complex sub-queries can gradually take a toll on performance.

Graph Databases, on the other hand, are all about relationships. In this talk we will look at using the popular Neo4j graph database with PHP to build efficient relational data for OmNomHub: not your average recipe site.

Michelle Sanver

October 05, 2014
Tweet

More Decks by Michelle Sanver

Other Decks in Programming

Transcript

  1. @michellesanver
    Graph Databases with PHP and Neo4j
    #PHPNW14

    View Slide

  2. About: Michelle Sanver
    @michellesanver

    View Slide

  3. About: Michelle Sanver
    @michellesanver
    President
    (Michelle)-[:LOVES]->(Neo4j)

    View Slide

  4. About: Michelle Sanver
    @michellesanver
    Code Addict
    (Michelle)-[:LOVES]->(Neo4j)

    View Slide

  5. About: Michelle Sanver
    @michellesanver
    Zürich, Switzerland
    (Michelle)-[:LOVES]->(Neo4j)

    View Slide

  6. About: Michelle Sanver
    @michellesanver
    (Michelle)-[:LOVES]->(Neo4j)
    Michelle
    is_from
    is_from lives_in
    lived_in

    View Slide

  7. Setting expectations.
    @michellesanver
    (Michelle)-[:LOVES]->(Neo4j)

    View Slide

  8. Why graphs?
    @michellesanver
    (Michelle)-[:LOVES]->(Neo4j)

    View Slide

  9. Why graphs?
    @michellesanver
    Graph theory has been studied since Leonard Euler’s
    Bridges 1736
    (Michelle)-[:LOVES]->(Neo4j)

    View Slide

  10. Why graphs?
    @michellesanver
    Graphs are really just connected data… They are
    everywhere
    (Michelle)-[:LOVES]->(Neo4j)

    View Slide

  11. Why graphs?
    @michellesanver
    (Michelle)-[:LOVES]->(Neo4j)

    View Slide

  12. Why graphs?
    @michellesanver
    It’s modern.
    (Michelle)-[:LOVES]->(Neo4j)

    View Slide

  13. Why graphs?
    @michellesanver
    Facebook open graph
    (Michelle)-[:LOVES]->(Neo4j)

    View Slide

  14. Why graphs?
    @michellesanver
    A graph is an easy way to visualise connected data.
    Michelle Graphs
    likes
    (Michelle)-[:LOVES]->(Neo4j)

    View Slide

  15. Graphs vs. Relational databases
    @michellesanver
    Relational question:
    Average age of everyone in this list?
    Graph question:
    Who is likely to be hungover from last nights open bar?
    (Michelle)-[:LOVES]->(Neo4j)

    View Slide

  16. Graphs vs. Relational databases
    @michellesanver
    Relational databases have tables
    Recipes
    (Michelle)-[:LOVES]->(Neo4j)

    View Slide

  17. Graphs vs. Relational databases
    @michellesanver
    Tables have relationships
    Recipes Groups
    (Michelle)-[:LOVES]->(Neo4j)

    View Slide

  18. Graphs vs. Relational databases
    @michellesanver
    Which causes a join table…
    Recipes Groups
    RecipeToGroup
    (Michelle)-[:LOVES]->(Neo4j)

    View Slide

  19. Graphs vs. Relational databases
    @michellesanver
    You query via the table
    Recipes Groups
    RecipeToGroup
    (Michelle)-[:LOVES]->(Neo4j)

    View Slide

  20. Graphs vs. Relational databases
    @michellesanver
    Imagine having *actual* relations
    Pasta group Spaghetti Italian group
    (Michelle)-[:LOVES]->(Neo4j)

    View Slide

  21. Graphs vs. Relational databases
    @michellesanver
    Recipes Groups
    RecipeToGroup
    Pasta group Spaghetti Italian group
    (Michelle)-[:LOVES]->(Neo4j)

    View Slide

  22. Graphs vs. Relational databases
    @michellesanver
    Would you still pick relational?
    (Michelle)-[:LOVES]->(Neo4j)

    View Slide

  23. Graphs vs. Relational databases
    @michellesanver
    A big query.
    The EAV model.
    (Michelle)-[:LOVES]->(Neo4j)

    View Slide

  24. Graphs vs. Relational databases
    @michellesanver
    (Michelle)-[:LOVES]->(Neo4j)
    SET @entityid = '3';
    SELECT ea.attribute_id,
    ea.attribute_code, eav.value AS 'value',
    'varchar' AS 'type'
    FROM catalog_category_entity e
    JOIN catalog_category_entity_varchar
    eav
    ON e.entity_id = eav.entity_id
    JOIN eav_attribute ea
    ON eav.attribute_id = ea.attribute_id
    WHERE e.entity_id = @entityid
    UNION
    SELECT ea.attribute_id,
    ea.attribute_code, eav.value
    AS 'value', 'int' AS 'type'
    FROM catalog_category_entity
    e
    JOIN
    catalog_category_entity_int
    eav
    ON e.entity_id = eav.entity_id
    JOIN eav_attribute ea
    ON eav.attribute_id =
    ea.attribute_id
    WHERE e.entity_id = @entityid
    UNION

    View Slide

  25. Graphs vs. Relational databases
    @michellesanver
    (Michelle)-[:LOVES]->(Neo4j)
    SELECT ea.attribute_id,
    ea.attribute_code,
    eav.value AS 'value',
    'decimal' AS 'type'
    FROM
    catalog_category_entity e
    JOIN
    catalog_category_entity_
    decimal eav
    ON e.entity_id =
    eav.entity_id
    JOIN eav_attribute ea
    ON eav.attribute_id =
    ea.attribute_id
    WHERE e.entity_id =
    @entityid
    UNION
    SELECT ea.attribute_id, ea.attribute_code,
    eav.value AS 'value', 'datetime' AS 'type'
    FROM catalog_category_entity e
    JOIN catalog_category_entity_datetime eav
    ON e.entity_id = eav.entity_id
    JOIN eav_attribute ea
    ON eav.attribute_id = ea.attribute_id
    WHERE e.entity_id = @entityid
    UNION
    SELECT ea.attribute_id, ea.attribute_code,
    eav.value AS 'value', 'text' AS 'type'
    FROM catalog_category_entity e
    JOIN catalog_category_entity_text eav
    ON e.entity_id = eav.entity_id
    JOIN eav_attribute ea
    ON eav.attribute_id = ea.attribute_id
    WHERE e.entity_id = @entityid

    View Slide

  26. Graphs vs. Relational databases
    @michellesanver
    (Michelle)-[:LOVES]->(Neo4j)
    Having a flexible schema
    database costs a lot.

    View Slide

  27. Graphs and Neo4j
    @michellesanver
    (Michelle)-[:LOVES]->(Neo4j)

    View Slide

  28. Neo4j
    @michellesanver
    (Michelle)-[:LOVES]->(Neo4j)

    View Slide

  29. neo4j.org
    @michellesanver
    (Michelle)-[:LOVES]->(Neo4j)

    View Slide

  30. neotechnology.com
    @michellesanver
    (Michelle)-[:LOVES]->(Neo4j)

    View Slide

  31. Java Based
    @michellesanver
    (Michelle)-[:LOVES]->(Neo4j)

    View Slide

  32. Open Source!
    github.com/neo4j
    @michellesanver
    (Michelle)-[:LOVES]->(Neo4j)

    View Slide

  33. Meetups everywhere
    neo4j.meetup.com
    @michellesanver
    (Michelle)-[:LOVES]->(Neo4j)

    View Slide

  34. Graphs and Neo4j
    @michellesanver
    Graphs have…
    (Michelle)-[:LOVES]->(Neo4j)

    View Slide

  35. Graphs and Neo4j
    @michellesanver
    ( Nodes )
    (Michelle)-[:LOVES]->(Neo4j)

    View Slide

  36. Graphs and Neo4j
    @michellesanver
    (Node) { Properties }
    As many as you want
    Type:
    Person!
    !
    Nick: !
    geekie
    Type: !
    Person!
    !
    Nick: !
    WyriHaximus
    (Michelle)-[:LOVES]->(Neo4j)

    View Slide

  37. Graphs and Neo4j
    @michellesanver
    (Node) [ Relationships ]
    As many as you want
    Type:
    Person!
    !
    Nick: !
    geekie
    Type: !
    Person!
    !
    Nick: !
    WyriHaximus
    (Michelle)-[:LOVES]->(Neo4j)

    View Slide

  38. Graphs and Neo4j
    @michellesanver
    [Relationship { properties } ]
    As many as you want
    Nick: !
    geekie
    Nick: !
    WyriHaximus
    knows
    (Michelle)-[:LOVES]->(Neo4j)

    View Slide

  39. Graphs and Neo4j
    @michellesanver
    Labels
    Type:
    Person!
    !
    Nick: !
    geekie
    (Michelle)-[:LOVES]->(Neo4j)

    View Slide

  40. Graphs and Neo4j
    @michellesanver
    Indexes for easy lookup
    Type:
    Person!
    !
    Nick: !
    geekie
    (Michelle)-[:LOVES]->(Neo4j)

    View Slide

  41. Graphs and Neo4j
    @michellesanver
    Common named graphs
    (Michelle)-[:LOVES]->(Neo4j)

    View Slide

  42. Graphs and Neo4j
    @michellesanver
    Common named graphs
    Local McLaughlin graph
    (Michelle)-[:LOVES]->(Neo4j)

    View Slide

  43. Graphs and Neo4j
    @michellesanver
    Common named graphs
    Diamond Butterfly Bull
    Franklin Tutte Wagner
    (Michelle)-[:LOVES]->(Neo4j)

    View Slide

  44. Graphs and Neo4j
    @michellesanver
    You can make art out of your DB.
    (Don’t)
    (Michelle)-[:LOVES]->(Neo4j)

    View Slide

  45. OmNomHub
    https://github.com/Omnomhub
    @michellesanver
    (Michelle)-[:LOVES]->(Neo4j)

    View Slide

  46. OmNomHub
    @michellesanver
    Like GitHub but for recipes!
    (Michelle)-[:LOVES]->(Neo4j)

    View Slide

  47. OmNomHub
    @michellesanver
    Fork a recipe
    (Michelle)-[:LOVES]->(Neo4j)

    View Slide

  48. OmNomHub
    @michellesanver
    See all forks
    (Michelle)-[:LOVES]->(Neo4j)

    View Slide

  49. OmNomHub
    @michellesanver
    Join groups
    (Michelle)-[:LOVES]->(Neo4j)

    View Slide

  50. OmNomHub
    @michellesanver
    Search similar recipes
    (Michelle)-[:LOVES]->(Neo4j)

    View Slide

  51. OmNomHub
    @michellesanver
    Have fun! ;-)
    (Michelle)-[:LOVES]->(Neo4j)

    View Slide

  52. Neo4j Cypher and Browser
    @michellesanver
    (Michelle)-[:LOVES]->(Neo4j)

    View Slide

  53. Hang in there
    (Michelle)-[:LOVES]->(Kittens) @michellesanver

    View Slide

  54. Neo4j Cypher
    @michellesanver
    Cypher: SQL for graph databases.
    (Michelle)-[:LOVES]->(Neo4j)

    View Slide

  55. Neo4j Cypher
    @michellesanver
    )
    (Michelle)-[:LOVES]->(Neo4j)
    Parentheses means nodes
    (Or hugs)
    (

    View Slide

  56. Neo4j Cypher
    @michellesanver
    }
    (Michelle)-[:LOVES]->(Neo4j)
    Curly braces means
    properties
    {

    View Slide

  57. Neo4j Cypher
    @michellesanver
    ]
    (Michelle)-[:LOVES]->(Neo4j)
    Square brackets means
    relationships
    [

    View Slide

  58. Neo4j Browser
    @michellesanver
    Makes it easy to visualise and
    query the data.
    (Michelle)-[:LOVES]->(Neo4j)

    View Slide

  59. Neo4j Browser
    @michellesanver
    Let’s learn cypher in the browser!
    (Michelle)-[:LOVES]->(Neo4j)

    View Slide

  60. DEMO
    @michellesanver
    (Michelle)-[:LOVES]->(Neo4j)

    View Slide

  61. Neo4j in PHP
    @michellesanver
    (Michelle)-[:LOVES]->(Neo4j)

    View Slide

  62. Neo4j in PHP
    @michellesanver
    As we saw
    There’s a REST interface!
    (Michelle)-[:LOVES]->(Neo4j)

    View Slide

  63. Neo4j in PHP
    @michellesanver
    Neo4jPHP
    PHP Wrapper for the Neo4j graph database REST
    interface
    (Michelle)-[:LOVES]->(Neo4j)
    github.com/jadell/neo4jphp

    View Slide

  64. Neo4j in PHP
    @michellesanver
    (Michelle)-[:LOVES]->(Neo4j)
    require('vendor/autoload.php');
    !
    $client =
    new Everyman\Neo4\Client('localhost', 7474);
    !
    print_r($client->getServerInfo());

    View Slide

  65. Neo4j in PHP
    @michellesanver
    (Michelle)-[:LOVES]->(Neo4j)
    Use $client to do anything!

    View Slide

  66. Neo4j in PHP
    @michellesanver
    (Michelle)-[:LOVES]->(Neo4j)
    Creating a node with the client
    $recipe = $client->makeNode();
    $recipe->setProperty('title', ‘Spaghetti Bolognese')
    ->setProperty('Description', ‘Italian words')
    ->setProperty('Ingredients', ‘A lot')
    ->save();
    $recipe->getId();

    View Slide

  67. Neo4j in PHP
    @michellesanver
    (Michelle)-[:LOVES]->(Neo4j)
    You get the idea, it’s well documented!
    Use $client to do anything, even Cypher queries.
    https://github.com/jadell/neo4jphp/wiki

    View Slide

  68. Neo4j in PHP
    @michellesanver
    (Michelle)-[:LOVES]->(Neo4j)
    In OmNomHub we’re using Symfony2
    There’s a bundle for that.
    https://github.com/klaussilveira/neo4j-client-bundle

    View Slide

  69. OmNomHub: Beyond Pizza
    @michellesanver
    (Michelle)-[:LOVES]->(Neo4j)

    View Slide

  70. OmNomHub: Beyond Pizza
    @michellesanver
    Connecting users
    “You both like”
    (Michelle)-[:LOVES]->(Neo4j)

    View Slide

  71. OmNomHub: Beyond Pizza
    @michellesanver
    Connecting recipes
    “These have similar ingredients and user base”
    (Michelle)-[:LOVES]->(Neo4j)

    View Slide

  72. OmNomHub: Beyond Pizza
    @michellesanver
    Being smart
    “You might like”
    (Michelle)-[:LOVES]->(Neo4j)

    View Slide

  73. OmNomHub: Beyond Pizza
    @michellesanver
    Being smart
    Smart recipe collections!
    (Michelle)-[:LOVES]->(Neo4j)

    View Slide

  74. OmNomHub: Beyond Pizza
    @michellesanver
    Being creepy
    “Don’t like meat huh?”
    (Michelle)-[:LOVES]->(Neo4j)

    View Slide

  75. Wrapup
    @michellesanver
    (Michelle)-[:LOVES]->(Neo4j)

    View Slide

  76. Wrapup
    @michellesanver
    Graphs are everywhere
    (Michelle)-[:LOVES]->(Neo4j)

    View Slide

  77. Wrapup
    @michellesanver
    They make it easy to connect data
    (Michelle)-[:LOVES]->(Neo4j)

    View Slide

  78. Wrapup
    @michellesanver
    Easier to be creepy <3
    (Michelle)-[:LOVES]->(Neo4j)

    View Slide

  79. Wrapup
    @michellesanver
    OmNomHub will be EPIC :D
    (Michelle)-[:LOVES]->(Neo4j)

    View Slide

  80. Wrapup
    @michellesanver
    Open Source!
    !
    !
    !
    !
    !
    https://github.com/Omnomhub
    (Michelle)-[:LOVES]->(Neo4j)

    View Slide

  81. Resources
    @michellesanver
    docs.neo4j.org
    Graph Databases - Ian Robinson, 

    neo technology gives you the e-book version for free.
    neo4j.org/learn
    (Michelle)-[:LOVES]->(Neo4j)

    View Slide

  82. Questions?
    @michellesanver
    (Michelle)-[:LOVES]->(Neo4j)

    View Slide

  83. Thank you
    #phpnw14
    @michellesanver
    (Michelle)-[:LOVES]->(Neo4j)

    View Slide

  84. Pleaaaaase….
    @michellesanver
    (Michelle)-[:LOVES]->(Neo4j)

    View Slide

  85. Give me feedback
    !
    https://joind.in/11807
    @michellesanver
    (Michelle)-[:LOVES]->(Neo4j)

    View Slide

  86. How would you use it?
    @michellesanver
    (Michelle)-[:LOVES]->(Neo4j)

    View Slide