Intro to Cypher
Upcoming SlideShare
Loading in...5
×
 

Intro to Cypher

on

  • 3,135 Views

Putting your data in a graph database is easy. We wanted to make it as easy to query the data. That's why we set out to create the cypher query language. This session introduces cypher shows a lot of ...

Putting your data in a graph database is easy. We wanted to make it as easy to query the data. That's why we set out to create the cypher query language. This session introduces cypher shows a lot of examples and explains how it is used.

Statistics

Views

Total Views
3,135
Views on SlideShare
2,796
Embed Views
339

Actions

Likes
12
Downloads
194
Comments
1

5 Einbettungen 339

http://blog.neo4j.org 268
http://neo4j.com 65
http://www.linkedin.com 4
https://twitter.com 1
http://localhost 1

Zugänglichkeit

Kategorien

Details hochladen

Uploaded via as Apple Keynote

Benutzerrechte

© Alle Rechte vorbehalten

Report content

Als unangemessen gemeldet Als unangemessen melden
Als unangemessen melden

Wählen Sie Ihren Grund, warum Sie diese Präsentation als unangemessen melden.

Löschen

11 of 1

  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Ihre Nachricht erscheint hier
    Processing...
  • Hello,

    Thanks so much for adding the presentation.

    Although, I've been having a awful time trying to view it.
    First, the suffix was .key.
    I changed this to .ppt, but I still get errors.
    Now it won't open.

    How can I get a simple and reliable PDF?

    Thanks a lot!

    Rodger
    Are you sure you want to
    Ihre Nachricht erscheint hier
    Processing...
Kommentar posten
Kommentar bearbeiten
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • 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.\n\nWe looked at alternatives - SPARQL, SQL, Gremlin and other...\n
  • 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.\n\nWe looked at alternatives - SPARQL, SQL, Gremlin and other...\n
  • 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.\n\nWe looked at alternatives - SPARQL, SQL, Gremlin and other...\n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • 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.\n\nWe looked at alternatives - SPARQL, SQL, Gremlin and other...\n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • 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.\n\nWe looked at alternatives - SPARQL, SQL, Gremlin and other...\n
  • \n
  • \n
  • 3rd minute\n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • 5th minute\n
  • \n
  • Life-Console\n•In Memory GDBs in Web Session\n•Set up with mutating Cypher (or Geoff)\n•Executes Cypher (also mutating)\n•Visualizes Graph & Query Results (d3)\n•Multiple Cypher Versions\n•Share: short link, tweet, yUML\n•Embeddable \n•Live Console for Docs\n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n

Intro to Cypher Intro to Cypher Presentation Transcript

  • (Neo4j)-[: ]->Cypher Michael Hunger - Neo Technology 1
  • (Michael) -[:WORKS_ON]-> (Neo4j) consoleCypher community graph Community MEServer Spring Cloud 2
  • 3
  • is a 4
  • NOSQL Neo4j 5
  • Graph Database 6
  • 7
  • Were talking about aProperty Graph 7
  • Were talking about aProperty Graph Nodes 7
  • Were talking about aProperty Graph Nodes Relationships 7
  • Were talking about aProperty Graph Em Joh il a n knows knows Alli Tob Lar Nodes son ias knows s knows And And knows knows rea rés s knows knows knows Pet Miic Mc knows Ian er knows a a knows knows De Mic lia h ael Relationships Properties (each a key+value) + Indexes (for easy look-ups) 7
  • 7
  • 8
  • (Neo4j) -[:IS_A]-> (Graph Database) LuceneSharding 1 M/s Master/ Index LS Slave TRAVERSA HIG TES H_A VA RA G IL. TE IN PROVIDES ACID Server RUN S_A LI TX S CE NS ED _L ES_T Ruby IK RU JS E MySQL S _A NS SC AL OClojure _O NS .net RU N Mongo 34bn embedded Heroku Nodes 9
  • How do you query this graph database? 10
  • With a Graph Query Language: Cypher 11
  • What is Cypher?๏Pattern-Matching Query Language๏Humane language๏Expressive๏Declarative: Say what you want, not how๏borrows from well known query langs๏Aggregation, Ordering, Limit๏Update the Graph 12
  • Something new? Why?๏ Existing Neo4j query mechanisms were not simple enough • Too verbose (Java API) • Too prescriptive (Gremlin)๏ SQL: Unable to express paths • these are crucial for graph-based reasoning • Neo4j is schema/table free๏ SPARQL: designed for a different data model • namespaces • properties as nodes 13
  • A GraphCan‘t see the Patterns for the Trees http://maxdemarzi.com/2012/02/13/visualizing-a-network-with-cypher/ 14
  • It‘s all about Patterns A B CWe want to find this Pattern! 15
  • Patterns in a Graph 16
  • 17
  • 18
  • 19
  • 20
  • Patterns as ASCII-ART 21
  • Patterns as ASCII-ART () --> () 21
  • Named Nodes A B 22
  • Named Nodes A B(A) --> (B) 22
  • Named Directed Rels LOVES A B 23
  • Named Directed Rels LOVES A B A -[:LOVES]-> B 23
  • PathsA B C 24
  • PathsA B CA --> B --> C 24
  • Cyclic-Path-Patterns A B C 25
  • Cyclic-Path-Patterns A B CA --> B --> C, A --> C 25
  • Cyclic-Path-Patterns A B CA --> B --> C, A --> C A --> B --> C <-- A 25
  • Variable Length Paths A B A BA B ... A -[*]-> B 26
  • Optional Relationships A B 27
  • Optional Relationships A B A -[?]-> B 27
  • 28
  • How does it work? 28
  • How does it work? 28
  • How does it work?// lookup starting point in an indexstart n=node:People(name = ‘Andreas’) And rea s 28
  • How does it work?// lookup starting point in an index then traverse to find resultsstart n=node:People(name = ‘Andreas’) match (n)--()--(foaf) return foaf And rea s 28
  • 28
  • The Parts of Cypher 29
  • 30
  • Cypher: START + RETURN๏ START <lookup> RETURN <expressions>๏ START binds terms using simple look-up •directly using known ids •or based on indexed Property๏ RETURN expressions specify result set 30
  • Cypher: START + RETURN๏ START <lookup> RETURN <expressions>๏ START binds terms using simple look-up •directly using known ids •or based on indexed Property๏ RETURN expressions specify result set // lookup node id 0, return that node start n=node(0) return n // lookup node in Index, return that node start n=node:Person(name="Andreas") return n // lookup all nodes, return all name properties start n=node(*) return n.name 30
  • 31
  • Cypher: MATCH๏ START <lookup> MATCH <pattern> RETURN <expr>๏ MATCH describes a pattern of nodes+relationships •node terms in optional parenthesis •lines with arrows for relationships 31
  • Cypher: MATCH๏ START <lookup> MATCH <pattern> RETURN <expr>๏ MATCH describes a pattern of nodes+relationships •node terms in optional parenthesis •lines with arrows for relationships // lookup n, traverse any relationship to some m start n=node(0) match (n)--(m) return n,m // any outgoing relationship from n to m start n=node(0) match n-->m return n,m // only KNOWS relationships from n to m start n=node(0) match n-[:KNOWS]->m return n,m // from n to m and capture the relationship as r start n=node(0) match n-[r]->m return n,r,m // from n outgoing to m, then incoming from o start n=node(0) match n-->m<--o return n,m,o 31
  • 32
  • Cypher: RETURN๏ RETURN <expressions>, aggregation(expr) as alias๏ RETURN nodes, rels, properties๏ RETURN expressions of functions and operators๏ RETURN aggregation functions on the above 32
  • Cypher: RETURN๏ RETURN <expressions>, aggregation(expr) as alias๏ RETURN nodes, rels, properties๏ RETURN expressions of functions and operators๏ RETURN aggregation functions on the above // aggregate on n, count the m‘s start n=node(0) match n--m return n,count(*) // alias n.name as name start n=node(0) return n.name as name // aggregate m‘s into list start n=node(*) match n--m return n,collect(m) // filter m‘s by name start n=node(*) match n--m return n,filter(x in collect(m): m.name ˜= /A.*/) as a_block 32
  • 33
  • Cypher: WHERE๏ START <lookup> [MATCH <pattern>] WHERE <condition> RETURN <expr>๏ WHERE filters nodes or relationships •uses expressions to constrain elements 33
  • Cypher: WHERE๏ START <lookup> [MATCH <pattern>] WHERE <condition> RETURN <expr>๏ WHERE filters nodes or relationships •uses expressions to constrain elements // lookup all nodes as n, constrained to name Andreas start n=node(*) where n.name=Andreas return n // filter nodes where age is less than 30 start n=node(*) where n.age<30 return n // filter using a regular expression start n=node(*) where n.name =~ /Tob.*/ return n // filter for a property exists start n=node(*) where has(n.name) return n 33
  • 34
  • Cypher: CREATE๏ CREATE <node>[,node or relationship] RETURN <expr> •create nodes with optional properties •create relationship (must have a type) 34
  • Cypher: CREATE๏ CREATE <node>[,node or relationship] RETURN <expr> •create nodes with optional properties •create relationship (must have a type) // create an anonymous node create n // create node with a property, returning it create n={name:Andreas} return n // lookup 2 nodes, then create a relationship and return it start n=node(0),m=node(1) create n-[r:KNOWS]-m return r // lookup nodes, then create a relationship with properties start n=node(1),m=node(2) create n-[r:KNOWS {since:2008}]->m 34
  • 35
  • Cypher: CREATE UNIQUE๏ CREATE UNIQUE node-[rel]->(node {prop : value}) •„fixes“ the graph •starts at bound nodes, properties rels and nodes by comparing types and tries to find •if not found creates them 35
  • Cypher: CREATE UNIQUE๏ CREATE UNIQUE node-[rel]->(node {prop : value}) •„fixes“ the graph •starts at bound nodes, properties rels and nodes by comparing types and tries to find •if not found creates them // create a new relationship start n=.., m=.. create unique n-[:KNOWS]->m // create a new node AND relationship start n=... create unique n-[:TAGGED]->(tag {name:“neo“}) // matches the tag node by name only creates new relationship start n=... create unique n-[:TAGGED]->(tag {name:“neo“}) 35
  • 36
  • Cypher: SET๏ SET [<node property>] [<relationship property>] •update a property on a node or relationship •must follow a START 36
  • Cypher: SET๏ SET [<node property>] [<relationship property>] •update a property on a node or relationship •must follow a START // update the name property start n=node(0) set n.name=Peter // update many nodes, using a calculation start n=node(*) set n.size=n.size+1 // match & capture a relationship, update a property start n=node(1) match n-[r]-m set r.times=10 36
  • 37
  • Cypher: DELETE๏ DELETE [<node>|<relationship>|<property>] •delete a node, relationship or property •toall relationships must be deleted first delete a node, 37
  • Cypher: DELETE๏ DELETE [<node>|<relationship>|<property>] •delete a node, relationship or property •toall relationships must be deleted first delete a node, // delete a node start n=node(5) delete n // remove a node and all relationships start n=node(3) match n-[r]-() delete n, r // remove a property start n=node(3) delete n.age 37
  • More Advanced Examples 38
  • START user = node(1) MATCH user -[user_skill]-> skill RETURN skill, user_skillSELECT skills.*, user_skill.*FROM usersJOIN user_skill ON users.id = user_skill.user_idJOIN skills ON user_skill.skill_id = skill.id WHERE users.id = 1 39
  • Example: Old, Influential FriendsSTART me = node(...)MATCH (me) - [f:FRIEND] - (old_friend) - [:FRIEND ] - (fof)WHERE ({today}-f.begin) > 365*10WITH  old_friend, collect(fof.name) as namesWHERE length(names) > 100RETURN old_friend, namesORDER BY old_friend.name ASC f:FRIEND :FRIEND me friend fof
  • Example: Simple RecommendationSTART me = node(...)MATCH (me) -[r1:RATED ]->(thing) <-[r2:RATED ]- (someone) -[r3:RATED ]->(cool_thing)WHERE ABS(r1.stars-r2.stars) <= 2 AND r3.stars > 3RETURN cool_thing, count(*) AS cntORDER BY cnt DESC LIMIT 10 r1:RATED thing r2:RATED me so TED r 3: RA cool thing 41
  • Cypher Cheat Sheet http://neo4j.org/resources/cypher 42
  • The Rabbithole http://console.neo4j.org This Graph: http://tinyurl.com/7cnvmlq 43
  • How to use Cypherin YOUR programs 44
  • Neo4j API ExecutionEngine engine = new ExecutionEngine(graphDB); String query = „start n=node:Person(name={name}) match n-[:ACTS_IN]->movie<-[:ACTS_IN]-friend return friend“; ExecutionResult result = engine.query(query, map(„name“, „Keanu“); for (Map<String,Object> row : result) { Node friend = row.get(„friend“); } Iterator<Node> friends = result.columnAs(„friend“);http://bit.ly/cypher-queries 45
  • How to get started? 46
  • How to get started?๏ Documentation 46
  • How to get started?๏ Documentation • docs.neo4j.org - tutorials+reference 46
  • How to get started?๏ Documentation • docs.neo4j.org - tutorials+reference • http://console.neo4j.org 46
  • How to get started?๏ Documentation • docs.neo4j.org - tutorials+reference • http://console.neo4j.org • Neo4j in Action 46
  • How to get started?๏ Documentation • docs.neo4j.org - tutorials+reference • http://console.neo4j.org • Neo4j in Action • Good Relationships 46
  • How to get started?๏ Documentation • docs.neo4j.org - tutorials+reference • http://console.neo4j.org • Neo4j in Action • Good Relationships๏ Get Neo4j 46
  • How to get started?๏ Documentation • docs.neo4j.org - tutorials+reference • http://console.neo4j.org • Neo4j in Action • Good Relationships๏ Get Neo4j • http://neo4j.org/download 46
  • How to get started?๏ Documentation • docs.neo4j.org - tutorials+reference • http://console.neo4j.org • Neo4j in Action • Good Relationships๏ Get Neo4j • http://neo4j.org/download • http://addons.heroku.com/neo4j/๏ Participate • http://groups.google.com/group/neo4j • http://neo4j.meetup.com 46
  • How to get started?๏ Documentation • docs.neo4j.org - tutorials+reference • http://console.neo4j.org • Neo4j in Action • Good Relationships๏ Get Neo4j • http://neo4j.org/download • http://addons.heroku.com/neo4j/๏ Participate • http://groups.google.com/group/neo4j • http://neo4j.meetup.com • a session like this one ;) 46
  • http://console.neo4j.org/r/GoThttps://dl.dropbox.com/u/14493611/got.txt 47
  • Neo4j-JDBC Driver & MovieDB Datasethttp://blog.neo4j.org/2012/06/wanted-your- help-in-testing-neo4j-jdbc.html 48
  • Got a Conference? Need a Dataset?http://blog.neo4j.org/2012/08/at-conference- need-dataset-neo4j-at.html 49
  • Neo4j in ActionSoftware Metrics 50
  • Graphs in Software Technolgoy๏ UML Diagrams are graphs๏ dependencies between classes, packages, modules etc are graphs๏ Software Metrics use dependency analysis๏ Visualizations๏ Cyclomatic Complexity,๏ Fan-in (afferent-coupling) / Fan-out (efferent coupling) etc. 51
  • Code City 52
  • Class Diagram is a Graph 53
  • SonarJ 54
  • But there is more๏Visualize & query Method, Field dependencies๏Collaborative filtering (co-usage)๏Ranking๏God classes๏Paths between classes 55
  • Welcome to Class-Graph๏take a JAR๏put it under ASM๏scan it superfast๏pull everything into Neo4j๏add categories, indexes๏Have Fun http://github.com/jexp/class-graph 56
  • Welcome to Class-Graph 57
  • Interactive Hands-On Session๏Lots of tasks๏use Cypher to solve them http://neo4j.org/resources/cypher๏be creative, work together๏ask !๏Server http://bit.ly/innoq-neo4j๏just the beginning 58
  • Task: Find java.lang.Number and return it 59
  • Task: Find java.lang.Number and return itSTART o=node:types(name="java.lang.Number") RETURN o; 59
  • Task: Subclasses of Number?•Return just the name•Order them alphabetically 60
  • Task: Subclasses of Number?START n=node:types(name="java.lang.Number")  MATCH n<-[:SUPER_TYPE]-s RETURN s.name ORDER BY s.name;•Return just the name•Order them alphabetically 60
  • Task: Which Methods does it have / how many•Find the top 5 classes with the most members 61
  • Task: Which Methods does it have / how manySTART n=node:types(name="java.lang.Number") MATCH n-[:METHOD_OF|FIELD_OF]->mRETURN m;•Find the top 5 classes with the most members 61
  • Task: Calculate the fan-out ofjava.lang.StringBuilder•Calculate fan-in•Which class has the highest fan-out•What about package-level? 62
  • Task: Calculate the fan-out of java.lang.StringBuilderSTART o=node:types(name="j.l.StringBuilder")MATCH o-[:FIELD_OF]->f-[:FIELD_TYPE]->tf, o-[:METHOD_OF]->m-[:PARAM_TYPE]->tp, m-[:RETURN_TYPE]->trRETURN o,count(distinct tf) + count(distinct tp) + count(distinct tr) as fan_out;•Calculate fan-in•Which class has the highest fan-out•What about package-level? 62
  • Task: Find longest Inheritance Path 63
  • Task: Find longest Inheritance Pathstart c=node:types(name="java.lang.Object") match path=p<-[:SUPER_TYPE*]-c return extract(n in nodes(path) : n.name),length(path) as lenorder by len desc limit 5; 63
  • Task: Find the class that used IOExceptionmost often 64
  • Task: Find the class that used IOExceptionmost oftenSTART ex=node:types(name="java.io.IOException"MATCH ex<-[:THROWS]-m<-[:METHOD_OF]-cRETURN c, count(*)ORDER BY count(*)LIMIT 5; 64
  • Task: Which other classes did classes that threw IOException use most often?•What could be a source of IOExceptions 65
  • Task: Which other classes did classes that threw IOException use most often?START ex=node:types(name="java.io.IOException")MATCH ex<-[:THROWS]-m<-[:METHOD_OF]-c,  mbr<-[:METHOD_OF|FIELD_OF]-c, mbr-[:FIELD_TYPE|PARAM_TYPE| RETURN_TYPE|THROWS]->other_typeWHERE other_type.name =~ /.+[.].+/RETURN other_type.name, count(*)ORDER BY count(*) descLIMIT 10;•What could be a source of IOExceptions 65
  • Task: Find a class you like and add a field withyour name and some type 66
  • Task: Find a class you like and add a field with your name and some typeSTART c=node:types(name="void"), t=node:types(name="java.lang.reflect.Proxy") CREATE c-[:FIELD_OF]->(field {name:“Michael“}) -[:FIELD_TYPE]->t; 66
  • Task: Delete the most annoying class and all itsmethods, fields and their relationships 67
  • Task: Delete the most annoying class and all its methods, fields and their relationshipsSTART c=node:types(name="java.awt.List"),MATCH c-[r1:FIELD_OF|METHOD_OF]->mbr-[r2]-() c-[r]-()DELETE c,mbr,r1,r2,r; 67