use neo4j in your next java project

34
Use Neo4j In Your Next Java Project Love relationships again Tobias Coetzee @ tobiascode

Upload: tobias-coetzee

Post on 10-Feb-2017

79 views

Category:

Technology


0 download

TRANSCRIPT

Page 1: Use Neo4j In Your Next Java Project

Use Neo4j In Your Next Java Project

Love relationships again

Tobias Coetzee

@tobiascode

Page 2: Use Neo4j In Your Next Java Project

All Roads Lead To SQL

Do we even have

other choices?

Every project we work on will have

some form of database, 99% of

those in the enterprise will be a

relational database

Page 3: Use Neo4j In Your Next Java Project

Impedance MismatchThe database model does not

match the domain model

Not ScalingMore data means less speed

Complex QueriesLots of joins and some self

joining nightmares as a bonus

Relationship Problems

Page 4: Use Neo4j In Your Next Java Project

Facebook For Superheroes

MEMBER_OF

TOO

K_PA

RT_INW

AS_I

N

Characters

Events

Teams

Stories

CONSISTS_OF

FR

IEN

D_O

F

Page 5: Use Neo4j In Your Next Java Project

Queries with lots of joins to get to the final answer

Similar Data Models

Configuration

Security

Navigation

Page 6: Use Neo4j In Your Next Java Project

Graph Databases

Just MathematicsBased on graph theory, created

by Leonhard Euler

All About RelationshipsIn graph databases relationships

are first class citizens

Neo4jMost well known and widely

used graph database

Page 7: Use Neo4j In Your Next Java Project

Graph Databases

NodesRepresent the entities or

records in the database

RelationshipsLink nodes together

PropertiesNodes and relationships can

also have properties

Page 8: Use Neo4j In Your Next Java Project

MEM

BER

_OF

MEM

BER

_OF

FRIEND_OF

WA

S_I

N

TOOK_PART_IN • Eyes: Blue

• Hair: None

• Citizenship: Canada• Since: 14 Feb 2001

Graph Databases

Page 9: Use Neo4j In Your Next Java Project

The database model does not

match the domain model

Impedance Mismatch

Page 10: Use Neo4j In Your Next Java Project

Impedance MismatchCharacter_Team

Character_Event

Team_Event

Character_Story

Team_Story

Event_Story

FriendsMarvelCharacter

Team

Event

Story

Page 11: Use Neo4j In Your Next Java Project

Traditional Solutions

ORM Frameworks, e.g.

Hibernate or Spring Data

Boilerplate code generation

tools

Impedance Mismatch

Page 12: Use Neo4j In Your Next Java Project

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: Use Neo4j In Your Next Java Project

MATCH

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

WHERE t.name = 'Avengers'

RETURN c.name

Cypher Intro

Page 14: Use Neo4j In Your Next Java Project

Demo Time

Page 15: Use Neo4j In Your Next Java Project

Lots of joins and some self

joining nightmares as a bonus

Complex Queries

Page 16: Use Neo4j In Your Next Java Project

Denormalise the database

Hide complexity behind

views and stored procedures

Traditional Solutions

Complex Queries

Page 17: Use Neo4j In Your Next Java Project

Friends of Friends

Name Number Friends

in Common

Vision 51

Wolfsbane 38

Punisher 33

Rage 38

Shard 25

Wind Dancer 25

Possible friend recommendations for Deadpool

Page 18: Use Neo4j In Your Next Java Project

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

Sooo Many Joins

Page 19: Use Neo4j In Your Next Java Project

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

Sooo Many Joins

Page 20: Use Neo4j In Your Next Java Project

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

Sooo Many Joins

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 21: Use Neo4j In Your Next Java Project

Quick Demo

Page 22: Use Neo4j In Your Next Java Project

How Do You Know Him?

MEM

BER

_OF

MEM

BER

_OF

FRIEND_OF

How can Deadpool connect to Ironman?

Page 23: Use Neo4j In Your Next Java Project

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

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

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

RETURN path

Shortest Path

Page 24: Use Neo4j In Your Next Java Project

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

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

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

RETURN path

Shortest Path

Page 25: Use Neo4j In Your Next Java Project

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

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

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

RETURN path

Shortest Path

Page 26: Use Neo4j In Your Next Java Project

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

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

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

RETURN path

Shortest Path

Page 27: Use Neo4j In Your Next Java Project

Quick Demo

Page 28: Use Neo4j In Your Next Java Project

Not ScalingMore data means less speed

Page 29: Use Neo4j In Your Next Java Project

Traditional Solutions

Offline databases

Batch runs to

process data

More indexes

Not Scaling

Page 30: Use Neo4j In Your Next Java Project

Speed

Embedded ServerProcessing requires reading the

same data you wrote

Fixed Size RecordsAll records of the same type

have the same size on disk

Index-Free AdjacencyPointer to the next node

Page 31: Use Neo4j In Your Next Java Project

Demo Time

Page 32: Use Neo4j In Your Next Java Project

Wrong UsageThis is not a hammer

Page 33: Use Neo4j In Your Next Java Project

Wrong Usage

Set OrientatedLists of things with few or no

joins

Global OperationsMade for local graph

operations

Aggregate QueriesProcessing requires reading the

same data you wrote

Page 34: Use Neo4j In Your Next Java Project

Thank YouFor Staying Till The End!

@tobiascode