Your SlideShare is downloading. ×
0
Cypher
Cypher
Cypher
Cypher
Cypher
Cypher
Cypher
Cypher
Cypher
Cypher
Cypher
Cypher
Cypher
Cypher
Cypher
Cypher
Cypher
Cypher
Cypher
Cypher
Cypher
Cypher
Cypher
Cypher
Cypher
Cypher
Cypher
Cypher
Cypher
Cypher
Cypher
Cypher
Cypher
Cypher
Cypher
Cypher
Cypher
Cypher
Cypher
Cypher
Cypher
Cypher
Cypher
Cypher
Cypher
Cypher
Cypher
Cypher
Cypher
Cypher
Cypher
Cypher
Cypher
Upcoming SlideShare
Loading in...5
×

Thanks for flagging this SlideShare!

Oops! An error has occurred.

×
Saving this for later? Get the SlideShare app to save on your phone or tablet. Read anywhere, anytime – even offline.
Text the download link to your phone
Standard text messaging rates apply

Cypher

19,313

Published on

Published in: Technology, Business
1 Comment
18 Likes
Statistics
Notes
No Downloads
Views
Total Views
19,313
On Slideshare
0
From Embeds
0
Number of Embeds
11
Actions
Shares
0
Downloads
289
Comments
1
Likes
18
Embeds 0
No embeds

Report content
Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
No notes for slide
  • There existed a number of different ways to query a graph database. This one aims to make querying easy, and to produce queries that are readable. We looked at alternatives - SPARQL, SQL, Gremlin and other...
  • Transcript

    • 1. Cypher Query Language Chicago Graph Database Meet-Up Max De Marzi
    • 2. What is Cypher?• Graph Query Language for Neo4j• Aims to make querying simple
    • 3. Why Cypher? • Existing Neo4j query mechanisms were not simple enough • Too verbose (Java API) • Too prescriptive (Gremlin)
    • 4. SQL? • Unable to express paths • these are crucial for graph-based reasoning • Neo4j is schema/table free
    • 5. SPARQL? • SPARQL designed for a different data model • namespaces • properties as nodes • high learning curve
    • 6. Design
    • 7. Design Decisions Declarative Most of the time, Neo4j knows better than you Imperative Declarative follow relationship specify starting pointbreadth-first vs depth-first specify desired outcome explicit algorithm algorithm adaptable based on query
    • 8. Design Decisions Pattern matching
    • 9. Design Decisions Pattern matching A B C
    • 10. Design Decisions Pattern matching
    • 11. Design Decisions Pattern matching
    • 12. Design Decisions Pattern matching
    • 13. Design Decisions Pattern matching
    • 14. Design Decisions ASCII-art patterns () --> ()
    • 15. Design Decisions Directed relationship A B (A) --> (B)
    • 16. Design Decisions Undirected relationship A B (A) -- (B)
    • 17. Design Decisions specific relationships LOVES A B A -[:LOVES]-> B
    • 18. Design Decisions Joined paths A B C A --> B --> C
    • 19. Design Decisions multiple paths A B C A --> B --> C, A --> C A --> B --> C <-- A
    • 20. Design Decisions Variable length paths A B A B A B ... A -[*]-> B
    • 21. Design Decisions Optional relationships A B A -[?]-> B
    • 22. Design Decisions Familiar for SQL users select start from match where where group by return order by
    • 23. STARTSELECT *FROM PersonWHERE firstName = “Max”START max=node:persons(firstName = “Max”)RETURN max
    • 24. MATCHSELECT skills.*FROM usersJOIN skills ON users.id = skills.user_idWHERE users.id = 101START user = node(101)MATCH user --> skillsRETURN skills
    • 25. Optional MATCHSELECT skills.*FROM usersLEFT JOIN skills ON users.id = skills.user_idWHERE users.id = 101START user = node(101)MATCH user –[?]-> skillsRETURN skills
    • 26. SELECT skills.*, user_skill.*FROM usersJOIN user_skill ON users.id = user_skill.user_idJOIN skills ON user_skill.skill_id = skill.id WHEREusers.id = 1
    • 27. START user = node(1)MATCH user -[user_skill]-> skillRETURN skill, user_skill
    • 28. IndexesUsed as multiple starting points, not to speedup any traversalsSTART a = node:nodes_index(type=User) MATCHa-[r:knows]-bRETURN ID(a), ID(b), r.weight
    • 29. http://maxdemarzi.com/2012/03/16/jung-in-neo4j-par
    • 30. Complicated MatchSome UGLY recursive self join on the groupstableSTART max=node:person(name=“Max")MATCH group <-[:BELONGS_TO*]- maxRETURN group
    • 31. WhereSELECT person.*FROM personWHERE person.age >32 OR person.hair = "bald"START person = node:persons("name:*") WHEREperson.age >32 OR person.hair = "bald"RETURN person
    • 32. ReturnSELECT person.name, count(*)FROM PersonGROUP BY person.nameORDER BY person.nameSTART person=node:persons("name:*") RETURNperson.name, count(*)ORDER BY person.name
    • 33. Order By, ParametersSame as SQL{node_id} expected as part of requestSTART me = node({node_id})MATCH (me)-[?:follows]->(friends)-[?:follows]->(fof)-[?:follows]->(fofof)-[?:follows]->othersRETURN me.name, friends.name, fof.name, fofof.name, count(others)ORDER BY friends.name, fof.name, fofof.name, count(others) DESC
    • 34. http://maxdemarzi.com/2012/02/13/visualizing-a-netw
    • 35. Graph FunctionsSome UGLY multiple recursive self and inner joins onthe user and all related tablesSTART lucy=node(1000), kevin=node(759) MATCH p= shortestPath( lucy-[*]-kevin ) RETURN p
    • 36. Aggregate FunctionsID: get the neo4j assigned identifierCount: add up the number of occurrencesMin: get the lowest valueMax: get the highest valueAvg: get the average of a numeric valueDistinct: remove duplicatesSTART me = node:nodes_index(type = user)MATCH (me)-[r?:wrote]-()RETURN ID(me), me.name, count(r), min(r.date), max(r.date)" ORDERBY ID(me)
    • 37. FunctionsCollect: put all values in a listSTART a = node:nodes_index(type=User)MATCH a-[:follows]->bRETURN a.name, collect(b.name)
    • 38. http://maxdemarzi.com/2012/02/02/graph-visualizatio
    • 39. Combine FunctionsCollect the ID of friendsSTART me = node:nodes_index(type = user)"MATCH (me)<-[r?:wrote]-(friends)RETURN ID(me), me.name, collect(ID(friends)), collect(r.date)ORDER BY ID(me)
    • 40. http://maxdemarzi.com/2012/03/08/connections-in-time/
    • 41. UsesRecommend FriendsSTART me = node({node_id})MATCH (me)-[:friends]->(friend)-[:friends]->(foaf)RETURN foaf.name
    • 42. UsesSix Degrees of Kevin BaconLength: counts the number of nodes along a pathExtract: gets the nodes/relationships from a pathSTART me=node({start_node_id}), them=node({destination_node_id})MATCH path = allShortestPaths( me-[?*]->them )RETURN length(path), extract(person in nodes(path) : person.name)
    • 43. UsesSimilar UsersUsers who rated same items within 2 points.Abs: gets absolute numeric valueSTART me = node(user1)MATCH (me)-[myRating:RATED]->(i)<-[otherRating:RATED]-(u)WHERE abs(myRating.rating-otherRating.rating)<=2RETURN u
    • 44. Boolean OperationsItems with a rating > 7 that similar users rated, but I have notAnd: this and that are trueOr: this or that is trueNot: this is falseSTART me=node(user1),        similarUsers=node(3) (result received in the first query)MATCH (similarUsers)-[r:RATED]->(item)WHERE r.rating > 7 AND NOT((me)-[:RATED]->(item)) RETURN itemhttp://thought-bytes.blogspot.com/2012/02/similarity-based-recommendation
    • 45. PredicatesALL: closure is true for all itemsANY: closure is true for any itemNONE: closure is true for no itemsSINGLE: closure is true for exactly 1 itemSTART london = node(1), moscow = node(2)MATCH path = london -[*]-> moscowWHERE all(city in nodes(path) wherecity.capital = true)
    • 46. Design Decisions Parsed, not an internal DSL Execution Semantics Serialisation Type System Portability
    • 47. Design Decisions Database vs Application Design Goal: single user interaction expressible as single query Queries have enough logic to find required data, not enough to process it
    • 48. Implementation
    • 49. Implementation • Recursive matching with backtrackingSTART x=... MATCH x-->y, x-->z, y-->z, z-->a-->b, z-->b
    • 50. Implementation Execution Planstart n=node(0) Cypher is Pipesreturn n lazily evaluatedParameters() pulling from pipes underneathNodes(n)Extract([n])ColumnFilter([n])
    • 51. Implementation Execution Planstart n=node(0)match n-[*]-> breturn n.name, n, count(*)order by n.ageParameters()Nodes(n)PatternMatch(n-[*]->b)Extract([n.name, n])EagerAggregation( keys: [n.name, n], aggregates: [count(*)])Extract([n.age])Sort(n.age ASC)ColumnFilter([n.name,n,count(*)])
    • 52. Implementation Execution Planstart n=node(0)match n-[*]-> breturn n.name, n, count(*)order by n.nameParameters()Nodes(n)PatternMatch(n-[*]->b)Extract([n.name, n])Sort(n.name ASC,n ASC)EagerAgregation( keys: [n.name, n], aggregates: [count(*)])ColumnFilter([n.name,n,count(*)])
    • 53. Thanks for Listening! Questions?maxdemarzi.com

    Ă—