Cypher Query Language                                Michael Hunger                                   @neo4jSunday, Februa...
The plan                    • Why graphs?                    • How graphs?                    • Why Cypher?               ...
Why graphs, why now?Sunday, February 24, 13
Why graphs, why now?    •   Big Data is the trend                 9 Y                   P  L 97 N W                       ...
Why graphs, why now?    • Big Data is the trend                                  P  L 97 N W                              ...
T5K Q S @          & ⛽                                                 ;                !2 ?                            & ...
A graph?                          Yes, a graphSunday, February 24, 13
A graph database...                    • Mathematical graphs                          • remember linked lists, trees?     ...
Some well-known graphsSunday, February 24, 13
Some well-known graphs                      diamondSunday, February 24, 13
Some well-known graphs                      diamond   butterflySunday, February 24, 13
Some well-known graphs                      diamond   butterfly   starSunday, February 24, 13
Some well-known graphs                      diamond   butterfly   star   bullSunday, February 24, 13
Some well-known graphs                      diamond    butterfly   star   bull                      franklinSunday, Februar...
Some well-known graphs                      diamond    butterfly    star   bull                      franklin   robertsonSu...
Some well-known graphs                      diamond    butterfly     star    bull                      franklin   robertson...
Some well-known graphs                      diamond    butterfly     star     bull                      franklin   robertso...
Sunday, February 24, 13
Graphs are everywhereSunday, February 24, 13
The property graph modelSunday, February 24, 13
The property graph modelSunday, February 24, 13
The property graph modelSunday, February 24, 13
The property graph model                                 :LOVES                                 :LOVES                    ...
The property graph model            name: “A”            age: 35                    :LOVES            name: “B”           ...
Sunday, February 24, 13
You know relationalSunday, February 24, 13
You know relationalSunday, February 24, 13
You know relational                          fooSunday, February 24, 13
You know relational                          foo   barSunday, February 24, 13
You know relational                          foo   foo_bar   barSunday, February 24, 13
You know relational                          foo   foo_bar   barSunday, February 24, 13
You know relational                          foo   foo_bar   barSunday, February 24, 13
You know relational                          foo   foo_bar   barSunday, February 24, 13
You know relational        now consider relationships...Sunday, February 24, 13
You know relational        now consider relationships...Sunday, February 24, 13
You know relational        now consider relationships...Sunday, February 24, 13
You know relational        now consider relationships...Sunday, February 24, 13
You know relational        now consider relationships...Sunday, February 24, 13
You know relational        now consider relationships...Sunday, February 24, 13
Neo4j is a graph database                    • Open Source                    • High Availability                    • Bac...
Why Cypher?                    • Not only scale of size is hard                    • Scale of complexity is pretty hard as...
Cypher attributesSunday, February 24, 13
Cypher attributes                     #1 DeclarativeSunday, February 24, 13
Cypher attributes                     #1 Declarative                     You tell Cypher what you want, not how to get itS...
Cypher attributesSunday, February 24, 13
Cypher attributes                     #2 ExpressiveSunday, February 24, 13
Cypher attributes                     #2 Expressive                     Optimize syntax for readingSunday, February 24, 13
Cypher attributesSunday, February 24, 13
Cypher attributes                     #3 Pattern MatchingSunday, February 24, 13
Cypher attributes                     #3 Pattern Matching                     Patterns are easy for your brainSunday, Febr...
Cypher attributesSunday, February 24, 13
Cypher attributes                     #4 IdempotentSunday, February 24, 13
Cypher attributes                     #4 Idempotent                     State change should be expressed idempotentlySunda...
2 Databases                           5 QueriesSunday, February 24, 13
Sunday, February 24, 13
Sunday, February 24, 13
Q1                          Give me all persons older                               than 30 yearsSunday, February 24, 13
Q1                      START n=node:Person("name:*")                      WHERE n.age>30                      RETURN n   ...
Q1                      START n=node:Person("name:*")                      WHERE n.age>30                      RETURN n   ...
Q1                      START n=node:Person("name:*")                      WHERE n.age>30                      RETURN n   ...
Q1                      START n=node:Person("name:*")                      WHERE n.age>30                      RETURN n   ...
Q2                 Show me all actors, and which                    movies they starred inSunday, February 24, 13
Neo4j is whiteboard friendlySunday, February 24, 13
Neo4j is whiteboard friendly                      (actor)-[:ACTS_IN]->(movie)Sunday, February 24, 13
Neo4j is whiteboard friendly                      (actor)-[:ACTS_IN]->(movie)Sunday, February 24, 13
Q2   START actor=node:Person("name:*")   MATCH (actor)-[:ACTS_IN]->(movie)   RETURN actor.name, movie.title               ...
Q2   START actor=node:Person("name:*")   MATCH (actor)-[:ACTS_IN]->(movie)   RETURN actor.name, movie.title               ...
Q2   START actor=node:Person("name:*")   MATCH (actor)-[:ACTS_IN]->(movie)   RETURN actor.name, movie.title               ...
Q2   START actor=node:Person("name:*")   MATCH (actor)-[:ACTS_IN]->(movie)   RETURN actor.name, movie.title               ...
Q2   START actor=node:Person("name:*")   MATCH (actor)-[:ACTS_IN]->(movie)   RETURN actor.name, movie.title               ...
Q2   START actor=node:Person("name:*")   MATCH (actor)-[:ACTS_IN]->(movie)   RETURN actor.name, movie.title               ...
Q3                    Show me all persons that are                     both actors and directorsSunday, February 24, 13
Q3   START person=node:Person("name:*")   WHERE (person)-[:ACTS_IN]->()     AND (person)-[:DIRECTED]->()   RETURN person.n...
Q3   START person=node:Person("name:*")   WHERE (person)-[:ACTS_IN]->()     AND (person)-[:DIRECTED]->()   RETURN person.n...
Q3   START person=node:Person("name:*")   WHERE (person)-[:ACTS_IN]->()     AND (person)-[:DIRECTED]->()   RETURN person.n...
Q3   START person=node:Person("name:*")   WHERE (person)-[:ACTS_IN]->()     AND (person)-[:DIRECTED]->()   RETURN person.n...
Q3   START person=node:Person("name:*")   WHERE (person)-[:ACTS_IN]->()     AND (person)-[:DIRECTED]->()   RETURN person.n...
Q4                           Show me all                          Lucy’s co-actorsSunday, February 24, 13
Q4   START lucy=node:Person(name="Lucy Liu")   MATCH lucy-[:ACTS_IN]->movie,         co_actor-[:ACTS_IN]->movie   RETURN D...
Q4   START lucy=node:Person(name="Lucy Liu")   MATCH lucy-[:ACTS_IN]->movie,         co_actor-[:ACTS_IN]->movie   RETURN D...
Q4   START lucy=node:Person(name="Lucy Liu")   MATCH lucy-[:ACTS_IN]->movie,         co_actor-[:ACTS_IN]->movie   RETURN D...
Q4   START lucy=node:Person(name="Lucy Liu")   MATCH lucy-[:ACTS_IN]->movie,         co_actor-[:ACTS_IN]->movie   RETURN D...
Q4   START lucy=node:Person(name="Lucy Liu")   MATCH lucy-[:ACTS_IN]->movie,         co_actor-[:ACTS_IN]->movie   RETURN D...
Q4         START lucy=node:Person(name="Lucy Liu"         MATCH lucy-[:ACTS_IN]->movie,               co_actor-[:ACTS_IN]-...
Q5                           Show me Lucy’s                          favorite directorsSunday, February 24, 13
Q5    START lucy=node:Person(name="Lucy Liu")    MATCH lucy-[:ACTS_IN]->movie,          director-[:DIRECTED]->movie    RET...
Q5    START lucy=node:Person(name="Lucy Liu")    MATCH lucy-[:ACTS_IN]->movie,          director-[:DIRECTED]->movie    RET...
Q5    START lucy=node:Person(name="Lucy Liu")    MATCH lucy-[:ACTS_IN]->movie,          director-[:DIRECTED]->movie    RET...
Q5    START lucy=node:Person(name="Lucy Liu")    MATCH lucy-[:ACTS_IN]->movie,          director-[:DIRECTED]->movie    RET...
Q5    START lucy=node:Person(name="Lucy Liu")    MATCH lucy-[:ACTS_IN]->movie,          director-[:DIRECTED]->movie    RET...
Q5    START lucy=node:Person(name="Lucy Liu")    MATCH lucy-[:ACTS_IN]->movie,          director-[:DIRECTED]->movie    RET...
Q5    START lucy=node:Person(name="Lucy Liu")    MATCH lucy-[:ACTS_IN]->movie,          director-[:DIRECTED]->movie    RET...
Q5    START lucy=node:Person(name="Lucy Liu")    MATCH lucy-[:ACTS_IN]->movie,          director-[:DIRECTED]->movie    RET...
...fasten your seat belt Dorothy,     cause Kansas is going bye-byeSunday, February 24, 13
Q4’                           Show me all                          Lucy’s co-actorsSunday, February 24, 13
Q4   START     lucy=node:Person(name="Lucy Liu")   MATCH     lucy-[:ACTS_IN]->movie,     co_actor-[:ACTS_IN]->movie   RETU...
Q4’   START     lucy=node:Person(name="Lucy Liu")   MATCH     lucy-[:ACTS_IN*2..2]-co_actor   RETURN     DISTINCT co_actor...
Q4’   START     lucy=node:Person(name="Lucy Liu")   MATCH     lucy-[:ACTS_IN*2..2]-co_actor   RETURN     DISTINCT co_actor...
Q6                          Bacon LucySunday, February 24, 13
START    lucy=node:Person(name="Lucy Liu"),    kevin=node:Person(name="Kevin Bacon")  MATCH p = shortestPath(           lu...
START    lucy=node:Person(name="Lucy Liu"),    kevin=node:Person(name="Kevin Bacon")  MATCH p = shortestPath(           lu...
START    lucy=node:Person(name="Lucy Liu"),    kevin=node:Person(name="Kevin Bacon")  MATCH p = shortestPath(           lu...
Q6  START    lucy=node:Person(name="Lucy Liu"),    kevin=node:Person(name="Kevin Bacon")  MATCH p = shortestPath(         ...
START    lucy=node:Person(name="Lucy Liu"),    kevin=node:Person(name="Kevin Bacon")  MATCH p = shortestPath(           lu...
START    lucy=node:Person(name="Lucy Liu"),    kevin=node:Person(name="Kevin Bacon")  MATCH p = shortestPath(          luc...
START    lucy=node:Person(name="Lucy Liu"),    kevin=node:Person(name="Kevin Bacon")  MATCH p = shortestPath(           lu...
Create Cypher 2                                (the movie)Sunday, February 24, 13
START    lucy = node:Person(name="Lucy Liu"),    david = node:Person(name="David Lynch")  CREATE    andres={name:"Andrés T...
START    lucy = node:Person(name="Lucy Liu"),    david = node:Person(name="David Lynch")  CREATE    andres={name:"Andrés T...
START    lucy = node:Person(name="Lucy Liu"),    david = node:Person(name="David Lynch")  CREATE    andres={name:"Andrés T...
START    lucy = node:Person(name="Lucy Liu"),    david = node:Person(name="David Lynch")  CREATE    andres={name:"Andrés T...
START    lucy = node:Person(name="Lucy Liu"),    david = node:Person(name="David Lynch")  CREATE    andres={name:"Andrés T...
START    lucy = node:Person(name="Lucy Liu"),    david = node:Person(name="David Lynch")  CREATE    andres={name:"Andrés T...
Add genre nodesSunday, February 24, 13
Person                     :DIRECTED   :ACTS_IN                             Movie             Genre                       ...
Movie                                 Movie                title:"Highlander"                    title:"The Matrix"       ...
START    ref=node(0),    movie=node:Movie("title:*")  WHERE    HAS(movie.genre)  CREATE UNIQUE    ref<-[:IS_GENRE]->(genre...
START    ref=node(0),    movie=node:Movie("title:*")  WHERE    HAS(movie.genre)  CREATE UNIQUE    ref<-[:IS_GENRE]->(genre...
START    ref=node(0),    movie=node:Movie("title:*")  WHERE    HAS(movie.genre)  CREATE UNIQUE    ref<-[:IS_GENRE]->(genre...
START    ref=node(0),    movie=node:Movie("title:*")  WHERE    HAS(movie.genre)  CREATE UNIQUE    ref<-[:IS_GENRE]->(genre...
RehashSunday, February 24, 13
START                          designates the start pointsSunday, February 24, 13
MATCH                          describes the patternSunday, February 24, 13
WHERE                          filters the result setSunday, February 24, 13
CREATE                          creates nodes and relationshipsSunday, February 24, 13
CREATE UNIQUE                          creates unique patternsSunday, February 24, 13
RETURN                          creates the result tableSunday, February 24, 13
QUESTION                          ALL THE THINGS!Sunday, February 24, 13
Upcoming SlideShare
Loading in...5
×

0221 Cypher for SQL Professionals

564

Published on

Published in: Technology
0 Comments
1 Like
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total Views
564
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
65
Comments
0
Likes
1
Embeds 0
No embeds

No notes for slide

0221 Cypher for SQL Professionals

  1. 1. Cypher Query Language Michael Hunger @neo4jSunday, February 24, 13
  2. 2. The plan • Why graphs? • How graphs? • Why Cypher? • How Cypher - Cypher & SQL side by sideSunday, February 24, 13
  3. 3. Why graphs, why now?Sunday, February 24, 13
  4. 4. Why graphs, why now? • Big Data is the trend 9 Y P L 97 N W Y P L0 7 N W T5K 0 S @ . ; T5K Q S @ . ; Q B B & ⛽ !2 ? ⛽ !2 ? # O- 3⛵ RC - 3⛵ J X %$ RC 8 X %$ "J8⚾] " ⚽9 ! M DJ ⚾] [ [ ! , ⚽9 E >1 I 7+ O , > E > U A IR 7 + O > 1 WA W# : 34 S ✈✈ R34 S : X T 5U L < K T . I 50 L < H V ☕" I 0 &N . ⛽ K - ⛵ & N A -G☕" ⛽ ⛵ M UA ZG [< % BQ $ ,: 1 MP F ⚾ [< $ ,: 8 ; % BQ ☕4✈ F H Z GV 1⚾P ]8 ; F ☕F ✈ 4 H Z GV ] 2 ? E E⚽ @Y ⚽ @Y 2 ? + CD + CD Sunday, February 24, 13
  5. 5. Why graphs, why now? • Big Data is the trend P L 97 N W Y • NOSQL is the answer T5K 0 S @ . ; Q & ⛽ B O- 3⛵ !2 ? # X %$ J RC 8⚾] " , ⚽9 ! > [ D 1 M UJ I 7 + O >E X # :R34 5 ✈ S H V ☕⛵ G " I 0L < ⛽ U& N A [< Z ☕9 1MP F ⚾ 8 ; $ , 9 Y P L 97 N W P ✈ 47 Z GV ] 2 Y F Y L0 H N W @ P L0 7 N W 0 ⚽ T 5 K Q S @ .T 5 K + @ . ; Y ? T5K Q S @ . ; B & ⛽ B & ⛽; Q S CD B ⛽ !2 ? # O- 3⛵ !2 ?3⛵ O- !2 ? - 3⛵ J RC 8 J R X %$ # 8 C " X %$ J RC X %$ " ⚽ ! > [ !81⚾] " , ⚽9 ! M DJ ⚾] [ [ , M9 J ⚾] > E > U A IR 7 + O U E IR A + O > 1 > D 1 W7 , ⚽9 E > I 7+ O W# : 34 S ✈ ✈ 34 S WA✈ R34 S 5U L # :" TI 5U L < K T : X T I 50 L < H V ☕" I 0 H V[☕⛵ - . ⛽ K - ⛵ &X < K . 0 N &N -. &N A ⛽ AG☕" ⛽ ⛵ M UA ZG [< % BQ $ ,: 1MP ⚾F ZG; % BM P ]8 [ < % B 8✈ <$ ,: 1QF ⚾ $ , ;: ☕4✈ F H Z GV 1⚾P ]8 ; F ☕F ☕F ✈ 4 H Z GV ] 2 4 HE GV ? E ⚽ @Y 2 ? E⚽ CD @Y ⚽ Z C @Y 2 ? + CD Sunday, February 24, 13 + + D
  6. 6. T5K Q S @ & ⛽ ; !2 ? & ⛽ B # O- 3⛵ RC O- 3⛵ !2 ? X %$ J # X %$ M DJ ⚾] " , ⚽9 ! > J RC8 [ P L 978N] " , ⚽9 ! > IR 7 + > E Y WA Y⚾ W 1 [ 9 Y 1 > E 7 : W 4 S O L 97 N T U ✈ M UJ I . +P D T 5 K 0 S @34; T O L0 # N@ I 5U L 0 7S 3 WA . T &P W Q # :RB 5 5 KX VS T B ; 0 K Q S @. ; Why graphs, why now? ✈ " N 5 < K -. & ⛽ H V X ! I 0 L H<ZG☕⛵ &⚾⛽A 8 [ < % B Q "2 & U ? ⛽ ⛽ RO A & N Q K ⛽ . M - 2 OP ] F $ ,: B ; ! # O- 3⛵ J ZG☕⛵ 1⚾P 3⛵ [ <☕4! QR C 3⛵ X %$ M - 8 ; ⚽FB # ?- C F ,: ✈ 1 $ % H Z GV 2 ? E RC 8 ⚾] " ✈ ⚽9 !#> ⚾ ]" 2 %$[ ! 8 D Y 2 $[ ! , ☕4[ F Z GV > D 1@8 XJ ? ⚽E ⚽ H E J I Y , +9 DJC ] ⚾ @" J ?⚽ X % U ✈ R 7 + O U CWA 7 + OM U 1 A 7 + O 9 E > DJ I 1 S + M ] D >E > I , > # : 34 5 R ✈ T 34 S ✈ W3 S R 4 WX V ☕⛵ G " I 0 L X # :" I 50 L X<#K" . I 50 L < K T ⛽ U& N < K - A H[V %⛵ . & H V N :T &N Z ☕ MP F ⚾ $G☕⛽ Q M U , : 9 Y ; LB < A ⛽ ⛵- G9 Y M P [ < % B Q⚾ :☕ U A : - [< % ✈BigH Z GV the trend F 7 1⚾W P8 Z$ F N W 9 ]8 ; F 1 ]8PZ ☕ 0 • ⚽4 Data is @ T 5 ⚽4 H Z GV. 2✈ E ? , N P ] L☕ 7 1 F F ✈ ; $ , + 9 Y D Y C @Y; 2 04 P G. 7 N 2 E K Q S @ B T 5 K? S Z LV @Y W ⚽ H @ Y ? P L0 7 is the answer • NOSQL N@ . - 3⛵X & W O⛽ + C& ? D !2 ⛽ + Q T5 B D 0 C ; @ . K Q? S T5K Q S # J %$[ ! # O- 3⛵ & ⛽ !2R C RC X B ; B8; " ⚾] J %$[ ⚽9 8 > "# O- 3⛵ ! !2 ? & •⛽ Big X M !2in complexity DJ ⚾] WA , ⚽9 J >$[ !R C 1 DJ ? O- 3⛵in size and R C 7 + M U E I I U ✈ R , O > 1 X 8 O > E % WA "# J %$[ ! : 34 S 7 +⚾] 5U L # :KM4 ✈ R 7 + O . E , ⚽9 > 8 ⚾] " , ⚽9 # > X < ✈ R3 UJ I TD S 1 T>DJ I 1U ✈ R 7 + O ZG H > E☕" WA M 0 H V[ :% . #0 L34 5K - V ⛵ I &X G$⛽ - 5U ⛽ N A ⛵ XIQ & " B : N <S #: 34 5 S ☕4 T F Z , ;< M A 1⚾P ]8 ☕ H 1⚾P " 8 [ <& B Q ☕ F ☕⛵ I ;U% :L < $ N ,A 0 I 0 L < F H- GV @ ✈ ? H EV V ⛽ & N ✈ K Z ⚽ . 24F ZG G ] MP E [ Z ☕9 $ , V ☕" 9 M P G ⛽ ⛵ UA [< BQ 9 Y ,:P $ % CD + ⚽ @Y W FY 1⚾ ✈ D H Z GV F 2? ]8 ; < Z L 1⚾ W ]8 ; ☕4 7 F P✈ F 0 Z GV Y L0 7 N W C 0 7 N + P L4 ⚽ @Y 2 ? N ? E . C. T 5 K S C @Y;2 T 5 K Q S @ B T 5 K + S @ B ; ⚽ H + Q @B . ; Q D D & ⛽ !& ⛽3⛵ ? !2 ? ⛽ !2 ? # O- 3⛵ 2R C O- RC - 3⛵ J X %$ RC 8 X %$ # 8 "J X %$ J8⚾] " ⚽9 ! M DJ ⚾] [ ⚽9 ! ⚾ " , ⚽9 ! > [ , M UJ I > [1 I 7+ O , > E > U A IR 7 + O D E ]WA + O > E 1 > 1 ✈ R34 S7 WA✈ R34 S W# : 34 S ✈ : X T 5U L # :" TI 5U L < K T &X < K . 0 . I 50 L < H V ☕" I 0 H V[☕⛵ - & N . ⛽ K - ⛵ N A ⛽ & N A -G☕" ⛽ ⛵ M UA ZG [< % BQ $ ,: 1MP F ⚾ ZG; % BM P ]8 [ < % B 8✈ <$ ,: 1⚾ QF $ , ;: ☕4✈ F H Z GV 1⚾P ]8 ; F ☕F ☕F ✈ 4 H Z GV ] 2 4 HE GV ? E ⚽ @Y 2 ? E⚽ CD @Y ⚽ Z C @Y 2 ? + CD Sunday, February 24, 13 + + D
  7. 7. A graph? Yes, a graphSunday, February 24, 13
  8. 8. A graph database... • Mathematical graphs • remember linked lists, trees? • the general-purpose data structure • “A relational database may tell you the average age of everyone in this room, but a graph database will tell you who is most likely to buy you a beer.”Sunday, February 24, 13
  9. 9. Some well-known graphsSunday, February 24, 13
  10. 10. Some well-known graphs diamondSunday, February 24, 13
  11. 11. Some well-known graphs diamond butterflySunday, February 24, 13
  12. 12. Some well-known graphs diamond butterfly starSunday, February 24, 13
  13. 13. Some well-known graphs diamond butterfly star bullSunday, February 24, 13
  14. 14. Some well-known graphs diamond butterfly star bull franklinSunday, February 24, 13
  15. 15. Some well-known graphs diamond butterfly star bull franklin robertsonSunday, February 24, 13
  16. 16. Some well-known graphs diamond butterfly star bull franklin robertson hortonSunday, February 24, 13
  17. 17. Some well-known graphs diamond butterfly star bull franklin robertson horton neo4jSunday, February 24, 13
  18. 18. Sunday, February 24, 13
  19. 19. Graphs are everywhereSunday, February 24, 13
  20. 20. The property graph modelSunday, February 24, 13
  21. 21. The property graph modelSunday, February 24, 13
  22. 22. The property graph modelSunday, February 24, 13
  23. 23. The property graph model :LOVES :LOVES :KNOWS :KNOWS :KNOWSSunday, February 24, 13
  24. 24. The property graph model name: “A” age: 35 :LOVES name: “B” age: 35 :LOVES since: 2012 :KNOWS :KNOWS since: 2008 name: “D” age: 35 name: “C” :KNOWS age: 35Sunday, February 24, 13
  25. 25. Sunday, February 24, 13
  26. 26. You know relationalSunday, February 24, 13
  27. 27. You know relationalSunday, February 24, 13
  28. 28. You know relational fooSunday, February 24, 13
  29. 29. You know relational foo barSunday, February 24, 13
  30. 30. You know relational foo foo_bar barSunday, February 24, 13
  31. 31. You know relational foo foo_bar barSunday, February 24, 13
  32. 32. You know relational foo foo_bar barSunday, February 24, 13
  33. 33. You know relational foo foo_bar barSunday, February 24, 13
  34. 34. You know relational now consider relationships...Sunday, February 24, 13
  35. 35. You know relational now consider relationships...Sunday, February 24, 13
  36. 36. You know relational now consider relationships...Sunday, February 24, 13
  37. 37. You know relational now consider relationships...Sunday, February 24, 13
  38. 38. You know relational now consider relationships...Sunday, February 24, 13
  39. 39. You know relational now consider relationships...Sunday, February 24, 13
  40. 40. Neo4j is a graph database • Open Source • High Availability • Backed by Neo Technology • In production for 10+ yearsSunday, February 24, 13
  41. 41. Why Cypher? • Not only scale of size is hard • Scale of complexity is pretty hard as wellSunday, February 24, 13
  42. 42. Cypher attributesSunday, February 24, 13
  43. 43. Cypher attributes #1 DeclarativeSunday, February 24, 13
  44. 44. Cypher attributes #1 Declarative You tell Cypher what you want, not how to get itSunday, February 24, 13
  45. 45. Cypher attributesSunday, February 24, 13
  46. 46. Cypher attributes #2 ExpressiveSunday, February 24, 13
  47. 47. Cypher attributes #2 Expressive Optimize syntax for readingSunday, February 24, 13
  48. 48. Cypher attributesSunday, February 24, 13
  49. 49. Cypher attributes #3 Pattern MatchingSunday, February 24, 13
  50. 50. Cypher attributes #3 Pattern Matching Patterns are easy for your brainSunday, February 24, 13
  51. 51. Cypher attributesSunday, February 24, 13
  52. 52. Cypher attributes #4 IdempotentSunday, February 24, 13
  53. 53. Cypher attributes #4 Idempotent State change should be expressed idempotentlySunday, February 24, 13
  54. 54. 2 Databases 5 QueriesSunday, February 24, 13
  55. 55. Sunday, February 24, 13
  56. 56. Sunday, February 24, 13
  57. 57. Q1 Give me all persons older than 30 yearsSunday, February 24, 13
  58. 58. Q1 START n=node:Person("name:*") WHERE n.age>30 RETURN n VS SELECT * FROM Person WHERE age > 30Sunday, February 24, 13
  59. 59. Q1 START n=node:Person("name:*") WHERE n.age>30 RETURN n VS SELECT * FROM Person WHERE age > 30Sunday, February 24, 13
  60. 60. Q1 START n=node:Person("name:*") WHERE n.age>30 RETURN n VS SELECT * FROM Person WHERE age > 30Sunday, February 24, 13
  61. 61. Q1 START n=node:Person("name:*") WHERE n.age>30 RETURN n VS SELECT * FROM Person WHERE age > 30Sunday, February 24, 13
  62. 62. Q2 Show me all actors, and which movies they starred inSunday, February 24, 13
  63. 63. Neo4j is whiteboard friendlySunday, February 24, 13
  64. 64. Neo4j is whiteboard friendly (actor)-[:ACTS_IN]->(movie)Sunday, February 24, 13
  65. 65. Neo4j is whiteboard friendly (actor)-[:ACTS_IN]->(movie)Sunday, February 24, 13
  66. 66. Q2 START actor=node:Person("name:*") MATCH (actor)-[:ACTS_IN]->(movie) RETURN actor.name, movie.title VS SELECT Person.name, Movie.title FROM Person JOIN Actor on Person.person_id = Actor.person_id JOIN Movie on Movie.movie_id = Actor.movie_idSunday, February 24, 13
  67. 67. Q2 START actor=node:Person("name:*") MATCH (actor)-[:ACTS_IN]->(movie) RETURN actor.name, movie.title VS SELECT Person.name, Movie.title FROM Person JOIN Actor on Person.person_id = Actor.person_id JOIN Movie on Movie.movie_id = Actor.movie_idSunday, February 24, 13
  68. 68. Q2 START actor=node:Person("name:*") MATCH (actor)-[:ACTS_IN]->(movie) RETURN actor.name, movie.title VS SELECT Person.name, Movie.title FROM Person JOIN Actor on Person.person_id = Actor.person_id JOIN Movie on Movie.movie_id = Actor.movie_idSunday, February 24, 13
  69. 69. Q2 START actor=node:Person("name:*") MATCH (actor)-[:ACTS_IN]->(movie) RETURN actor.name, movie.title VS SELECT Person.name, Movie.title FROM Person JOIN Actor on Person.person_id = Actor.person_id JOIN Movie on Movie.movie_id = Actor.movie_idSunday, February 24, 13
  70. 70. Q2 START actor=node:Person("name:*") MATCH (actor)-[:ACTS_IN]->(movie) RETURN actor.name, movie.title VS SELECT Person.name, Movie.title FROM Person JOIN Actor on Person.person_id = Actor.person_id JOIN Movie on Movie.movie_id = Actor.movie_idSunday, February 24, 13
  71. 71. Q2 START actor=node:Person("name:*") MATCH (actor)-[:ACTS_IN]->(movie) RETURN actor.name, movie.title VS SELECT Person.name, Movie.title FROM Person JOIN Actor on Person.person_id = Actor.person_id JOIN Movie on Movie.movie_id = Actor.movie_idSunday, February 24, 13
  72. 72. Q3 Show me all persons that are both actors and directorsSunday, February 24, 13
  73. 73. Q3 START person=node:Person("name:*") WHERE (person)-[:ACTS_IN]->() AND (person)-[:DIRECTED]->() RETURN person.name VS SELECT name FROM Person WHERE person_id IN ( SELECT person_id FROM Actor) AND person_id IN ( SELECT person_id FROM Director)Sunday, February 24, 13
  74. 74. Q3 START person=node:Person("name:*") WHERE (person)-[:ACTS_IN]->() AND (person)-[:DIRECTED]->() RETURN person.name VS SELECT name FROM Person WHERE person_id IN ( SELECT person_id FROM Actor) AND person_id IN ( SELECT person_id FROM Director)Sunday, February 24, 13
  75. 75. Q3 START person=node:Person("name:*") WHERE (person)-[:ACTS_IN]->() AND (person)-[:DIRECTED]->() RETURN person.name VS SELECT name FROM Person WHERE person_id IN ( SELECT person_id FROM Actor) AND person_id IN ( SELECT person_id FROM Director)Sunday, February 24, 13
  76. 76. Q3 START person=node:Person("name:*") WHERE (person)-[:ACTS_IN]->() AND (person)-[:DIRECTED]->() RETURN person.name VS SELECT name FROM Person WHERE person_id IN ( SELECT person_id FROM Actor) AND person_id IN ( SELECT person_id FROM Director)Sunday, February 24, 13
  77. 77. Q3 START person=node:Person("name:*") WHERE (person)-[:ACTS_IN]->() AND (person)-[:DIRECTED]->() RETURN person.name VS SELECT name FROM Person WHERE person_id IN ( SELECT person_id FROM Actor) AND person_id IN ( SELECT person_id FROM Director)Sunday, February 24, 13
  78. 78. Q4 Show me all Lucy’s co-actorsSunday, February 24, 13
  79. 79. Q4 START lucy=node:Person(name="Lucy Liu") MATCH lucy-[:ACTS_IN]->movie, co_actor-[:ACTS_IN]->movie RETURN DISTINCT co_actor.name VS SELECT DISTINCT co_actor.name FROM Person lucy JOIN Actor a1 ON lucy.person_id = a1.person_id JOIN Actor a2 ON a1.movie_id = a2.movie_id JOIN Person co_actor ON co_actor.person.id = a2.person_id WHERE lucy.name = "Lucy Liu"Sunday, February 24, 13
  80. 80. Q4 START lucy=node:Person(name="Lucy Liu") MATCH lucy-[:ACTS_IN]->movie, co_actor-[:ACTS_IN]->movie RETURN DISTINCT co_actor.name VS SELECT DISTINCT co_actor.name FROM Person lucy JOIN Actor a1 ON lucy.person_id = a1.person_id JOIN Actor a2 ON a1.movie_id = a2.movie_id JOIN Person co_actor ON co_actor.person.id = a2.person_id WHERE lucy.name = "Lucy Liu"Sunday, February 24, 13
  81. 81. Q4 START lucy=node:Person(name="Lucy Liu") MATCH lucy-[:ACTS_IN]->movie, co_actor-[:ACTS_IN]->movie RETURN DISTINCT co_actor.name VS SELECT DISTINCT co_actor.name FROM Person lucy JOIN Actor a1 ON lucy.person_id = a1.person_id JOIN Actor a2 ON a1.movie_id = a2.movie_id JOIN Person co_actor ON co_actor.person.id = a2.person_id WHERE lucy.name = "Lucy Liu"Sunday, February 24, 13
  82. 82. Q4 START lucy=node:Person(name="Lucy Liu") MATCH lucy-[:ACTS_IN]->movie, co_actor-[:ACTS_IN]->movie RETURN DISTINCT co_actor.name VS SELECT DISTINCT co_actor.name FROM Person lucy JOIN Actor a1 ON lucy.person_id = a1.person_id JOIN Actor a2 ON a1.movie_id = a2.movie_id JOIN Person co_actor ON co_actor.person.id = a2.person_id WHERE lucy.name = "Lucy Liu"Sunday, February 24, 13
  83. 83. Q4 START lucy=node:Person(name="Lucy Liu") MATCH lucy-[:ACTS_IN]->movie, co_actor-[:ACTS_IN]->movie RETURN DISTINCT co_actor.name VS SELECT DISTINCT co_actor.name FROM Person lucy JOIN Actor a1 ON lucy.person_id = a1.person_id JOIN Actor a2 ON a1.movie_id = a2.movie_id JOIN Person co_actor ON co_actor.person.id = a2.person_id WHERE lucy.name = "Lucy Liu"Sunday, February 24, 13
  84. 84. Q4 START lucy=node:Person(name="Lucy Liu" MATCH lucy-[:ACTS_IN]->movie, co_actor-[:ACTS_IN]->movie RETURN DISTINCT co_actor.name; VS SELECT DISTINCT co_actor.name FROM Person lucy JOIN Actor a1 ON lucy.person_id = a1.person_id JOIN Actor a2 ON a1.movie_id = a2.movie_id JOIN Person co_actor ON co_actor.person.id = a2.person_id WHERE lucy.name = "Lucy Liu"Sunday, February 24, 13
  85. 85. Q5 Show me Lucy’s favorite directorsSunday, February 24, 13
  86. 86. Q5 START lucy=node:Person(name="Lucy Liu") MATCH lucy-[:ACTS_IN]->movie, director-[:DIRECTED]->movie RETURN director.name, count(*) ORDER BY count(*) desc VS SELECT dir.name, count(*) FROM Person lucy JOIN Actor on Person.person_id = Actor.person_id JOIN Director on Actor.movie_id = Director.movie_id JOIN Person dir on Director.person.id = dir.person_id WHERE lucy.name = "Lucy Liu" GROUP BY dir.name ORDER BY count(*) DESCSunday, February 24, 13
  87. 87. Q5 START lucy=node:Person(name="Lucy Liu") MATCH lucy-[:ACTS_IN]->movie, director-[:DIRECTED]->movie RETURN director.name, count(*) ORDER BY count(*) desc VS SELECT dir.name, count(*) FROM Person lucy JOIN Actor on Person.person_id = Actor.person_id JOIN Director on Actor.movie_id = Director.movie_id JOIN Person dir on Director.person.id = dir.person_id WHERE lucy.name = "Lucy Liu" GROUP BY dir.name ORDER BY count(*) DESCSunday, February 24, 13
  88. 88. Q5 START lucy=node:Person(name="Lucy Liu") MATCH lucy-[:ACTS_IN]->movie, director-[:DIRECTED]->movie RETURN director.name, count(*) ORDER BY count(*) desc VS SELECT dir.name, count(*) FROM Person lucy JOIN Actor on Person.person_id = Actor.person_id JOIN Director on Actor.movie_id = Director.movie_id JOIN Person dir on Director.person.id = dir.person_id WHERE lucy.name = "Lucy Liu" GROUP BY dir.name ORDER BY count(*) DESCSunday, February 24, 13
  89. 89. Q5 START lucy=node:Person(name="Lucy Liu") MATCH lucy-[:ACTS_IN]->movie, director-[:DIRECTED]->movie RETURN director.name, count(*) ORDER BY count(*) desc VS SELECT dir.name, count(*) FROM Person lucy JOIN Actor on Person.person_id = Actor.person_id JOIN Director on Actor.movie_id = Director.movie_id JOIN Person dir on Director.person.id = dir.person_id WHERE lucy.name = "Lucy Liu" GROUP BY dir.name ORDER BY count(*) DESCSunday, February 24, 13
  90. 90. Q5 START lucy=node:Person(name="Lucy Liu") MATCH lucy-[:ACTS_IN]->movie, director-[:DIRECTED]->movie RETURN director.name, count(*) ORDER BY count(*) desc VS SELECT dir.name, count(*) FROM Person lucy JOIN Actor on Person.person_id = Actor.person_id JOIN Director on Actor.movie_id = Director.movie_id JOIN Person dir on Director.person.id = dir.person_id WHERE lucy.name = "Lucy Liu" GROUP BY dir.name ORDER BY count(*) DESCSunday, February 24, 13
  91. 91. Q5 START lucy=node:Person(name="Lucy Liu") MATCH lucy-[:ACTS_IN]->movie, director-[:DIRECTED]->movie RETURN director.name, count(*) ORDER BY count(*) desc VS SELECT dir.name, count(*) FROM Person lucy JOIN Actor on Person.person_id = Actor.person_id JOIN Director on Actor.movie_id = Director.movie_id JOIN Person dir on Director.person.id = dir.person_id WHERE lucy.name = "Lucy Liu" GROUP BY dir.name ORDER BY count(*) DESCSunday, February 24, 13
  92. 92. Q5 START lucy=node:Person(name="Lucy Liu") MATCH lucy-[:ACTS_IN]->movie, director-[:DIRECTED]->movie RETURN director.name, count(*) ORDER BY count(*) desc VS SELECT dir.name, count(*) FROM Person lucy JOIN Actor on Person.person_id = Actor.person_id JOIN Director on Actor.movie_id = Director.movie_id JOIN Person dir on Director.person.id = dir.person_id WHERE lucy.name = "Lucy Liu" GROUP BY dir.name ORDER BY count(*) DESCSunday, February 24, 13
  93. 93. Q5 START lucy=node:Person(name="Lucy Liu") MATCH lucy-[:ACTS_IN]->movie, director-[:DIRECTED]->movie RETURN director.name, count(*) ORDER BY count(*) desc VS SELECT dir.name, count(*) FROM Person lucy JOIN Actor on Person.person_id = Actor.person_id JOIN Director on Actor.movie_id = Director.movie_id JOIN Person dir on Director.person.id = dir.person_id WHERE lucy.name = "Lucy Liu" GROUP BY dir.name ORDER BY count(*) DESCSunday, February 24, 13
  94. 94. ...fasten your seat belt Dorothy, cause Kansas is going bye-byeSunday, February 24, 13
  95. 95. Q4’ Show me all Lucy’s co-actorsSunday, February 24, 13
  96. 96. Q4 START lucy=node:Person(name="Lucy Liu") MATCH lucy-[:ACTS_IN]->movie, co_actor-[:ACTS_IN]->movie RETURN DISTINCT co_actor.nameSunday, February 24, 13
  97. 97. Q4’ START lucy=node:Person(name="Lucy Liu") MATCH lucy-[:ACTS_IN*2..2]-co_actor RETURN DISTINCT co_actor.nameSunday, February 24, 13
  98. 98. Q4’ START lucy=node:Person(name="Lucy Liu") MATCH lucy-[:ACTS_IN*2..2]-co_actor RETURN DISTINCT co_actor.nameSunday, February 24, 13
  99. 99. Q6 Bacon LucySunday, February 24, 13
  100. 100. START lucy=node:Person(name="Lucy Liu"), kevin=node:Person(name="Kevin Bacon") MATCH p = shortestPath( lucy-[:ACTS_IN*]-kevin ) RETURN EXTRACT(n in NODES(p) : COALESCE(n.name?, n.title?))Sunday, February 24, 13
  101. 101. START lucy=node:Person(name="Lucy Liu"), kevin=node:Person(name="Kevin Bacon") MATCH p = shortestPath( lucy-[:ACTS_IN*]-kevin ) RETURN EXTRACT(n in NODES(p) : COALESCE(n.name?, n.title?))Sunday, February 24, 13
  102. 102. START lucy=node:Person(name="Lucy Liu"), kevin=node:Person(name="Kevin Bacon") MATCH p = shortestPath( lucy-[:ACTS_IN*]-kevin ) RETURN EXTRACT(n in NODES(p) : COALESCE(n.name?, n.title?))Sunday, February 24, 13
  103. 103. Q6 START lucy=node:Person(name="Lucy Liu"), kevin=node:Person(name="Kevin Bacon") MATCH p = shortestPath( lucy-[:ACTS_IN*]-kevin ) RETURN EXTRACT(n in NODES(p) : COALESCE(n.name?, n.title?))Sunday, February 24, 13
  104. 104. START lucy=node:Person(name="Lucy Liu"), kevin=node:Person(name="Kevin Bacon") MATCH p = shortestPath( lucy-[:ACTS_IN*]-kevin ) RETURN EXTRACT(n in NODES(p) : COALESCE(n.name?, n.title?))Sunday, February 24, 13
  105. 105. START lucy=node:Person(name="Lucy Liu"), kevin=node:Person(name="Kevin Bacon") MATCH p = shortestPath( lucy-[:ACTS_IN*]-kevin ) RETURN EXTRACT(n in NODES(p) : COALESCE(n.name?, n.title?))Sunday, February 24, 13
  106. 106. START lucy=node:Person(name="Lucy Liu"), kevin=node:Person(name="Kevin Bacon") MATCH p = shortestPath( lucy-[:ACTS_IN*]-kevin ) RETURN EXTRACT(n in NODES(p) : COALESCE(n.name?, n.title?))Sunday, February 24, 13
  107. 107. Create Cypher 2 (the movie)Sunday, February 24, 13
  108. 108. START lucy = node:Person(name="Lucy Liu"), david = node:Person(name="David Lynch") CREATE andres={name:"Andrés Taylor"}, cypher2={title:"Cypher 2"}, lucy-[:ACTS_IN {name:"Rita Foster"}]->cypher2, andres-[:ACTS_IN {name:"Manuel"}]->cypher2, david-[:DIRECTED]->cypher2 RETURN cypher2Sunday, February 24, 13
  109. 109. START lucy = node:Person(name="Lucy Liu"), david = node:Person(name="David Lynch") CREATE andres={name:"Andrés Taylor"}, cypher2={title:"Cypher 2"}, lucy-[:ACTS_IN {name:"Rita Foster"}]->cypher2, andres-[:ACTS_IN {name:"Manuel"}]->cypher2, david-[:DIRECTED]->cypher2 RETURN cypher2Sunday, February 24, 13
  110. 110. START lucy = node:Person(name="Lucy Liu"), david = node:Person(name="David Lynch") CREATE andres={name:"Andrés Taylor"}, cypher2={title:"Cypher 2"}, lucy-[:ACTS_IN {name:"Rita Foster"}]->cypher2, andres-[:ACTS_IN {name:"Manuel"}]->cypher2, david-[:DIRECTED]->cypher2 RETURN cypher2Sunday, February 24, 13
  111. 111. START lucy = node:Person(name="Lucy Liu"), david = node:Person(name="David Lynch") CREATE andres={name:"Andrés Taylor"}, cypher2={title:"Cypher 2"}, lucy-[:ACTS_IN {name:"Rita Foster"}]->cypher2, andres-[:ACTS_IN {name:"Manuel"}]->cypher2, david-[:DIRECTED]->cypher2 RETURN cypher2Sunday, February 24, 13
  112. 112. START lucy = node:Person(name="Lucy Liu"), david = node:Person(name="David Lynch") CREATE andres={name:"Andrés Taylor"}, cypher2={title:"Cypher 2"}, lucy-[:ACTS_IN {name:"Rita Foster"}]->cypher2, andres-[:ACTS_IN {name:"Manuel"}]->cypher2, david-[:DIRECTED]->cypher2 RETURN cypher2Sunday, February 24, 13
  113. 113. START lucy = node:Person(name="Lucy Liu"), david = node:Person(name="David Lynch") CREATE andres={name:"Andrés Taylor"}, cypher2={title:"Cypher 2"}, lucy-[:ACTS_IN {name:"Rita Foster"}]->cypher2, andres-[:ACTS_IN {name:"Manuel"}]->cypher2, david-[:DIRECTED]->cypher2 RETURN cypher2Sunday, February 24, 13
  114. 114. Add genre nodesSunday, February 24, 13
  115. 115. Person :DIRECTED :ACTS_IN Movie Genre :GENRE :IS_GENRE RootSunday, February 24, 13
  116. 116. Movie Movie title:"Highlander" title:"The Matrix" genre: "Action" genre: "Action" :GENRE :GENRE Genre name: "Action" :IS_GENRE RootSunday, February 24, 13
  117. 117. START ref=node(0), movie=node:Movie("title:*") WHERE HAS(movie.genre) CREATE UNIQUE ref<-[:IS_GENRE]->(genre {name:movie.genre}) genre-[:GENRE]->movieSunday, February 24, 13
  118. 118. START ref=node(0), movie=node:Movie("title:*") WHERE HAS(movie.genre) CREATE UNIQUE ref<-[:IS_GENRE]->(genre {name:movie.genre}) genre-[:GENRE]->movieSunday, February 24, 13
  119. 119. START ref=node(0), movie=node:Movie("title:*") WHERE HAS(movie.genre) CREATE UNIQUE ref<-[:IS_GENRE]->(genre {name:movie.genre}) genre-[:GENRE]->movieSunday, February 24, 13
  120. 120. START ref=node(0), movie=node:Movie("title:*") WHERE HAS(movie.genre) CREATE UNIQUE ref<-[:IS_GENRE]->(genre {name:movie.genre}) genre-[:GENRE]->movieSunday, February 24, 13
  121. 121. RehashSunday, February 24, 13
  122. 122. START designates the start pointsSunday, February 24, 13
  123. 123. MATCH describes the patternSunday, February 24, 13
  124. 124. WHERE filters the result setSunday, February 24, 13
  125. 125. CREATE creates nodes and relationshipsSunday, February 24, 13
  126. 126. CREATE UNIQUE creates unique patternsSunday, February 24, 13
  127. 127. RETURN creates the result tableSunday, February 24, 13
  128. 128. QUESTION ALL THE THINGS!Sunday, February 24, 13
  1. A particular slide catching your eye?

    Clipping is a handy way to collect important slides you want to go back to later.

×