$30 off During Our Annual Pro Sale. View Details »

Graph Databases with PHP and Neo4j

Graph Databases with PHP and Neo4j

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

July 17, 2014
Tweet

More Decks by Michelle Sanver

Other Decks in Programming

Transcript

  1. @michellesanver
    Graph Databases with PHP and Neo4j
    AmsterdamPHP

    View Slide

  2. About: Michelle Sanver
    @michellesanver

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

  12. Graphs vs. Relational databases
    @michellesanver
    Relational question:
    Average age of everyone in this list?
    Graph question:
    Who is most likely to hug you here?
    (Michelle)-[:LOVES]->(Neo4j)

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

  60. 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

  61. 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

  62. 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

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

  75. 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

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

    View Slide

  77. Thank you
    AmsterdamPHP
    @michellesanver
    (Michelle)-[:LOVES]->(Neo4j)

    View Slide

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

    View Slide