Slide 1

Slide 1 text

Graph Databases & Neo4j Regina Imhoff @StabbyMcDuck

Slide 2

Slide 2 text

What is a graph?

Slide 3

Slide 3 text

Charles Annie Betty Follows Follow s Follow s Follows

Slide 4

Slide 4 text

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

Slide 5

Slide 5 text

Charles Annie Betty Follows Follow s Follow s Follows Node (Vertex) Relationship (Edge)

Slide 6

Slide 6 text

Graph Databases • CRUD • Create, Read, Update, Delete • Non-Relational (NoSQL) • Connected data • Social networks!

Slide 7

Slide 7 text

Property Graph • Nodes • Contain properties (key-value pairs) • One or more labels • Relationships • Named • Directional • Can also contain properties

Slide 8

Slide 8 text

Graph Storage • Storage • Native graph storage: optimized for storing and managing graphs • Non-native: serialize into relational, object-oriented, or other data store

Slide 9

Slide 9 text

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

Slide 10

Slide 10 text

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!

Slide 11

Slide 11 text

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

Slide 12

Slide 12 text

Why Use a Graph Database? • Agility • Schema-free • Change the data model as you develop

Slide 13

Slide 13 text

IRL Scenario • The manufacturing and sales of yarn • Applicable to most social and manufacturing! • www.ravelry.com

Slide 14

Slide 14 text

IRL Scenario

Slide 15

Slide 15 text

Wool Cards Material User Carding

Slide 16

Slide 16 text

Wool Cards Material User Carding Owns User Owns User

Slide 17

Slide 17 text

Carding Material Dyeing User Dyes

Slide 18

Slide 18 text

Dyeing Spins Material Material Spinning User Carding Material

Slide 19

Slide 19 text

Material Knits Item User Spinning Patterns Authors Pattern User

Slide 20

Slide 20 text

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

Slide 21

Slide 21 text

Owns Initiates Exchanged User Item Transaction Transactions

Slide 22

Slide 22 text

Owned Owns Receives Owns Initiates Exchanged User Item Transaction User Transactions

Slide 23

Slide 23 text

☠ 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

Slide 24

Slide 24 text

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

Slide 25

Slide 25 text

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

Slide 26

Slide 26 text

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"

Slide 27

Slide 27 text

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

Slide 28

Slide 28 text

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

Slide 29

Slide 29 text

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

Slide 30

Slide 30 text

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

Slide 31

Slide 31 text

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

Slide 32

Slide 32 text

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

Slide 33

Slide 33 text

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

Slide 34

Slide 34 text

Cypher Query

Slide 35

Slide 35 text

Cypher Query

Slide 36

Slide 36 text

Cypher Query

Slide 37

Slide 37 text

Neo4j Browser