Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Neo4j Fundamentals

994 views

Published on

Slides for Neo4j Fundamentals Class

Published in: Technology
  • Be the first to comment

Neo4j Fundamentals

  1. 1. Neo4j Fundamentals
  2. 2. Today’s first graph
 (:Person { name:”Max De Marzi"} )-[:TEACHES]-> (:Database { name:"Neo4j"} )
  3. 3. Logistics • WIFI-SSID: IvyRoomGuest Password: no password • Install Neo4j 3.2.1 from USB or from neo4j.com/download • Coffee break • Working together today • Pair on the exercises and help each other! • Ask relevant questions immediately: raise your hand if you have problems • Keep larger questions to the breaks / end • Use the scratch-pad google doc: bit.ly/_fundamentals • Join neo4j.com/slack and go to the #training-fundamentals channel • Please fill out the feedback form: bit.ly/neo-survey (gives a discount code)
  4. 4. What are we going to do today? • Module I Intro to graphs and Neo4j • Module III First steps in Cypher • Module III Hands-on Cypher • Resources
  5. 5. Module I: Intro to Graphs and Neo4j • Why graphs? • Intro to the property graph model • Querying the graph
  6. 6. Module I: Intro to Graphs and Neo4j • Why graphs? • Intro to the property graph model • Querying the graph
  7. 7. The world is a graph – everything is connected • people, places, events • companies, markets • countries, history, politics • sciences, art, teaching • technology, networks, machines, 
 applications, users • software, code, dependencies, 
 architecture, deployments • criminals, fraudsters and their behavior
  8. 8. Use Cases Internal Applications Master Data Management Network and 
 IT Operations Fraud Detection Customer-Facing Applications Real-Time Recommendations Graph-Based Search Identity and 
 Access Management
  9. 9. Whiteboard friendliness
  10. 10. Whiteboard friendliness
  11. 11. Tom Hanks Hugo Weaving Cloud Atlas The Matrix Lana Wachowski ACTED_IN ACTED_IN ACTED_IN DIRECTED DIRECTED Whiteboard friendliness
  12. 12. name: Tom Hanks born: 1956 title: Cloud Atlas released: 2012 title: The Matrix released: 1999 name: Lana Wachowski born: 1965 ACTED_IN roles: Zachry ACTED_IN roles: Bill Smoke DIRECTED DIRECTED ACTED_IN roles: Agent Smith name: Hugo Weaving born: 1960 Person Movie Movie Person Director ActorPerson Actor Whiteboard friendliness
  13. 13. Module I: Intro to Graphs and Neo4j • Why graphs? • Intro to the property graph model • Querying the graph
  14. 14. Neo4j Fundamentals • Nodes • Relationships • Properties • Labels
  15. 15. CAR Property Graph Model Components Nodes • Represent the objects in the graph • Can be labeled PERSON PERSON
  16. 16. CAR DRIVES Property Graph Model Components Nodes •Represent the objects in the graph •Can be labeled Relationships • Relate nodes by type and direction LOVES LOVES LIVES WITH OW NS PERSON PERSON
  17. 17. CAR DRIVES name: “Dan” born: May 29, 1970 twitter: “@dan” name: “Ann” born: Dec 5, 1975 since: 
 Jan 10, 2011 brand: “Volvo” model: “V70” Property Graph Model Components Nodes •Represent the objects in the graph •Can be labeled Relationships •Relate nodes by type and direction Properties • Name-value pairs that can go on nodes and relationships. LOVES LOVES LIVES WITH OW NS PERSON PERSON
  18. 18. Exercise: Graphs are all around us Can you identify nodes and relationships in the room?
  19. 19. Summary of the graph building blocks • Nodes - Entities and complex value types • Relationships - Connect entities and structure domain • Properties - Entity attributes, relationship qualities, metadata • Labels - Group nodes by role
  20. 20. Module I: Intro to Graphs and Neo4j • Why graphs? • Intro to the property graph model • Querying the graph
  21. 21. A pattern matching query language made for graphs 21 Cypher
  22. 22. 22 • Declarative • Expressive • Pattern Matching Cypher
  23. 23. Pattern in our Graph Model LOVES Dan Ann NODE NODE Relationship
  24. 24. Cypher: Express Graph Patterns (:Person { name:"Dan"} ) -[:LOVES]-> (:Person { name:"Ann"} ) LOVES Dan Ann LABEL PROPERTY NODE NODE LABEL PROPERTY Relationship
  25. 25. Cypher: CREATE Graph Patterns CREATE (:Person { name:"Dan"} ) -[:LOVES]-> (:Person { name:"Ann"} ) LOVES Dan Ann LABEL PROPERTY NODE NODE LABEL PROPERTY Relationship
  26. 26. Cypher: MATCH Graph Patterns MATCH (:Person { name:"Dan"} ) -[:LOVES]-> ( whom ) RETURN whom LOVES Dan ? VARIABLE NODE NODE LABEL PROPERTY Relationship
  27. 27. Module II: First steps in Cypher • ASCII-Art • Demo • Constraints • Let's graph our group!
  28. 28. Module II: First steps in Cypher We will learn how to: • represent nodes and relationships in Cypher • create and find nodes • add properties • use constraints and create nodes uniquely • create relationships • find patterns • delete data
  29. 29. Ascii Art: Nodes () or (n) • Surrounded with parentheses • Use an alias to refer to our node later (n:Label1) • Specify a Label, starts with a colon : • Group nodes by roles or types, think of labels as tags (n:Label {prop: 'value'}) • Nodes can have properties
  30. 30. Ascii Art: Relationships --> or -[r:TYPE]-> • Wrapped with hyphens & square brackets • Like labels, a relationship type starts with a colon : < > Specify the direction of the relationship -[:KNOWS {since: 2010}]-> • Relationships can have properties too!
  31. 31. Ascii Art: Relationships (n:Label {prop:'value'})-[:TYPE]->(m:Label) • mini graph examples that we want to search for • use variables for later use (p1:Person {name:'Alice'})-[:KNOWS]->(p2:Person {name:'Bob')
  32. 32. Module II: First steps in Cypher • ASCII-Art • Demo • Constraints • Let's graph our group!
  33. 33. CREATE a person node within the graph to represent yourself!
 
 
 
 
 Label of :Person Property :name should be 'My Name' Let's try it - CREATE myself!
  34. 34. CREATE statement: CREATE (p:Person {name: 'My Name'})
 RETURN p; Properties are stored as key-value pairs {key:'value'} Welcome to the graph
  35. 35. CREATE (p:Person {name: 'My Name'})
 RETURN p;
 
 Then run the following query MATCH (p:Person {name: 'My Name'})
 RETURN p
 
 OOOPS! I created two of me, that's not good. Let’s try running the create statement again
  36. 36. Module II: First steps in Cypher • ASCII-Art • Demo • Constraints • Let's graph our group!
  37. 37. Unique Constraints We create unique constraints to: • ensure uniqueness • allow fast lookup of nodes which match label-property pairs.
  38. 38. We create unique constraints to: • ensure uniqueness • allow fast lookup of nodes which match label-property pairs. 
 CREATE CONSTRAINT ON (label:Label)
 ASSERT label.property IS UNIQUE Unique Constraints
  39. 39. Let’s create a constraint on :Person(name):
 CREATE CONSTRAINT ON (p:Person) ASSERT p.name IS UNIQUE; Creating a constraint
  40. 40. Let’s create a constraint on :Person(name):
 CREATE CONSTRAINT ON (p:Person) ASSERT p.name IS UNIQUE; 
 Unable to create CONSTRAINT ON ( person:Person ) ASSERT person.name IS UNIQUE:
 Multiple nodes with label `Person` have property `name` = 'My Name':
 node(292)
 node(293)
 
 Oops! Our data violates the constraint we’re trying to create. Creating a constraint
  41. 41. Creating a constraint We’ll first get rid of all the nodes we’ve created: MATCH (n) DETACH DELETE n; 
 And create the constraint again:
 
 CREATE CONSTRAINT ON (p:Person) ASSERT p.name IS UNIQUE; 

  42. 42. Module II: First steps in Cypher • ASCII-Art • Demo • Constraints • Let's graph our group!
  43. 43. Let's do it together • Let’s create a graph of our group together! • Open your browser to http://l.neo4j.org/trainer • And type along with me. • Don't be afraid, this is just a sandbox in a playground. 

  44. 44. CREATE statement:
 CREATE (p:Person {name: 'Your Name'})
 RETURN p
 
 Let's find ourselves: CREATE -> MATCH MATCH (p:Person {name: 'Your Name'}) RETURN p; Your turn: CREATE a node for yourself
  45. 45. SET and update properties Find yourself and SET your city as a property. MATCH (p:Person {name: 'Your Name'}) SET p.city = 'Your City' RETURN p;
  46. 46. Let's create ourselves twice. What happens now? CREATE (p:Person {name: 'Your Name'}) MERGEto the rescue: • MATCHes the whole pattern to find it • If not found CREATE the pattern • Relies on constraint for strong guarantees MERGE (p:Person {name: 'Your Name'}) RETURN p MERGE - Find Or Create
  47. 47. Most challenging task of the course: 
 Ask your right neighbor for their name.
 Find the pair of you:
 MATCH (p1:Person {name: 'Your Name'})
 MATCH (p2:Person {name: 'Your Neighbor'})
 RETURN p1, p2; Or in one statement: MATCH (p1:Person {name: 'Your Name'}),
 (p2:Person {name: 'Your Neighbor'}) RETURN p1, p2; Creating relationships
  48. 48. Create a relationship between you and your neighbor:
 MATCH (p1:Person {name: 'Your Name'}) MATCH (p2:Person {name: 'Your Neighbor'}) CREATE (p1)-[:KNOWS]->(p2); CREATEing relationships
  49. 49. To avoid duplicate relationships, use MERGE MERGE (p1:Person {name: 'Your Name'})
 MERGE (p2:Person {name: 'Your Neighbor'})
 MERGE (p1)-[:KNOWS]->(p2); Relationship is created uniquely by • type • direction (can be left off) • properties MERGEing relationships
  50. 50. Let's clean up and delete ourselves MATCH (p:Person {name: 'Your Name'})
 DELETE p; Oops! We can’t delete a node that still has relationships attached. We need to delete those as well. Cleaning up
  51. 51. Let's clean up and delete ourselves MATCH (p:Person {name: 'Your Name'})
 DELETE p; Oops! We can’t delete a node that still has relationships attached. We need to delete those as well. 
 MATCH (p:Person {name: 'Your Name'}) DETACH DELETE p; Cleaning up
  52. 52. Open these in your browser. They’ll be useful for the rest of the session: ● Cypher Reference Card
 http://neo4j.com/docs/cypher-refcard/ ● Neo4j Developer Pages 
 http://neo4j.com/developer/cypher ● Neo4j Documentation 
 http://neo4j.com/docs Useful resources for learning Cypher
  53. 53. Module III: Hands-on with Cypher • Setup • Cypher basics • Aggregates • Constraints and indexes • Write queries • Style and modeling conventions • Modeling exercise: Movie genres
  54. 54. We will learn how to • represent nodes and relationships in Cypher • create and find nodes • add properties • use constraints and create nodes uniquely • create relationships • find patterns • delete data
  55. 55. Module III: Hands-on with Cypher • Setup • Cypher basics • Aggregates • Constraints and indexes • Write queries • Style and modeling conventions • Modeling exercise: Movie genres
  56. 56. 1. Start the server. 2. It should be running on: http://localhost:7474 3. Log-in with default credentials user: neo4j password: neo4j 4. Choose a new password We’re good to go! Make sure you’ve got Neo4j running
  57. 57. :play movies Type the following command into the query editor:
 
 :play movies
  58. 58. :play movies Type the following command into the query editor:
 
 :play movies
  59. 59. • Change node colors • Change which node property is displayed • Run queries with CMD (CTRL) + Enter • Insert new line with SHIFT + Enter • Expand the query bar with ESC • Double-click a node and see what happens! • Use :clear to clear past results • CMD (CTRL) + Up Arrow to scroll through past queries Neo4j Browser 101
  60. 60. Module III: Hands-on with Cypher • Setup • Cypher basics • Aggregates • Constraints and indexes • Write queries • Style and modeling conventions • Modeling exercise: Movie genres
  61. 61. Cypher is just ASCII Art
  62. 62. Nodes Nodes are drawn with parentheses.
 ()
  63. 63. Relationships Relationships are drawn with square brackets.
 []
  64. 64. Patterns Patterns are drawn by connecting nodes and relationships with hyphens, optionally specifying a direction with > and < signs.
 
 ()-[]-() ()-[]->() ()<-[]-()
  65. 65. The components of a Cypher query MATCH (m:Movie)
 RETURN m
 
 MATCH and RETURN are Cypher keywords
 m is a variable
 :Movie is a node label
  66. 66. The components of a Cypher query MATCH (p:Person)-[r:ACTED_IN]->(m:Movie)
 RETURN p, r, m
 
 MATCH and RETURN are Cypher keywords
 p,r, and m are variables
 :Movie is a node label
 :ACTED_IN is a relationship type
  67. 67. The components of a Cypher query MATCH path = (:Person)-[:ACTED_IN]->(:Movie)
 RETURN path
 
 MATCH and RETURN are Cypher keywords
 path is a variable
 :Movie is a node label
 :ACTED_IN is a relationship type
  68. 68. MATCH (m:Movie)
 RETURN m Graph versus Tabular results
  69. 69. MATCH (m:Movie)
 RETURN m.title, m.released
 
 Properties are accessed with {variable}.{property_key} Graph versus Tabular results
  70. 70. Case sensitive
 
 Node labels Relationship types Property keys Case insensitive
 
 Cypher keywords
 Case sensitivity
  71. 71. Case sensitive
 
 :Person :ACTED_IN name Case insensitive
 
 MaTcH return 
 Case sensitivity
  72. 72. Cypher: The Basics Type the following command into the query editor:
 
 :play http://guides.neo4j.com/fundamentals/cypher_the_basics.html
  73. 73. Follow the guides in your browser until you see...
  74. 74. Module III: Hands-on with Cypher • Setup • Cypher basics • Aggregates • Constraints and indexes • Write queries • Style and modeling conventions • Modeling exercise: Movie genres
  75. 75. Aggregates Aggregate queries in Cypher are a little bit different than in SQL as we don’t need to specify a grouping key.
  76. 76. Aggregates We implicitly group by any non aggregate fields in the RETURN statement.
 // implicitly groups by p.name
 MATCH (p:Person)-[:ACTED_IN]->(m:Movie)
 RETURN p.name, count(*) AS movies
  77. 77. Other aggregate functions There are other aggregate functions as well. 
 
 You can see the full list on the Cypher refcard:
 neo4j.com/docs/cypher-refcard/current/
  78. 78. Continue with the guide Continue with the guide in your browser
  79. 79. Module III: Hands-on with Cypher • Setup • Cypher basics • Aggregates • Constraints and indexes • Write queries • Style and modeling conventions • Modeling exercise: Movie genres
  80. 80. :schema Type the following command into the query editor:
 
 :schema
  81. 81. There are three types of unique constraints: • Unique node property constraint Unique Constraints
  82. 82. There are three types of unique constraints: • Unique node property constraint 
 CREATE CONSTRAINT ON (label:Label)
 ASSERT label.property IS UNIQUE Unique Constraints
  83. 83. There are three types of unique constraints: • Unique node property constraint • Node property existence constraint Unique Constraints
  84. 84. There are three types of unique constraints: • Unique node property constraint • Node property existence constraint CREATE CONSTRAINT ON (label:Label)
 ASSERT EXISTS(label.name) Unique Constraints
  85. 85. There are three types of unique constraints: • Unique node property constraint • Node property existence constraint • Relationship property existence constraint Unique Constraints
  86. 86. There are three types of unique constraints: • Unique node property constraint • Node property existence constraint • Relationship property existence constraint CREATE CONSTRAINT ON ()-[rel:REL_TYPE]->()
 ASSERT EXISTS(rel.name) Unique Constraints
  87. 87. Indexes We create indexes to: • allow fast lookup of nodes which match label-property pairs.
  88. 88. Indexes We create indexes to: • allow fast lookup of nodes which match label-property pairs. 
 CREATE INDEX ON :Label(property)
  89. 89. What are these fast lookups? The following predicates use indexes: • Equality • STARTS WITH • CONTAINS • ENDS WITH • Range searches • (Non-) existence checks
  90. 90. How are indexes used in Neo4j? Indexes are only used to find the starting points for queries. Use index scans to look up rows in tables and join them with rows from other tables Use indexes to find the starting points for a query. Relational Graph
  91. 91. Cypher: The Basics Type the following command into the query editor:
 
 :play http://guides.neo4j.com/fundamentals/cypher_constraints.html
  92. 92. Module III: Hands-on with Cypher • Setup • Cypher basics • Aggregates • Constraints and indexes • Write queries • Style and modeling conventions • Modeling exercise: Movie genres
  93. 93. The CREATE Clause CREATE (m:Movie {title:'Mystic River', released:2003}) RETURN m
  94. 94. The SET Clause MATCH (m:Movie {title: 'Mystic River'}) SET m.tagline = 'We bury our sins here, Dave. We wash them clean.' RETURN m
  95. 95. The CREATE Clause MATCH (m:Movie {title: 'Mystic River'}) MATCH (p:Person {name: 'Kevin Bacon'}) CREATE (p)-[r:ACTED_IN {roles: ['Sean']}]->(m) RETURN p, r, m
  96. 96. The MERGE Clause MERGE (p:Person {name: 'Tom Hanks'}) RETURN p
  97. 97. The MERGE Clause MERGE (p:Person {name: 'Tom Hanks', oscar: true}) RETURN p
  98. 98. The MERGE Clause MERGE (p:Person {name: 'Tom Hanks', oscar: true}) RETURN p
 
 There is not a :Person node with name:'Tom Hanks' and oscar:true in the graph, but there is a :Person node with name:'Tom Hanks'. 
 
 What do you think will happen here?
  99. 99. The MERGE Clause MERGE (p:Person {name: 'Tom Hanks'}) SET p.oscar = true RETURN p
  100. 100. The MERGE Clause MERGE (p:Person {name: 'Tom Hanks'})-[:ACTED_IN] ->(m:Movie {title: 'The Terminal'}) RETURN p, m
  101. 101. The MERGE Clause MERGE (p:Person {name: 'Tom Hanks'})-[:ACTED_IN] ->(m:Movie {title: 'The Terminal'}) RETURN p, m 
 There is not a :Movie node with title:"The Terminal" in the graph, but there is a :Person node with name:"Tom Hanks". 
 
 What do you think will happen here?
  102. 102. MERGE (p:Person {name: 'Tom Hanks'}) MERGE (m:Movie {title: 'The Terminal'}) MERGE (p)-[r:ACTED_IN]->(m) RETURN p, r, m The MERGE Clause
  103. 103. MERGE (p:Person {name: 'Your Name'}) ON CREATE SET p.created = timestamp(), p.updated = 0 ON MATCH SET p.updated = p.updated + 1 RETURN p.created, p.updated; ON CREATE and ON MATCH
  104. 104. Module III: Hands-on with Cypher • Setup • Cypher basics • Aggregates • Constraints and indexes • Write queries • Style and modeling conventions • Modeling exercise: Movie genres
  105. 105. Clauses Cypher clauses should be written in ALL CAPS and on their own line.
 
 MATCH ...
 WHERE ...
 RETURN ...
  106. 106. Functions Functions should be written in lowerCamelCase.
 
 MATCH path = allShortestPaths() ...
 WHERE size(nodes(path)) ... 
 RETURN ...
  107. 107. Keywords Keywords should be written in ALL CAPS but don’t need to go on their own line.
 
 MATCH ...
 RETURN collect(DISTINCT n)
  108. 108. Labels Labels are written in UpperCamelCase.
 
 CREATE(n:Person)
 CREATE(n:GraphDatabase)
 CREATE(n:VeryDescriptiveLabel)
  109. 109. Relationship Types Relationships are written in UPPERCASE_WITH_UNDERSCORES (to separate words).
 
 CREATE(n)-[:LOVES]->(m)
 CREATE(n)-[:REALLY_LOVES]->(m)
 CREATE(n)-[:IS_IN_LOVE_WITH]->(m)
  110. 110. Properties are written in lowerCamelCase.
 
 CREATE(n)
 SET n.name = 'Dave'
 
 CREATE(n)
 SET n.firstName = 'Dave'
 
 CREATE(n)
 SET n.fullName = 'Dave Gordon' Properties
  111. 111. Module III: Hands-on with Cypher • Setup • Cypher basics • Aggregates • Constraints and indexes • Write queries • Style and modeling conventions • Modeling exercise: Movie genres
  112. 112. The age old question: should we model them as properties or as nodes?
 
 Adding movie genres vs
  113. 113. MATCH (m:Movie {title: 'The Matrix'})
 SET m.genre = ['Action', 'Sci-Fi']
 RETURN m Genres as properties
  114. 114. MATCH (m:Movie {title: 'Mystic River'})
 SET m.genre = ['Action', 'Mystery']
 RETURN m 
 
 Genres as properties
  115. 115. Accessing a movie’s genres is quick and easy. MATCH (m:Movie {title:"The Matrix"})
 RETURN m.genre; The good side of properties
  116. 116. Finding movies that share genres is painful and we have a disconnected pattern in the MATCH clause - a sure sign you have a modeling issue. MATCH (m1:Movie), (m2:Movie)
 WHERE any(x IN m1.genre WHERE x IN m2.genre)
 AND m1 <> m2
 RETURN m1, m2; The bad side of properties
  117. 117. MATCH (m:Movie {title:"The Matrix"})
 MERGE (action:Genre {name:"Action"})
 MERGE (scifi:Genre {name:"Sci-Fi"})
 MERGE (m)-[:IN_GENRE]->(action)
 MERGE (m)-[:IN_GENRE]->(scifi) 
 
 Genres as nodes
  118. 118. MATCH (m:Movie {title:"Mystic River"})
 MERGE (action:Genre {name:"Action"})
 MERGE (mystery:Genre {name:"Mystery"})
 MERGE (m)-[:IN_GENRE]->(action)
 MERGE (m)-[:IN_GENRE]->(mystery) Genres as nodes
  119. 119. Finding movies that share genres is a natural graph pattern. MATCH (m1:Movie)-[:IN_GENRE]->(g:Genre),
 (m2:Movie)-[:IN_GENRE]->(g)
 RETURN m1, m2, g The good side of nodes
  120. 120. Accessing the genres of movies requires a bit more typing. MATCH (m:Movie {title:"The Matrix"}),
 (m)-[:IN_GENRE]->(g:Genre)
 RETURN g.name; The (not too) bad side of nodes
  121. 121. Add summary slide!
  122. 122. Thank you for your time!
 Please help us improve: bit.ly/neo-survey . A filled-out survey gives you discount on your next Neo4j class!

×