65
GraphAware TM by Michal Bachman a few best practices and lessons learned Modelling Data in Neo4j

Modelling Data as Graphs (Neo4j)

Embed Size (px)

DESCRIPTION

Modelling Data in Neo4j for beginners, common mistakes, frequently asked questions, hardware sizing and a few extra tips

Citation preview

Page 1: Modelling Data as Graphs (Neo4j)

GraphAwareTM

by Michal Bachman

a few best practices and lessons learned

Modelling Data in Neo4j

Page 2: Modelling Data as Graphs (Neo4j)

GraphAwareTM

Page 3: Modelling Data as Graphs (Neo4j)

GraphAwareTM

Ride-sharing website

History of rides

Friendships from Facebook

Aim: build trust between users

Example Domain

Page 4: Modelling Data as Graphs (Neo4j)

GraphAwareTM

There is no single correct way.

Modelling Data as Graphs

Page 5: Modelling Data as Graphs (Neo4j)

GraphAwareTM

Graphs are very whiteboard friendly.

Modelling Data as Graphs

Page 6: Modelling Data as Graphs (Neo4j)

ipsum

FRIEND_OF

name: “Michael”

User

name: “Laura”

User

FRIEND_OF

name: “Peter”

User

DROVE

name: “Alice”

User

DROVE

Page 7: Modelling Data as Graphs (Neo4j)

ipsum

FRIEND_OF

name: “Michael”

User

name: “Laura”

User

FRIEND_OF

name: “Peter”

User

DROVE

name: “Alice”

User

DROVE

name: “Jenny”

User

DROVE

Page 8: Modelling Data as Graphs (Neo4j)

ipsum

FRIEND_OF

name: “Michael”

User

name: “Laura”

User

FRIEND_OF

name: “Peter”

User

DROVE

name: “Alice”

User

DROVE

name: “Jenny”

User

DROVE

date: 2014-01-29date: 2014-01-29date: 2014-01-27

Page 9: Modelling Data as Graphs (Neo4j)

ipsum

FRIEND_OF

name: “Michael”

User

name: “Laura”

User

FRIEND_OF

name: “Peter”

User

DROVE

name: “Alice”

User

DROVE

name: “Jenny”

User

DROVE

date: 2014-01-29date: 2014-01-29date: 2014-01-27

RODE_TOGETHER

RODE_TOGETHER

Page 10: Modelling Data as Graphs (Neo4j)

ipsum

FRIEND_OF

name: “Michael”

User

name: “Laura”

User

FRIEND_OF

date: 2014-01-29from: “London”

to: “Nottingham”

RideDRIVER

name: “Alice”

User

PASSENGER

date: 2014-01-27from: “Brighton”

to: “Hastings”

Ride

PASSENGER

name: “Peter”

User

PASSENGER

name: “Jenny”

User

DRIVER

Page 11: Modelling Data as Graphs (Neo4j)

GraphAwareTM

Make important concepts in your domain nodes, you will gain flexibility.

Nodes vs. Relationships

Page 12: Modelling Data as Graphs (Neo4j)

ipsum

FRIEND_OF

name: “Michael”

User

name: “Laura”

User

FRIEND_OF

date: 2014-01-29from: “London”

to: “Nottingham”

RideDRIVER

name: “Alice”

User

PASSENGER

date: 2014-01-27from: “Brighton”

to: “Hastings”

Ride

PASSENGER

name: “Peter”

User

PASSENGER

name: “Jenny”

User

DRIVER

Page 13: Modelling Data as Graphs (Neo4j)

ipsum

FRIEND_OF

name: “Michael”

User

name: “Laura”

User

FRIEND_OF

date: 2014-01-29from: “London”

to: “Nottingham”

RideDRIVER

name: “Alice”

User

PASSENGER

date: 2014-01-27from: “Brighton”

to: “Hastings”

Ride

PASSENGER

name: “Peter”

User

PASSENGER

name: “Jenny”

User

DRIVER

RATEDrating: 5RATED

rating: 3

Page 14: Modelling Data as Graphs (Neo4j)

ipsum

FRIEND_OFname: “Michael”

User

name: “Laura”

User

date: 2014-01-29from: “London”

to: “Nottingham”

RideDRIVER

name: “Alice”

User

PASSENGER

date: 2014-01-27from: “Brighton”

to: “Hastings”

Ride

PASSENGER

name: “Peter”

User

PASSENGER

name: “Jenny”

User

DRIVER

RATEDrating: 5RATED

rating: 3

Page 15: Modelling Data as Graphs (Neo4j)

ipsum

FRIEND_OFname: “Michael”

User

name: “Laura”

User

date: 2014-01-29from: “London”

to: “Nottingham”

RideDRIVER

name: “Alice”

User

PASSENGER

date: 2014-01-27from: “Brighton”

to: “Hastings”

Ride

PASSENGER

name: “Peter”

User

PASSENGER

name: “Jenny”

User

DRIVER

RATEDrating: 5RATED

rating: 3

Page 16: Modelling Data as Graphs (Neo4j)

GraphAwareTM

a common mistake

Bidirectional Relationships

Page 17: Modelling Data as Graphs (Neo4j)

DEFEATEDCzech Republic

Sweden

GraphAwareTM

Ice Hockey

Page 18: Modelling Data as Graphs (Neo4j)

DEFEATEDCzech Republic

Sweden

GraphAwareTM

Ice Hockey

Page 19: Modelling Data as Graphs (Neo4j)

DEFEATED

Czech Republic

Sweden

DEFEATED_BY

GraphAwareTM

Ice Hockey (Implied Relationship)

Page 20: Modelling Data as Graphs (Neo4j)

DEFEATED

Czech Republic

Sweden

DEFEATED_BY

GraphAwareTM

Ice HockeyIce Hockey (Implied Relationship)

Page 21: Modelling Data as Graphs (Neo4j)

PARTNERNeo Technology GraphAware

PARTNERNeo Technology GraphAware

GraphAwareTM

Company Partnership (Naturally Bidirectional)

Page 22: Modelling Data as Graphs (Neo4j)

PARTNER

Neo Technology GraphAware

PARTNER

GraphAwareTM

Company Partnership (Naturally Bidirectional)

Page 23: Modelling Data as Graphs (Neo4j)

PARTNER

Neo Technology GraphAware

PARTNER

GraphAwareTM

Company Partnership (Naturally Bidirectional)

Page 24: Modelling Data as Graphs (Neo4j)

Neo Technology GraphAware

PARTNER

GraphAwareTM

Company Partnership (Naturally Bidirectional)

Page 25: Modelling Data as Graphs (Neo4j)

Neo Technology GraphAware

PARTNER

GraphAwareTM

Company Partnership (Naturally Bidirectional)

Page 26: Modelling Data as Graphs (Neo4j)

GraphAwareTM

In Neo4j, the speed of traversal does not depend on the direction of the relationships being traversed.

Traversal Speed

Page 27: Modelling Data as Graphs (Neo4j)

GraphAwareTM

Why?

GraphAwareTM

Page 28: Modelling Data as Graphs (Neo4j)

GraphAwareTM

Node Record in the Node Store (9 bytes), first bit = inUse flag

Relationship Record in the Relationship Store (33 bytes), first bit = inUse flag, second bit unused

next relationship

(35 bits)

next property (36 bits)

first node(35 bits)

second node (35 bits)

type(16 bits)

first node's previous

relationship (35 bits)

first node's next

relationship (35 bits)

second node's first relationship

(35 bits)

second node's next relationship

(35 bits)

next property (36 bits)

Neo4j Data Layout

Page 29: Modelling Data as Graphs (Neo4j)

GraphAwareTM

Neo4j APIs allow developers to completely ignore relationship direction when querying the graph.

Traversal APIs

Page 30: Modelling Data as Graphs (Neo4j)

GraphAwareTM

MATCH  (neo)-­‐[:PARTNER]-­‐>(partner)

Cypher

Page 31: Modelling Data as Graphs (Neo4j)

GraphAwareTM

MATCH  (neo)<-­‐[:PARTNER]-­‐(partner)

Cypher

Page 32: Modelling Data as Graphs (Neo4j)

GraphAwareTM

MATCH  (neo)-­‐[:PARTNER]-­‐(partner)

Cypher

Page 33: Modelling Data as Graphs (Neo4j)

GraphAwareTM

Different quality in each direction => should have two relationships!

Heads Up!

LOVES

Geeky Guy Girl

DOESN’T CARE ABOUT

Page 34: Modelling Data as Graphs (Neo4j)

ipsum

FRIEND_OFname: “Michael”

User

name: “Laura”

User

date: 2014-01-29from: “London”

to: “Nottingham”

RideDRIVER

name: “Alice”

User

PASSENGER

date: 2014-01-27from: “Brighton”

to: “Hastings”

Ride

PASSENGER

name: “Peter”

User

PASSENGER

name: “Jenny”

User

DRIVER

RATEDrating: 5RATED

rating: 3

Page 35: Modelling Data as Graphs (Neo4j)

ipsum

FRIEND_OFname: “Michael”

User

name: “Laura”

User

date: 2014-01-29from: “London”

to: “Nottingham”

RideDRIVER

name: “Alice”

User

PASSENGER

date: 2014-01-27from: “Brighton”

to: “Hastings”

Ride

PASSENGER

name: “Peter”

User

PASSENGER

name: “Jenny”

User

DRIVER

RATEDrating: ?RATED

rating: 3

HATEDDISLIKEDNEUTRALLIKEDLOVED

Page 36: Modelling Data as Graphs (Neo4j)

FRIEND_OFname: “Michael”

User

name: “Laura”

User

date: 2014-01-29from: “London”

to: “Nottingham”

RideDRIVER

name: “Alice”

User

PASSENGER

date: 2014-01-27from: “Brighton”

to: “Hastings”

Ride

PASSENGER

name: “Peter”

User

PASSENGER

name: “Jenny”

User

DRIVER

LOVEDNEUTRAL

Page 37: Modelling Data as Graphs (Neo4j)

GraphAwareTM

performance comparison

Qualifying Relationships

Page 38: Modelling Data as Graphs (Neo4j)

ipsum

FRIEND_OFname: “Michael”

User

name: “Laura”

User

date: 2014-01-29from: “London”

to: “Nottingham”

RideDRIVER

name: “Alice”

User

PASSENGER

date: 2014-01-27from: “Brighton”

to: “Hastings”

Ride

PASSENGER

name: “Peter”

User

PASSENGER

name: “Jenny”

User

DRIVER

RATEDrating: 5RATED

rating: 3

Qualifying by Properties

Page 39: Modelling Data as Graphs (Neo4j)

GraphAwareTM

START      ride=node({id})  MATCH      (ride)<-­‐[r:RATED]-­‐(passenger)  WHERE      r.rating  >  3  RETURN    passenger

Who liked the ride? (Cypher)

Page 40: Modelling Data as Graphs (Neo4j)

GraphAwareTM

for  (Relationship  r  :  ride.getRelationships(INCOMING,  RATED))    {          if  ((int)  r.getProperty("rating")  >  3)            {                  Node  passenger  =  r.getStartNode();  //do  something  with  it          }  }

Who liked the ride? (Java)

Page 41: Modelling Data as Graphs (Neo4j)

FRIEND_OFname: “Michael”

User

name: “Laura”

User

date: 2014-01-29from: “London”

to: “Nottingham”

RideDRIVER

name: “Alice”

User

PASSENGER

date: 2014-01-27from: “Brighton”

to: “Hastings”

Ride

PASSENGER

name: “Peter”

User

PASSENGER

name: “Jenny”

User

DRIVER

LOVEDNEUTRAL

Qualifying by Relationship Type

Page 42: Modelling Data as Graphs (Neo4j)

GraphAwareTM

START      ride=node({id})  MATCH      (ride)<-­‐[r:LIKED|LOVED]-­‐(passenger)  RETURN    passenger

Who liked the ride? (Cypher)

Page 43: Modelling Data as Graphs (Neo4j)

GraphAwareTM

for  (Relationship  r  :  ride.getRelationships(INCOMING,  LIKED,  LOVED))    {          Node  passenger  =  r.getStartNode();  //do  something  with  it  }

Who liked the ride? (Java)

Page 44: Modelling Data as Graphs (Neo4j)

GraphAwareTM

Page 45: Modelling Data as Graphs (Neo4j)

GraphAwareTM

Page 46: Modelling Data as Graphs (Neo4j)

FRIEND_OFname: “Michael”

User

name: “Laura”

User

date: 2014-01-29from: “London”

to: “Nottingham”

RideDRIVER

name: “Alice”

User

PASSENGER

date: 2014-01-27from: “Brighton”

to: “Hastings”

Ride

PASSENGER

name: “Peter”

User

PASSENGER

name: “Jenny”

User

DRIVER

LOVEDNEUTRAL

Winner!

Page 47: Modelling Data as Graphs (Neo4j)

Other interesting info?

Page 48: Modelling Data as Graphs (Neo4j)

GraphAwareTM

frequently asked question

Hardware Sizing

Page 49: Modelling Data as Graphs (Neo4j)

GraphAwareTM

HDD

Record Files

Transaction Log

Operating System

JVM

Neo4j

Object Cache

Core API

Other APIs

TransactionManagement

File System Cache

Node

s

Rela

tions

hips

Prop

ertie

s

Rela

tions

hip

Type

s

Neo4j Architecture

Page 50: Modelling Data as Graphs (Neo4j)

GraphAwareTM

>  cd  data  >  ls  -­‐ah

Disk Space

Page 51: Modelling Data as Graphs (Neo4j)

GraphAwareTM

drwxr-­‐xr-­‐x      5  bachmanm    wheel      170B  19  Oct  12:56  index  -­‐rw-­‐r-­‐-­‐r-­‐-­‐      1  bachmanm    wheel        31K  19  Oct  12:56  messages.log  -­‐rw-­‐r-­‐-­‐r-­‐-­‐      1  bachmanm    wheel        69B  19  Oct  12:56  neostore  -­‐rw-­‐r-­‐-­‐r-­‐-­‐      1  bachmanm    wheel          9B  19  Oct  12:56  neostore.id  -­‐rw-­‐r-­‐-­‐r-­‐-­‐      1  bachmanm    wheel      8.8K  19  Oct  12:56  neostore.nodestore.db  -­‐rw-­‐r-­‐-­‐r-­‐-­‐      1  bachmanm    wheel          9B  19  Oct  12:56  neostore.nodestore.db.id  -­‐rw-­‐r-­‐-­‐r-­‐-­‐      1  bachmanm    wheel        39M  19  Oct  12:56  neostore.propertystore.db  -­‐rw-­‐r-­‐-­‐r-­‐-­‐      1  bachmanm    wheel      153B  19  Oct  12:56  neostore.propertystore.db.arrays  -­‐rw-­‐r-­‐-­‐r-­‐-­‐      1  bachmanm    wheel          9B  19  Oct  12:56  neostore.propertystore.db.arrays.id  -­‐rw-­‐r-­‐-­‐r-­‐-­‐      1  bachmanm    wheel          9B  19  Oct  12:56  neostore.propertystore.db.id  -­‐rw-­‐r-­‐-­‐r-­‐-­‐      1  bachmanm    wheel        43B  19  Oct  12:56  neostore.propertystore.db.index  -­‐rw-­‐r-­‐-­‐r-­‐-­‐      1  bachmanm    wheel          9B  19  Oct  12:56  neostore.propertystore.db.index.id  -­‐rw-­‐r-­‐-­‐r-­‐-­‐      1  bachmanm    wheel      140B  19  Oct  12:56  neostore.propertystore.db.index.keys  -­‐rw-­‐r-­‐-­‐r-­‐-­‐      1  bachmanm    wheel          9B  19  Oct  12:56  neostore.propertystore.db.index.keys.id  -­‐rw-­‐r-­‐-­‐r-­‐-­‐      1  bachmanm    wheel      154B  19  Oct  12:56  neostore.propertystore.db.strings  -­‐rw-­‐r-­‐-­‐r-­‐-­‐      1  bachmanm    wheel          9B  19  Oct  12:56  neostore.propertystore.db.strings.id  -­‐rw-­‐r-­‐-­‐r-­‐-­‐      1  bachmanm    wheel        31M  19  Oct  12:56  neostore.relationshipstore.db  -­‐rw-­‐r-­‐-­‐r-­‐-­‐      1  bachmanm    wheel          9B  19  Oct  12:56  neostore.relationshipstore.db.id  -­‐rw-­‐r-­‐-­‐r-­‐-­‐      1  bachmanm    wheel        38B  19  Oct  12:56  neostore.relationshiptypestore.db  -­‐rw-­‐r-­‐-­‐r-­‐-­‐      1  bachmanm    wheel          9B  19  Oct  12:56  neostore.relationshiptypestore.db.id  -­‐rw-­‐r-­‐-­‐r-­‐-­‐      1  bachmanm    wheel      140B  19  Oct  12:56  neostore.relationshiptypestore.db.names  -­‐rw-­‐r-­‐-­‐r-­‐-­‐      1  bachmanm    wheel          9B  19  Oct  12:56  neostore.relationshiptypestore.db.names.id

Disk Space

Page 52: Modelling Data as Graphs (Neo4j)

GraphAwareTM

Disk Space

node 14B

relationship 33B

property 41B

Page 53: Modelling Data as Graphs (Neo4j)

GraphAwareTM

Disk Space (Example)

1,000 nodes x 14B = 13.7 kB1,000,000 rels x 33B = 31.5 MB2,010,000 props x 41B = 78.6 MBTOTAL 110.1 MB

Page 54: Modelling Data as Graphs (Neo4j)

GraphAwareTM

How about low level cache? Any guesses?

Low Level Cache

Page 55: Modelling Data as Graphs (Neo4j)

GraphAwareTM

Same as disk space

Low Level Cache

Page 56: Modelling Data as Graphs (Neo4j)

GraphAwareTM

High Level Cache

node 344B

relationship 208B

property 116B

...

Page 57: Modelling Data as Graphs (Neo4j)

Other interesting info?

Page 58: Modelling Data as Graphs (Neo4j)

GraphAwareTM

Cypher is great!

Cypher is improving

But don’t be afraid of writing some Java

Java API vs. Cypher

Page 59: Modelling Data as Graphs (Neo4j)

GraphAwareTM

Experiment

Measure

Analyse

Ask

Conclusion

Page 60: Modelling Data as Graphs (Neo4j)

GraphAwareTM

www.graphaware.com @graph_aware

Thanks!

Page 61: Modelling Data as Graphs (Neo4j)

GraphAwareTM

Next  meetup

• The  transport  graph  – Roads,  Nodes  and  Automobiles (Jacqui  Read)  

– Transport  Network  Route  Finding  Using  A  Graph (Ian  Cartwright  &  Ben  Earlham)  

• 26th  February  2014  • Here!

Page 62: Modelling Data as Graphs (Neo4j)

GraphAwareTM

Page 63: Modelling Data as Graphs (Neo4j)

GraphAwareTM

Ian Robinson, Jim Webber & Emil Eifrem

Graph Databases

h

Compliments

of Neo Technology

Page 64: Modelling Data as Graphs (Neo4j)

GraphAwareTM

Take  me  to  the  pub…

Page 65: Modelling Data as Graphs (Neo4j)

GraphAwareTM

www.graphaware.com @graph_aware

Thanks!