Use Neo4j In Your Next Java Project

Post on 10-Feb-2017

79 views 0 download

Transcript of Use Neo4j In Your Next Java Project

Use Neo4j In Your Next Java Project

Love relationships again

Tobias Coetzee

@tobiascode

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

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

Facebook For Superheroes

MEMBER_OF

TOO

K_PA

RT_INW

AS_I

N

Characters

Events

Teams

Stories

CONSISTS_OF

FR

IEN

D_O

F

Queries with lots of joins to get to the final answer

Similar Data Models

Configuration

Security

Navigation

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

Graph Databases

NodesRepresent the entities or

records in the database

RelationshipsLink nodes together

PropertiesNodes and relationships can

also have properties

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

The database model does not

match the domain model

Impedance Mismatch

Impedance MismatchCharacter_Team

Character_Event

Team_Event

Character_Story

Team_Story

Event_Story

FriendsMarvelCharacter

Team

Event

Story

Traditional Solutions

ORM Frameworks, e.g.

Hibernate or Spring Data

Boilerplate code generation

tools

Impedance Mismatch

Impedance Mismatch

MEMBER_OF

TOO

K_PA

RT_IN

WA

S_I

N

Characters

Events

Teams

Stories

CONSISTS_OF

FR

IEN

D_O

F

MATCH

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

WHERE t.name = 'Avengers'

RETURN c.name

Cypher Intro

Demo Time

Lots of joins and some self

joining nightmares as a bonus

Complex Queries

Denormalise the database

Hide complexity behind

views and stored procedures

Traditional Solutions

Complex Queries

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

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

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

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

Quick Demo

How Do You Know Him?

MEM

BER

_OF

MEM

BER

_OF

FRIEND_OF

How can Deadpool connect to Ironman?

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

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

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

RETURN path

Shortest Path

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

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

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

RETURN path

Shortest Path

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

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

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

RETURN path

Shortest Path

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

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

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

RETURN path

Shortest Path

Quick Demo

Not ScalingMore data means less speed

Traditional Solutions

Offline databases

Batch runs to

process data

More indexes

Not Scaling

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

Demo Time

Wrong UsageThis is not a hammer

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

Thank YouFor Staying Till The End!

@tobiascode