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.

Michelle Sanver

July 17, 2014
Tweet

More Decks by Michelle Sanver

Other Decks in Programming

Transcript

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

    Euler’s Bridges 1736 (Michelle)-[:LOVES]->(Neo4j)
  2. Why graphs? @michellesanver A graph is an easy way to

    visualise connected data. Michelle Graphs likes (Michelle)-[:LOVES]->(Neo4j)
  3. 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)
  4. Graphs vs. Relational databases @michellesanver Which causes a join table…

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

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

    group Spaghetti Italian group (Michelle)-[:LOVES]->(Neo4j)
  7. Graphs and Neo4j @michellesanver Node Properties As many as you

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

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

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

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

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

    your DB. (Don’t) (Michelle)-[:LOVES]->(Neo4j)
  13. Neo4j in PHP @michellesanver As we saw There’s a REST

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

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

    Everyman\Neo4\Client('localhost', 7474); ! print_r($client->getServerInfo());
  16. 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();
  17. 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
  18. 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
  19. 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)