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. 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 likely to be hungover from last nights open bar? (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 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
  8. 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
  9. Graphs and Neo4j @michellesanver (Node) { Properties } As many

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

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

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

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

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

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

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

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

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