Top Banner
1 Relationship Counselling with Neo4j Tobias Coetzee @tobiascode
45

Relationship Counselling with Neo4j

Feb 10, 2017

Download

Technology

Tobias Coetzee
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Relationship Counselling with Neo4j

1

Relationship Counselling

with Neo4j

Tobias Coetzee

@tobiascode

Page 2: Relationship Counselling with Neo4j

2

All roads lead to SQL

Do we even have other choices?

Page 3: Relationship Counselling with Neo4j

3

Relationship problems

Impedance mismatch

Complex queries

Not scaling

Page 4: Relationship Counselling with Neo4j

4

SuperFace

MEMBER_OF

TOO

K_PA

RT_INW

AS_I

N

Characters

Events

Teams

Stories

CONSISTS_OF

FR

IEN

D_O

F

Page 5: Relationship Counselling with Neo4j

5

Queries with lots of joins to get to the final answer

Similar data models

Configuration

Security

Navigation

Page 6: Relationship Counselling with Neo4j

6

Graph databases

Just mathematics

All about relationships

Neo4j

Page 7: Relationship Counselling with Neo4j

7

Graph databases

Nodes

Relationships

Properties

Page 8: Relationship Counselling with Neo4j

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

Page 9: Relationship Counselling with Neo4j

9

Impedance mismatch

Page 10: Relationship Counselling with Neo4j

10

Impedance mismatch

Character_Team

Character_Event

Team_Event

Character_Story

Team_Story

Event_Story

FriendsMarvelCharacter

Team

Event

Story

Page 11: Relationship Counselling with Neo4j

11

Impedance mismatch

Traditional methods

Page 12: Relationship Counselling with Neo4j

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

Page 13: Relationship Counselling with Neo4j

13

Cypher Intro

MATCH

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

WHERE t.name = 'Avengers'

RETURN c.name

Page 14: Relationship Counselling with Neo4j

14

Cypher Intro

MATCH

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

WHERE t.name = 'Avengers'

RETURN c.name

Page 15: Relationship Counselling with Neo4j

15

Cypher Intro

MATCH

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

WHERE t.name = 'Avengers'

RETURN c.name

Page 16: Relationship Counselling with Neo4j

16

Cypher Intro

MATCH

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

WHERE t.name = 'Avengers'

RETURN c.name

Page 17: Relationship Counselling with Neo4j

17

Cypher Intro

MATCH

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

WHERE t.name = 'Avengers'

RETURN c.name

Page 18: Relationship Counselling with Neo4j

18

Cypher Intro

MATCH

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

WHERE t.name = 'Avengers'

RETURN c.name

Page 19: Relationship Counselling with Neo4j

19

Cypher Intro

MATCH

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

WHERE t.name = 'Avengers'

RETURN c.name

Page 20: Relationship Counselling with Neo4j

20

Cypher Intro

MATCH

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

WHERE t.name = 'Avengers'

RETURN c.name

Page 21: Relationship Counselling with Neo4j

21

Cypher Intro

MATCH

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

WHERE t.name = 'Avengers'

RETURN c.name

Page 22: Relationship Counselling with Neo4j

22

Cypher Intro

MATCH

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

WHERE t.name = 'Avengers'

RETURN c.name

Page 23: Relationship Counselling with Neo4j

23

DEMO

3 Ways to query

Neo4j

Page 24: Relationship Counselling with Neo4j

24

Complex queries

Page 25: Relationship Counselling with Neo4j

25

Complex queries

Traditional methods

Page 26: Relationship Counselling with Neo4j

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

Page 27: Relationship Counselling with Neo4j

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

Page 28: Relationship Counselling with Neo4j

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

Page 29: Relationship Counselling with Neo4j

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

Page 30: Relationship Counselling with Neo4j

30

DEMO

Friend of friend

results

Page 31: Relationship Counselling with Neo4j

31

How do you know him?

How can Deadpool connect to Ironman?

MEM

BER

_OF

MEM

BER

_OF

FRIEND_OF

Page 32: Relationship Counselling with Neo4j

32

Shortest path

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

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

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

RETURN path

Page 33: Relationship Counselling with Neo4j

33

Shortest path

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

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

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

RETURN path

Page 34: Relationship Counselling with Neo4j

34

Shortest path

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

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

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

RETURN path

Page 35: Relationship Counselling with Neo4j

35

Shortest path

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

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

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

RETURN path

Page 36: Relationship Counselling with Neo4j

36

Shortest path

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

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

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

RETURN path

Page 37: Relationship Counselling with Neo4j

37

DEMO

Shortest path results

Page 38: Relationship Counselling with Neo4j

38

Not scaling

Page 39: Relationship Counselling with Neo4j

39

Complex queries

Traditional methods

Page 40: Relationship Counselling with Neo4j

40

Speed

Index-free adjacency

Fixed size records

Embedded server

Page 41: Relationship Counselling with Neo4j

41

DEMO

Neo4j API’s

Page 42: Relationship Counselling with Neo4j

42

Wrong usage

Page 43: Relationship Counselling with Neo4j

43

Wrong usage

Set orientated

Global operations

Aggregate queries

Page 44: Relationship Counselling with Neo4j

44

questions?

Page 45: Relationship Counselling with Neo4j

vote

and

win

#esca

pe2016

@tobiascode