application modeling with graph databases

28
Application Modeling with Graph Databases http://joind.in/6694

Upload: josh-adell

Post on 16-May-2015

6.003 views

Category:

Technology


1 download

TRANSCRIPT

Page 1: Application Modeling with Graph Databases

Application Modelingwith Graph Databases

http://joind.in/6694 

Page 2: Application Modeling with Graph Databases

@josh_adell

• Software developer: PHP, Javascript, SQL• http://www.dunnwell.com• http://blog.everymansoftware.com

• http://github.com/jadell/neo4jphp• http://frostymug.herokuapp.com

Page 3: Application Modeling with Graph Databases

The Problem

Page 4: Application Modeling with Graph Databases

The Solution?

> -- First degree> SELECT actor_name FROM cast WHERE movie_title IN (SELECT DISTINCT movie_title FROM cast WHERE actor_name='Kevin Bacon')

> -- Second degree> SELECT actor_name FROM cast WHERE movie_title IN (SELECT DISTINCT movie_title FROM cast WHERE actor_name IN (SELECT actor_name FROM cast WHERE movie_title IN (SELECT DISTINCT movie_title FROM cast WHERE actor_name='Kevin Bacon')))

> -- Third degree> SELECT actor_name FROM cast WHERE movie_title IN(SELECT DISTINCT movie_title FROM cast WHERE actor_name IN (SELECT actor_name FROM cast WHERE movie_title IN (SELECT DISTINCT movie_title FROM cast WHERE actor_name IN (SELECT actor_name FROM cast WHERE movie_title IN (SELECT DISTINCT movie_title FROM cast WHERE actor_name='Kevin Bacon'))))

Page 5: Application Modeling with Graph Databases

The Truth

Relational databases aren't very good with relationships

Data

RDBMs

Page 6: Application Modeling with Graph Databases

RDBs Use Set Math

Page 7: Application Modeling with Graph Databases

Try again?

Page 8: Application Modeling with Graph Databases

Right Tool for the Job

=

Page 9: Application Modeling with Graph Databases
Page 10: Application Modeling with Graph Databases

Warning: Computer Science Ahead

A graph is an ordered pair G = (V, E)

where V is a set of vertices and

E is a set of edges,which are pairs of vertices in V.

Page 11: Application Modeling with Graph Databases

Graphs are Everywhere

Page 12: Application Modeling with Graph Databases

Relational Databases are Graphs!

Page 13: Application Modeling with Graph Databases

Everything is connected

Page 14: Application Modeling with Graph Databases

Some Graph Use Cases

• Social networking• Manufacturing• Map directions• Geo-spatial algorithms• Fraud detection• Multi-tenancy• Dependency mapping• Bioinformatics• Natural language processing

Page 15: Application Modeling with Graph Databases

Graphs are "Whiteboard-Friendly"

Nouns => nodes, Verbs => relationships

Page 16: Application Modeling with Graph Databases

Back to Bacon

START s=node:actors(name="Keanu Reeves"),      e=node:actors(name="Kevin Bacon")

MATCH p = shortestPath( s-[*]-e )

RETURN p, length(p)

http://tinyurl.com/c65d99w

Page 17: Application Modeling with Graph Databases

ACL

• Users can belong to groups• Groups can belong to groups• Groups and users have permissions on objects

o reado writeo denied

Page 18: Application Modeling with Graph Databases

START u=node:users(name="User 3")MATCH u-[:belongs_to*]->gRETURN g

                                               http://tinyurl.com/cyn3rkx

Page 19: Application Modeling with Graph Databases

START u=node:users(name="User 2"),      o=node:objects(name="Home")MATCH u-[:belongs_to*0..]->g,      g-[:can_read]->oRETURN g                                               http://tinyurl.com/dx7onro

Page 20: Application Modeling with Graph Databases

START u=node:users(name="User 3"),      o=node:objects(name="Users 1 Blog")MATCH u-[:belongs_to*0..]->g,      g-[:can_read]->o,      u-[d?:denied*]->oWHERE d is nullRETURN g

http://tinyurl.com/bwtyhvt

Page 21: Application Modeling with Graph Databases

Real Life Example

• Companies have brands, locations, location groups• Brands have locations, location groups• Location groups have locations

Page 22: Application Modeling with Graph Databases

START c=node:companies(name="Company 1")MATCH c-[:HAS*]->lWHERE l.type = 'location'RETURN l ORDER BY l.name

http://tinyurl.com/cxm4heh

Page 23: Application Modeling with Graph Databases

START b=node:brands(name="Brand 1")MATCH b<-[:HAS*]-c-[:HAS*]->l<-[h?:HAS*]-bWHERE h IS NULL AND l.type='location'RETURN l ORDER BY l.name

http://tinyurl.com/cl537w6

Page 24: Application Modeling with Graph Databases

Tweet

@chicken_tech

we should be using graph dbs!

Page 25: Application Modeling with Graph Databases

But Wait...There's More!

• Mutating Cypher (insert, update)• Indexing (auto, full-text, spatial)• Batches and Transactions• Embedded (for JVM) or REST

Page 26: Application Modeling with Graph Databases

Where fore art thou, RDB?

• Aggregation• Ordered data• Truly tabular data• Few or clearly defined relationships

Page 27: Application Modeling with Graph Databases

Questions?

Page 28: Application Modeling with Graph Databases

Resources

• http://joind.in/6694

• http://neo4j.org• http://docs.neo4j.org• http://www.youtube.com/watch?v=UodTzseLh04

• http://github.com/jadell/neo4jphp

• http://joshadell.com• [email protected]• @josh_adell• Google+, Facebook, LinkedIn