relationship counselling with neo4j

45
1 Relationship Counselling with Neo4j Tobias Coetzee @tobiascode

Upload: tobias-coetzee

Post on 10-Feb-2017

96 views

Category:

Technology


1 download

TRANSCRIPT

1

Relationship Counselling

with Neo4j

Tobias Coetzee

@tobiascode

2

All roads lead to SQL

Do we even have other choices?

3

Relationship problems

Impedance mismatch

Complex queries

Not scaling

4

SuperFace

MEMBER_OF

TOO

K_PA

RT_INW

AS_I

N

Characters

Events

Teams

Stories

CONSISTS_OF

FR

IEN

D_O

F

5

Queries with lots of joins to get to the final answer

Similar data models

Configuration

Security

Navigation

6

Graph databases

Just mathematics

All about relationships

Neo4j

7

Graph databases

Nodes

Relationships

Properties

8

Graph databases

MEM

BER

_OF

MEM

BER

_OF

FRIEND_OF

WA

S_I

N

TOOK_PART_IN

• Since: 14 Feb 2001

• Eyes: Blue

• Hair: None

• Citizenship: Canada

9

Impedance mismatch

10

Impedance mismatch

Character_Team

Character_Event

Team_Event

Character_Story

Team_Story

Event_Story

FriendsMarvelCharacter

Team

Event

Story

11

Impedance mismatch

Traditional methods

12

Impedance mismatch

MEMBER_OF

TOO

K_PA

RT_IN

WA

S_I

N

Characters

Events

Teams

Stories

CONSISTS_OF

FR

IEN

D_O

F

13

Cypher Intro

MATCH

(c:Character)-[:MEMBER_OF]->(t:Team)

WHERE t.name = 'Avengers'

RETURN c.name

14

Cypher Intro

MATCH

(c:Character)-[:MEMBER_OF]->(t:Team)

WHERE t.name = 'Avengers'

RETURN c.name

15

Cypher Intro

MATCH

(c:Character)-[:MEMBER_OF]->(t:Team)

WHERE t.name = 'Avengers'

RETURN c.name

16

Cypher Intro

MATCH

(c:Character)-[:MEMBER_OF]->(t:Team)

WHERE t.name = 'Avengers'

RETURN c.name

17

Cypher Intro

MATCH

(c:Character)-[:MEMBER_OF]->(t:Team)

WHERE t.name = 'Avengers'

RETURN c.name

18

Cypher Intro

MATCH

(c:Character)-[:MEMBER_OF]->(t:Team)

WHERE t.name = 'Avengers'

RETURN c.name

19

Cypher Intro

MATCH

(c:Character)-[:MEMBER_OF]->(t:Team)

WHERE t.name = 'Avengers'

RETURN c.name

20

Cypher Intro

MATCH

(c:Character)-[:MEMBER_OF]->(t:Team)

WHERE t.name = 'Avengers'

RETURN c.name

21

Cypher Intro

MATCH

(c:Character)-[:MEMBER_OF]->(t:Team)

WHERE t.name = 'Avengers'

RETURN c.name

22

Cypher Intro

MATCH

(c:Character)-[:MEMBER_OF]->(t:Team)

WHERE t.name = 'Avengers'

RETURN c.name

23

DEMO

3 Ways to query

Neo4j

24

Complex queries

25

Complex queries

Traditional methods

26

Friends of friends

Possible friend recommendations for Deadpool

Name Number friends in

common

Vision 51

Wolfsbane 38

Punisher 33

Rage 38

Shard 25

Wind Dancer 25

27

Sooo many joins

SELECT FriendOfFriend.Name, COUNT(*)

FROM MarvelCharacter deadpool

INNER JOIN Friends DeadpoolFriends

ON deadpool.Id = DeadpoolFriends.CharacterId1

INNER JOIN Friends FriendsFriends

ON DeadpoolFriends.CharacterId2 = FriendsFriends.CharacterId1

INNER JOIN MarvelCharacter FriendOfFriend

ON FriendsFriends.CharacterId2 = FriendOfFriend.Id

WHERE deadpool.Name = 'Deadpool'

AND FriendsFriends.CharacterId2 NOT IN( SELECT CharacterId2

FROM MarvelCharacter

INNER JOIN Friends

ON MarvelCharacter.Id = CharacterId1

WHERE Name = 'Deadpool')

GROUP BY FriendOfFriend.Name

ORDER BY COUNT(*) DESC

28

Sooo many joins

MATCH (deadpool:Character)-[:FRIEND_OF*2]->(FriendOfFriend)

WHERE deadpool.name = 'Deadpool'

AND NOT (deadpool)-[:FRIEND_OF]->(FriendOfFriend)

AND NOT deadpool = FriendOfFriend

RETURN FriendOfFriend.name, COUNT(*)

ORDER BY COUNT(*) DESC, FriendOfFriend.name

29

Sooo many joins

MATCH (deadpool:Character)-[:FRIEND_OF*2]->(FriendOfFriend)

FROM MarvelCharacter deadpool

INNER JOIN Friends DeadpoolFriends

ON deadpool.Id = DeadpoolFriends.CharacterId1

INNER JOIN Friends FriendsFriends

ON DeadpoolFriends.CharacterId2 = FriendsFriends.CharacterId1

INNER JOIN MarvelCharacter FriendOfFriend

ON FriendsFriends.CharacterId2 = FriendOfFriend.Id

30

DEMO

Friend of friend

results

31

How do you know him?

How can Deadpool connect to Ironman?

MEM

BER

_OF

MEM

BER

_OF

FRIEND_OF

32

Shortest path

MATCH (deadpool:Character { name:"Deadpool" }),

(other:Character { name:"Ultron" }),

path = shortestPath((deadpool)-[*]-(other))

RETURN path

33

Shortest path

MATCH (deadpool:Character { name:"Deadpool" }),

(other:Character { name:"Ultron" }),

path = shortestPath((deadpool)-[*]-(other))

RETURN path

34

Shortest path

MATCH (deadpool:Character { name:"Deadpool" }),

(other:Character { name:"Ultron" }),

path = shortestPath((deadpool)-[*]-(other))

RETURN path

35

Shortest path

MATCH (deadpool:Character { name:"Deadpool" }),

(other:Character { name:"Ultron" }),

path = shortestPath((deadpool)-[*]-(other))

RETURN path

36

Shortest path

MATCH (deadpool:Character { name:"Deadpool" }),

(other:Character { name:"Ultron" }),

path = shortestPath((deadpool)-[*]-(other))

RETURN path

37

DEMO

Shortest path results

38

Not scaling

39

Complex queries

Traditional methods

40

Speed

Index-free adjacency

Fixed size records

Embedded server

41

DEMO

Neo4j API’s

42

Wrong usage

43

Wrong usage

Set orientated

Global operations

Aggregate queries

44

questions?

vote

and

win

#esca

pe2016

@tobiascode