jugmarche: neo4j 2 (cypher)

18
Neo4j 2: Learning Cypher JUG Marche - June 2014 Onofrio Panzarino

Upload: onofrio-panzarino

Post on 10-May-2015

479 views

Category:

Software


0 download

DESCRIPTION

Slides of the JugMarche June meeting, about Neo4j and Cypher

TRANSCRIPT

Page 1: JugMarche: Neo4j 2 (Cypher)

Neo4j 2: Learning CypherJUG Marche - June 2014

Onofrio Panzarino

Page 2: JugMarche: Neo4j 2 (Cypher)

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)

What is Neo4j

- Graph database- ACID transaction- 2 running modes > Embedded (JAVA API) > Server (REST)- Cypher Query Language

Page 4: JugMarche: Neo4j 2 (Cypher)

Model: nodes and relationships

Source: http://gist.neo4j.org/?8635758

Page 5: JugMarche: Neo4j 2 (Cypher)

Schema?

Schemaless (key-value)

Nodes: > propertiesRelationships: > properties

SchemaNodes:

0+ LabelsRelationships: 1 TypeIndexes+Constraints

Page 6: JugMarche: Neo4j 2 (Cypher)

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)

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)

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)

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)

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)

Idempotent operations

MATCH (a:Language)REMOVE a.version

MATCH (a:Language)DELETE a

Page 12: JugMarche: Neo4j 2 (Cypher)

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)

Optional paths

MATCH (a:Language)OPTIONAL MATCH (a)-[:RUNS_ON]-(b)RETURN a,bORDER BY a.name

Page 14: JugMarche: Neo4j 2 (Cypher)

Aggregations

MATCH (a:Language)OPTIONALMATCH (a)-[:RUNS_ON]-(b)RETURN b.name, COUNT(*)

Page 15: JugMarche: Neo4j 2 (Cypher)

Other keywords

Splitting queriesWITH

Manipulating collectionsEXTRACT, FILTER, REDUCE

PredicatesALL, ANY, SINGLE, NONE

Page 16: JugMarche: Neo4j 2 (Cypher)

More features

IntegrationLOAD CSV WITH HEADERS

FROM <file> AS line

CREATE ...

ProfilingPROFILE <query>

Page 17: JugMarche: Neo4j 2 (Cypher)

Too simple domain?

http://gist.neo4j.org/