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

Graph Databases + Neo4j

Graph Databases + Neo4j

A primer on graph databases, why you would use one, what they do, and one application for a graph database.

879085df57d4124774c3d30f35614922?s=128

Regina Imhoff

August 07, 2017
Tweet

Transcript

  1. Graph Databases & Neo4j Regina Imhoff @StabbyMcDuck

  2. What is a graph?

  3. Charles Annie Betty Follows Follow s Follow s Follows

  4. Charles Annie Betty Follows Follow s Follow s Follows Node

    (Vertex)
  5. Charles Annie Betty Follows Follow s Follow s Follows Node

    (Vertex) Relationship (Edge)
  6. Graph Databases • CRUD • Create, Read, Update, Delete •

    Non-Relational (NoSQL) • Connected data • Social networks!
  7. Property Graph • Nodes • Contain properties (key-value pairs) •

    One or more labels • Relationships • Named • Directional • Can also contain properties
  8. Graph Storage • Storage • Native graph storage: optimized for

    storing and managing graphs • Non-native: serialize into relational, object-oriented, or other data store
  9. Graph Processing • Native graph processing: leverage index-free adjacency •

    Connected nodes physically point to each other • Relationships are turned into first class entities in data records at store level • Non-native graph processing: place a layer of graph on an existing database storage engine • Can’t do graph traversal as it isn’t stored as a graph
  10. Why Use a Graph Database? • Performance • Increased performance

    when working with connected data • Join-intensive query performance • Relational databases deteriorate • Graph databases remain mostly constant • Queries are localized to a portion of the graph • Even as data set gets bigger!
  11. Why Use a Graph Database? • Flexibility • Graph databases

    are additive • Can add new kinds of relationships, nodes, labels, subgraphs, etc. to existing structure • Won't disturb existing queries! • Less developer time spent on modeling domains
  12. Why Use a Graph Database? • Agility • Schema-free •

    Change the data model as you develop
  13. IRL Scenario • The manufacturing and sales of yarn •

    Applicable to most social and manufacturing! • www.ravelry.com
  14. IRL Scenario

  15. Wool Cards Material User Carding

  16. Wool Cards Material User Carding Owns User Owns User

  17. Carding Material Dyeing User Dyes

  18. Dyeing Spins Material Material Spinning User Carding Material

  19. Material Knits Item User Spinning Patterns Authors Pattern User

  20. Wool Carding User Cards Material Dyeing Material User Dyes Spinni

    ng Material Material User Spins Material Item Material User Knits Pattern Patterns User Authors User Owns
  21. Owns Initiates Exchanged User Item Transaction Transactions

  22. Owned Owns Receives Owns Initiates Exchanged User Item Transaction User

    Transactions
  23. ☠ RUT-ROH! ☠ • Scenario: somebody who purchased a skein

    of yarn has tested positive for anthrax! • We need to find all the people who were involved in the production of this dye lot to test them too
  24. SQL Query // Spinner SELECT users.* FROM users INNER JOIN

    spinnings ON spinnings.spinner_id = users.id INNER JOIN items ON items.material_id = spinnings.id INNER JOIN users as knitters ON knitters.id = items.knitter_id WHERE knitters.name = "Bob" UNION
  25. SQL Query // Dyer SELECT users.* FROM users INNER JOIN

    dyeings ON dyeings.dyer_id = users.id INNER JOIN spinnings ON spinnings.material_id = dyeings.id INNER JOIN items ON items.material_id = spinnings.id INNER JOIN users as knitters ON knitters.id = items.knitter_id WHERE knitters.name = "Bob" UNION
  26. SQL Query // Carder SELECT users.* FROM users INNER JOIN

    cardings ON cardings.carder_id = users.id INNER JOIN dyeings ON dyeings.material_id = cardings.id INNER JOIN spinnings ON spinnings.material_id = dyeings.id INNER JOIN items ON items.material_id = spinnings.id INNER JOIN users as knitters ON knitters.id = items.knitter_id WHERE knitters.name = "Bob"
  27. Cypher Query MATCH (p:Person)-[:OWNS]->(i:Item)-[*]-(q:Person) WHERE p.name = ‘Bob' AND i.name

    = 'socks' RETURN q
  28. Cypher Query MATCH (p:Person)-[:OWNS]->(i:Item)-[*]-(q:Person) WHERE p.name = ‘Bob' AND i.name

    = 'socks' RETURN q Variable
  29. Cypher Query MATCH (p:Person)-[:OWNS]->(i:Item)-[*]-(q:Person) WHERE p.name = ‘Bob' AND i.name

    = 'socks' RETURN q Variable Node
  30. Cypher Query MATCH (p:Person)-[:OWNS]->(i:Item)-[*]-(q:Person) WHERE p.name = ‘Bob' AND i.name

    = 'socks' RETURN q Variable Node Relationship
  31. Cypher Query MATCH (p:Person)-[:OWNS]->(i:Item)-[*]-(q:Person) WHERE p.name = ‘Bob' AND i.name

    = 'socks' RETURN q Variable Node Relationship Variable length path
  32. Cypher Query MATCH (p:Person)-[:OWNS]->(i:Item)-[*]-(q:Person) WHERE p.name = ‘Bob' AND i.name

    = 'socks' RETURN q Variable Node Relationship Variable length path Type
  33. Cypher Query MATCH (p:Person)-[:OWNS]->(i:Item)-[*]-(q:Person) WHERE p.name = ‘Bob' AND i.name

    = 'socks' RETURN q Variable Node Relationship Variable length path Type Directed Relationship Relationship
  34. Cypher Query

  35. Cypher Query

  36. Cypher Query

  37. Neo4j Browser