Neo4j Intro Training

Welcome

  1. Find a seat. Say hi!
  2. Grab a USB key, and plug it in.
Windows Mac/Linux

Run the "Install-Neo4j.exe"

Start a terminal

When you see Neo4j desktop

When you see the command prompt,

Click the start button

Type "./bin/neo4j start"

  1. Grab a coffee and get ready to begin!

Neo4j Tutorial

(you)-[:KNOW]->(Neo4j)

with Michael Hunger

info@neotechnology.com | @neo4j

Why did you come?

Answer five Questions

Schedule

  8.30am  -  Registration, installation and breakfast
  9.00am  -  Session 1: Graphs and Modeling
10.30am  -  Morning coffee break
10.45am  -  Session 2: Neo4j and Cypher
12.00pm  -  Lunch
  1.00pm  -  Session 3: Advanced queries using Cypher
  2.30pm  -  Afternoon coffee break
  2.45pm  -  Session 4: Next steps and Q&A
  4.00pm  -  End of training

(Introduction to Graphs)

But first an important message …

These are Charts …

charts

Not Graphs

charts not graphs

Leonhard Euler 1707-1783

euler

Swiss Mathematician

7 Bridges of Königsberg

konigsberg

Königsberg (Prussia) - 1736

7 Bridges, one route?

7 Bridges of Königsberg - Approach

konigsberg1

Identify parts → Landmasses & Bridges

7 Bridges of Königsberg - Model

konigsberg3

It’s a graph → Nodes & Relationships

7 Bridges of Königsberg - Solution

konigsberg4

Path finding → There was no route

So what about data?

Alice & Bob

Add Detail

Segue: Graphs in a Relational World

Tables

rel graph0

One table

rel graph1

Another one

rel graph2

JOINing them

rel graph4

This is the data

rel graph5

Explicit as a graph

rel graph6

Nodes

rel graph7

Relationships and …

rel graph8

Nodes, Relationships and Labels.

rel graph9

Modeling with graphs

Start with a whiteboard

1

model1

2

model2

3

model3

4

model4

5

model5

Model incrementally

6

model6

7

model7

(Brief tour of Neo4j)

Neo4j Browser - finding help

browser1

Neo4j Browser - execute Cypher, visualize

browser2

Neo4j Browser - importing sample data

browser3

Neo4j Data browser - display more data

browser4

Coffee?

Intro to Cypher

Cypher is Neo4j’s graph query language

It’s all about patterns

It’s all about patterns

bg1

Triangle1

Triangle2

How?

Two nodes, one relationship

The graph

  • a
  • b

Two nodes, one relationship

The pattern

  • a

The graph

ab1

ab2

ab3

Two nodes, one relationship

The graph

  • a

The code

Two nodes, one relationship

Two nodes, one relationship

Two nodes, one relationship

The graph

  • a
  • r

The code

(a)-[r] - >()

Two nodes, one relationship

Two nodes, one optional relationship

The graph

  • a

Optional stroke

Two nodes, one relationship

The graph

  • a
  • m
  • ACTED_IN

The code

(a)-[:ACTED_IN]→(m)

Returning role properties

Paths

The graph

  • a
  • b
  • c

The code

(a) - - >(b) - - >(c)

Paths

The graph

  • a
  • b
  • c

The code

(a) - - >(b)< - - (c)

Paths

The graph

  • a
  • m
  • d
  • ACTED_IN
  • DIRECTED

A Movies Query

The code

The result

a.name m.title d.name

“Keanu Reeves”

“The Matrix”

“Andy Wachowski”

“Keanu Reeves”

“The Matrix Reloaded”

“Andy Wachowski”

“Noah Wyle”

“A Few Good Men”

“Rob Reiner”

“Tom Hanks”

“Cloud Atlas”

“Andy Wachowski”

A Movies Query with alias

The code

The result

actor movie director

“Keanu Reeves”

“The Matrix”

“Andy Wachowski”

“Keanu Reeves”

“The Matrix Reloaded”

“Andy Wachowski”

“Noah Wyle”

“A Few Good Men”

“Rob Reiner”

“Tom Hanks”

“Cloud Atlas”

“Andy Wachowski”

Paths

The graph

  • a
  • m
  • m
  • d
  • ACTED_IN
  • DIRECTED

The code

Paths

Paths

The graph

  • a
  • m
  • d
  • ACTED_IN
  • DIRECTED

The code

Paths

The graph

  • a
  • m
  • d
  • ACTED_IN
  • DIRECTED

Paths

Aggregation

a.name m.title d.name

“Keanu Reeves”

“The Matrix”

“Andy Wachowski”

“Keanu Reeves”

“The Matrix Reloaded”

“Andy Wachowski”

“Noah Wyle”, “A Few Good Men”

“Rob Reiner”

“Tom Hanks”, “Cloud Atlas"

Aggregation functions

Aggregation

a.name d.name count(*)

“Aaron Sorkin"

“Rob Reiner”

2

“Keanu Reeves”

"Andy Wachowski”

3

“Hugo Weaving”

“Tom Tykwer”

1

Aggregation

a.name d.name count(m)

“Aaron Sorkin"

“Rob Reiner”

2

“Keanu Reeves”

"Andy Wachowski”

3

“Hugo Weaving”

“Tom Tykwer”

1

As

actor director count

“Aaron Sorkin"

“Rob Reiner”

2

“Keanu Reeves”

"Andy Wachowski”

3

“Hugo Weaving”

“Tom Tykwer”

1

Lab

Which directors acted also in their movie?

Unique relationships in paths

Sort and limit

Aggregation

notitle

  • a
  • m
  • d
  • ACTED_IN
  • DIRECTED

Starting somewhere

Get all nodes

Find a specific node (within all-nodes)

The Graph

notitle

Find a specific node (with a label)

The Graph

notitle

Start with a specific (labeled) node

(Movies featuring Tom Hanks)

The Graph

notitle

notitle

Start with a specific labeled node

(Directors who worked with Tom Hanks)

The Graph

Tom Hanks

Movies

Directors

Start with a specific labeled node

(Directors who worked with Tom Hanks)

Create label-specific index

Create two indexes.

Labels on pattern "anchors"

(Movies featuring both Tom Hanks and Kevin Bacon)

Conditions

(Movies in which Tom Hanks acted, that were released before 1992)

The Graph

Tom Hanks

Movies

Conditions on properties

(Movies in which Keanu Reeves played Neo)

Conditions on properties

(Movies in which Keanu Reeves played Neo)

Conditions based on comparisons

(Actors who worked with Tom and are older than he was)

Conditions based on comparisons

(Actors who worked with Tom and are older than he was)

Conditions based on patterns

(Actors who worked with Gene Hackman)

Conditions based on patterns

(Actors who worked with Gene and were directors of their own films)

Conditions based on patterns

Wildcard matching on properties

(Actors who worked with Keanu, but not when he was also working with Hugo)

Lab

Who are the five busiest actors?

Advanced lab

Recommend 3 actors that Keanu Reeves should work with (but hasn’t).

Coffee? Lunch?

Updating graphs with Cypher

Creating nodes

Lab

Insert a Movie with the title of “Mystic River” an a “released” year of 1993

Adding properties

Changing properties

Creating relationships

Lab

Set Kevin Bacon’s roles in Mystic River
from to “Sean Dev”

Lab

Change Kevin Bacon’s role in Mystic River 
from “Sean Dev” to “Sean Devine”

Lab

Add Clint Eastwood as the director of Mystic River

Lab

List all the characters in the movie “The Matrix”

Deleting nodes

(Wrong Error Message)

Deleting relationships

Deleting nodes and relationships

(delete all content in the database)

Lab

Add KNOWS relationships between all actors who were in the same movie

Match or Create is MERGE

Uses indexes, constraints and locks to guarantee unique lookup and creation

Match or Create is MERGE

Coffee?

More Cypher!

Matching multiple relationships

(Create KNOWS relationships between anyone, Actors or Directors, who worked together)

Variable length paths

The pattern

  • a
  • b
  • a
  • b
  • a
  • b

(a)-[*1..3]→(b)

Friends-of-Friends

Lab

Return Friends-of-Friends of Keanu Reeves who are not immediate friends

Bacon Number!

Lab

Return the names of the people joining Charlize to Kevin.

Case Studies

Customers

customers

Cisco

cs cisco

Cisco

cs cisco hmp

Accenture

cs accenture

Adobe

cs adobe

GLassdor

cs glassdoor

HP

cs hp

Junisphere

cs junisphere

Maaii

cs maaii

Seven Bridges Genomics

cs seven bridges

SFR

cs sfr

Teachscape

cs teachscape

Deutsche Telekom

cs telekom

Telenor

cs telenor

Viadeo

cs viadeo

Next steps

The rise of the graph

It’s not just social

Find the graphs in your domain,
and model them

Get involved in the community

Graphgists

http://gist.neo4j.org

StackOverflow

so

http://stackoverflow.com/questions/tagged/neo4j

Neo4j Google Group

google group

http://groups.google.com/group/neo4j

Github Issues and code

github

https://github.com/neo4j/neo4j/issues

Meetups/User groups

meetups

http://neo4j.meetup.com/

The Graphistas map

graphistas map

http://www.neo4j.org/participate/contributors#map

(Thank You)

(Graphs are everywhere)

/

#

Status of the presenation

Cypher queries execution