application modeling with graph databases - relationships are cool

26
Application Modeling with Graph Databases – Relationships are cool! Lars Martin, JUG Saxony Day, 04.04.2014

Upload: lars-martin

Post on 27-Aug-2014

1.085 views

Category:

Software


1 download

DESCRIPTION

My slides on Application Modeling with Graph Databases at JUG Saxony Day 2014.

TRANSCRIPT

Page 1: Application Modeling with Graph Databases - Relationships are cool

Application Modeling with Graph Databases – Relationships are cool!

Lars Martin, JUG Saxony Day, 04.04.2014

Page 2: Application Modeling with Graph Databases - Relationships are cool

Application Modeling with Graph Databases – Relationships are cool!

§  Developer, Architect, Consultant

§  Java Enterprise, Eclipse, Continuous …

§  Entrepreneur since 1998

Who’s the guy?

Page 3: Application Modeling with Graph Databases - Relationships are cool

Agenda

² Status Quo

² SQL Join Hell

² Graph Basics

² Application Modeling

Application Modeling with Graph Databases – Relationships are cool!

Page 4: Application Modeling with Graph Databases - Relationships are cool

Application Modeling with Graph Databases – Relationships are cool!

Status Quo

http://db-engines.com/en/ranking_definition

Page 5: Application Modeling with Graph Databases - Relationships are cool

Application Modeling with Graph Databases – Relationships are cool!

SQL Join Hell (1)

Customer Id Name Address

1 Robert 3 2 Lars 7 3 Michael 23

Address Id Location

3 Berlin 4 Munich 7 Dresden

23 Leipzig

CustomerAddress CId AId

1 3 2 7 2 8 3 23

Address Id Location

3 Berlin 7 Dresden 8 New York

23 Leipzig

Address Id Customer Location

3 1 Berlin 7 2 Dresden 8 2 New York

23 3 Leipzig

Customer Id Name

1 Robert 2 Lars 3 Michael

1:1 Relationship

m:n Relationship

1:n Relationship

Customer Id Name

1 Robert 2 Lars 3 Michael

Page 6: Application Modeling with Graph Databases - Relationships are cool

Application Modeling with Graph Databases – Relationships are cool!

SQL Join Hell (2)

Page 7: Application Modeling with Graph Databases - Relationships are cool

Application Modeling with Graph Databases – Relationships are cool!

SQL Join Hell (3)

­ all JOINs are executed every time you query (traverse) the relationship

­ executing a JOIN means to search for a key in another table

­ with Indices executing a JOIN means to lookup a key

­ B-Tree Index: O(log(n))

­ more entries è more lookups è slower JOINs

Page 8: Application Modeling with Graph Databases - Relationships are cool
Page 9: Application Modeling with Graph Databases - Relationships are cool

Application Modeling with Graph Databases – Relationships are cool!

Graphs – a Crash Course in Coolness

G = (V, E) G

raph

Vert

ex

Edge

h"p://de.wikipedia.org/wiki/Graph_(Graphentheorie)  

Page 10: Application Modeling with Graph Databases - Relationships are cool

Application Modeling with Graph Databases – Relationships are cool!

Graphs – a Crash Course in Coolness

Page 11: Application Modeling with Graph Databases - Relationships are cool

Application Modeling with Graph Databases – Relationships are cool!

Graphs – a Crash Course in Coolness

Vertices •  unique identifier •  outgoing edges •  incoming edges •  key/value pairs

Edges •  unique identifier •  start vertex •  end vertex •  type •  key/value pairs

https://github.com/tinkerpop/gremlin/wiki/Defining-a-Property-Graph

index-free adjacency (≈ O(1))

Page 12: Application Modeling with Graph Databases - Relationships are cool
Page 13: Application Modeling with Graph Databases - Relationships are cool

Application Modeling with Graph Databases – Relationships are cool!

Real World Graphs (1)

maps closely to the data model noun = vertex verb = edge

Page 14: Application Modeling with Graph Databases - Relationships are cool

Application Modeling with Graph Databases – Relationships are cool!

Real World Graphs (2)

“whiteboard” friendly

Page 15: Application Modeling with Graph Databases - Relationships are cool

Application Modeling with Graph Databases – Relationships are cool!

Ø  Social: Facebook, Twitter, LinkedIn

Ø  Recommendations: Amazon, MovieDB

Ø  Logistics: Package Routing

Ø  Financial: Fraud Detection

Ø  Software: Dependency Management

Ø  Authorization & Access Control:

Ø  …

Real World Graphs (3)

Page 16: Application Modeling with Graph Databases - Relationships are cool

Application Modeling with Graph Databases – Relationships are cool!

Ø  Social: Facebook, Twitter, LinkedIn

Ø  Recommendations: Amazon, MovieDB

Ø  Logistics: Package Routing

Ø  Financial: Fraud Detection

Ø  Software: Dependency Management

Ø  Authorization & Access Control:

Ø  …

Real World Graphs (3)

our area of activities

Page 17: Application Modeling with Graph Databases - Relationships are cool
Page 18: Application Modeling with Graph Databases - Relationships are cool

Application Modeling with Graph Databases – Relationships are cool!

Application Modeling

Spring Data – projects.spring.io/spring-data

ü  Spring Data Neo4j supports property graph features

XO - eXtended Objects – github.com/buschmais/xo

ü  Lightweight Datastore-agnostic ORM

ü  Implementation of “Composite Pattern”

ü  Interface based (no POJOS), multiple inheritance

ü  XO-Neo4j supports property graph features

Page 19: Application Modeling with Graph Databases - Relationships are cool

Application Modeling with Graph Databases – Relationships are cool!

Application Modeling

Page 20: Application Modeling with Graph Databases - Relationships are cool

Application Modeling with Graph Databases – Relationships are cool!

Application Modeling

@Labelpublic interface User { @Indexed(unique = true) String getName(); void setName(String name); @Relation("POSTED") @Outgoing Set<Tweet> getTweets(); @Relation("FOLLOWS") @Outgoing Set<User> getFollowing();}

@Labelpublic interface Tweet { @Indexed(unique = true) long getTweetId(); void setTweetId(long id); String getText(); void setText(String text); @Relation("POSTED") @Incoming User getSender(); void setSender(User sender); @Relation("MENTIONED") @Outgoing Set<User> getMentions();

…}

Page 21: Application Modeling with Graph Databases - Relationships are cool

MATCH(me)-[:POSTED]->(tweet)-[:MENTIONED]->(user)

WHEREme.name = 'Neo4j’ AND NOT (me)-[:FOLLOWS]->(user)

WITHuser ORDER BY user.name

RETURNDISTINCT user  

Application Modeling with Graph Databases – Relationships are cool!

Application Modeling

@ResultOf@Cypher("MATCH (me)-[:POSTED]->(tweet)-[:MENTIONED]->(user) WHERE id(me) = id({this}) AND NOT (me)-[:FOLLOWS]->(user) WITH user ORDER BY user.name RETURN DISTINCT user")Result<User> suggestFriends();  

Page 22: Application Modeling with Graph Databases - Relationships are cool

Demo

Application Modeling with Graph Databases – Relationships are cool!

Application Modeling

Page 23: Application Modeling with Graph Databases - Relationships are cool

Relational Database •  tabular data structures

Graph Database

•  connected data, esp. multiple degrees

•  schema-less

Application Modeling with Graph Databases – Relationships are cool!

Application Modeling

“A relational database may tell you the average age of everyone in this place …

… but a graph database will tell you who is most likely to buy you a beer.”

Page 24: Application Modeling with Graph Databases - Relationships are cool
Page 25: Application Modeling with Graph Databases - Relationships are cool

SMB GmbH – Plauenscher Ring 21 – D-01187 Dresden

Dipl.-Inf. Lars Martin +49-(0)173-64 24 461 [email protected]

Page 26: Application Modeling with Graph Databases - Relationships are cool