jugmarche: neo4j 2 (cypher)
DESCRIPTION
Slides of the JugMarche June meeting, about Neo4j and CypherTRANSCRIPT
![Page 1: JugMarche: Neo4j 2 (Cypher)](https://reader033.vdocuments.site/reader033/viewer/2022052822/554f399ab4c90572088b4e61/html5/thumbnails/1.jpg)
Neo4j 2: Learning CypherJUG Marche - June 2014
Onofrio Panzarino
![Page 2: JugMarche: Neo4j 2 (Cypher)](https://reader033.vdocuments.site/reader033/viewer/2022052822/554f399ab4c90572088b4e61/html5/thumbnails/2.jpg)
Graph databases and me
● RDF (Semantic web)○ 2004-2005: Sesame (SPARQL)
● Neo4j○ 2011: Java API (Embedded), Traversers○ 2013: Cypher
Twitter: @onof80 Blog: http://learningcypher.blogspot.com My book: Learning Cypher
![Page 3: JugMarche: Neo4j 2 (Cypher)](https://reader033.vdocuments.site/reader033/viewer/2022052822/554f399ab4c90572088b4e61/html5/thumbnails/3.jpg)
What is Neo4j
- Graph database- ACID transaction- 2 running modes > Embedded (JAVA API) > Server (REST)- Cypher Query Language
![Page 4: JugMarche: Neo4j 2 (Cypher)](https://reader033.vdocuments.site/reader033/viewer/2022052822/554f399ab4c90572088b4e61/html5/thumbnails/4.jpg)
Model: nodes and relationships
Source: http://gist.neo4j.org/?8635758
![Page 5: JugMarche: Neo4j 2 (Cypher)](https://reader033.vdocuments.site/reader033/viewer/2022052822/554f399ab4c90572088b4e61/html5/thumbnails/5.jpg)
Schema?
Schemaless (key-value)
Nodes: > propertiesRelationships: > properties
SchemaNodes:
0+ LabelsRelationships: 1 TypeIndexes+Constraints
![Page 6: JugMarche: Neo4j 2 (Cypher)](https://reader033.vdocuments.site/reader033/viewer/2022052822/554f399ab4c90572088b4e61/html5/thumbnails/6.jpg)
Querying: REST API
Properties of node with id 32:GET http://localhost:7474/db/data/node/32/properties
{
"user": "@onof80",
"lastLogin": 1394304000,
"tags": ["Java","Scala","NoSql"]
}
![Page 7: JugMarche: Neo4j 2 (Cypher)](https://reader033.vdocuments.site/reader033/viewer/2022052822/554f399ab4c90572088b4e61/html5/thumbnails/7.jpg)
Traversing the graphfor ( Path position : db.traversalDescription()
.depthFirst()
.relationships( Rels.KNOWS, Direction.INCOMING )
.evaluator( Evaluators.toDepth( 5 ) )
.traverse( node6 ) )
{
output += position + "\n";
}
(6)
(6)<--[KNOWS,1]--(3)
(6)<--[KNOWS,1]--(3)<--[KNOWS,6]--(0)
(6)<--[KNOWS,1]--(3)<--[KNOWS,6]--(0)<--[KNOWS,4]--(5)
Powerful but hardcoded
![Page 8: JugMarche: Neo4j 2 (Cypher)](https://reader033.vdocuments.site/reader033/viewer/2022052822/554f399ab4c90572088b4e61/html5/thumbnails/8.jpg)
Querying with Cypher
START n=node(10)MATCH path =(n)<-[:KNOWS*0..5]-()RETURN path
http://console.neo4j.org/
![Page 9: JugMarche: Neo4j 2 (Cypher)](https://reader033.vdocuments.site/reader033/viewer/2022052822/554f399ab4c90572088b4e61/html5/thumbnails/9.jpg)
Create a full path
CREATE (a:Language { name: "Java"}),
(b:Language { name: "Scala" }),
(c:Language { name: "JavaScript" }),
(jvm:Platfom { name: "JVM" }),
path=(a)-[r:RUNS_ON]->(jvm)
<-[r2:RUNS_ON]-(b)
RETURN path
![Page 10: JugMarche: Neo4j 2 (Cypher)](https://reader033.vdocuments.site/reader033/viewer/2022052822/554f399ab4c90572088b4e61/html5/thumbnails/10.jpg)
MERGE
Look for a pattern:● Found? Match it● Else create it
MERGE (a:Language { name: "Java" })
SET a.version = 8
RETURN a
![Page 11: JugMarche: Neo4j 2 (Cypher)](https://reader033.vdocuments.site/reader033/viewer/2022052822/554f399ab4c90572088b4e61/html5/thumbnails/11.jpg)
Idempotent operations
MATCH (a:Language)REMOVE a.version
MATCH (a:Language)DELETE a
![Page 12: JugMarche: Neo4j 2 (Cypher)](https://reader033.vdocuments.site/reader033/viewer/2022052822/554f399ab4c90572088b4e61/html5/thumbnails/12.jpg)
But...
MERGE (a:Language { name: "Java" })
ON MATCH SET a.version = a.version + 1
ON CREATE SET a.version = 1
RETURN a
![Page 13: JugMarche: Neo4j 2 (Cypher)](https://reader033.vdocuments.site/reader033/viewer/2022052822/554f399ab4c90572088b4e61/html5/thumbnails/13.jpg)
Optional paths
MATCH (a:Language)OPTIONAL MATCH (a)-[:RUNS_ON]-(b)RETURN a,bORDER BY a.name
![Page 14: JugMarche: Neo4j 2 (Cypher)](https://reader033.vdocuments.site/reader033/viewer/2022052822/554f399ab4c90572088b4e61/html5/thumbnails/14.jpg)
Aggregations
MATCH (a:Language)OPTIONALMATCH (a)-[:RUNS_ON]-(b)RETURN b.name, COUNT(*)
![Page 15: JugMarche: Neo4j 2 (Cypher)](https://reader033.vdocuments.site/reader033/viewer/2022052822/554f399ab4c90572088b4e61/html5/thumbnails/15.jpg)
Other keywords
Splitting queriesWITH
Manipulating collectionsEXTRACT, FILTER, REDUCE
PredicatesALL, ANY, SINGLE, NONE
![Page 16: JugMarche: Neo4j 2 (Cypher)](https://reader033.vdocuments.site/reader033/viewer/2022052822/554f399ab4c90572088b4e61/html5/thumbnails/16.jpg)
More features
IntegrationLOAD CSV WITH HEADERS
FROM <file> AS line
CREATE ...
ProfilingPROFILE <query>
![Page 18: JugMarche: Neo4j 2 (Cypher)](https://reader033.vdocuments.site/reader033/viewer/2022052822/554f399ab4c90572088b4e61/html5/thumbnails/18.jpg)
Any questions?
Q&A
More infos:http://docs.neo4j.org/ My bookAll Neo4j books