Handling Highly Connected Data with Neo4j Graph Databases

Handling Highly Connected Data with Neo4j Graph Databases

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

August 28, 2014
Tweet

Transcript

  1. @michellesanver Graph Databases with PHP and Neo4j Laracon

  2. About: Michelle Sanver @michellesanver

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

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

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

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

  7. I do this for fun. @michellesanver (Michelle)-[:LOVES]->(Neo4j)

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

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

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

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

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

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

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

    visualise connected data. Michelle Graphs likes (Michelle)-[:LOVES]->(Neo4j)
  15. Graphs vs. Relational databases @michellesanver Relational question: Average age of

    everyone in this list? Graph question: Who do I indirectly know in this room? (Michelle)-[:LOVES]->(Neo4j)
  16. Graphs vs. Relational databases @michellesanver Relational databases have tables Recipes

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

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

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

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

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

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

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

    model. (Michelle)-[:LOVES]->(Neo4j)
  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
  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
  26. Graphs vs. Relational databases @michellesanver (Michelle)-[:LOVES]->(Neo4j) Having a flexible schema

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

  42. Graphs and Neo4j @michellesanver Common named graphs Local McLaughlin graph

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

  79. Thank you Laracon @michellesanver (Michelle)-[:LOVES]->(Neo4j)

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