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

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.

01da6d807a29ad6d49801c0157518148?s=128

Michelle Sanver

July 17, 2014
Tweet

Transcript

  1. @michellesanver Graph Databases with PHP and Neo4j AmsterdamPHP

  2. About: Michelle Sanver @michellesanver

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

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

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

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

  7. Why graphs? @michellesanver Graph theory has been studied since Leonard

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

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

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

  11. Why graphs? @michellesanver A graph is an easy way to

    visualise connected data. Michelle Graphs likes (Michelle)-[:LOVES]->(Neo4j)
  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)
  13. Graphs vs. Relational databases @michellesanver Relational databases have tables Recipes

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

  29. Graphs and Neo4j @michellesanver Node Properties As many as you

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

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

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

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

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

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

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

  37. Graphs and Neo4j @michellesanver Common named graphs Diamond Butterfly Bull

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

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

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

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

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

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

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

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

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

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

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

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

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

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

  52. Neo4j Browser @michellesanver Makes it easy to visualise and query

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

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

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

  56. Neo4j in PHP @michellesanver As we saw There’s a REST

    interface! (Michelle)-[:LOVES]->(Neo4j)
  57. Neo4j in PHP @michellesanver Neo4jPHP PHP Wrapper for the Neo4j

    graph database REST interface (Michelle)-[:LOVES]->(Neo4j) github.com/jadell/neo4jphp
  58. Neo4j in PHP @michellesanver (Michelle)-[:LOVES]->(Neo4j) require('vendor/autoload.php'); ! $client = new

    Everyman\Neo4\Client('localhost', 7474); ! print_r($client->getServerInfo());
  59. Neo4j in PHP @michellesanver (Michelle)-[:LOVES]->(Neo4j) Use $client to do anything!

  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();
  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
  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
  63. OmNomHub: Beyond Pizza @michellesanver (Michelle)-[:LOVES]->(Neo4j)

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

  65. OmNomHub: Beyond Pizza @michellesanver Connecting recipes “These have similar ingredients

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

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

  68. OmNomHub: Beyond Pizza @michellesanver Being creepy “Don’t like meat huh?”

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

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

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

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

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

  74. Wrapup @michellesanver Open Source! ! ! ! ! ! https://github.com/Omnomhub

    (Michelle)-[:LOVES]->(Neo4j)
  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)
  76. Questions? @michellesanver (Michelle)-[:LOVES]->(Neo4j)

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

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