Date post: | 16-May-2015 |
Category: |
Technology |
Upload: | josh-adell |
View: | 5,998 times |
Download: | 1 times |
Application Modelingwith Graph Databases
http://joind.in/6694
@josh_adell
• Software developer: PHP, Javascript, SQL• http://www.dunnwell.com• http://blog.everymansoftware.com
• http://github.com/jadell/neo4jphp• http://frostymug.herokuapp.com
The Problem
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'))))
The Truth
Relational databases aren't very good with relationships
Data
RDBMs
RDBs Use Set Math
Try again?
Right Tool for the Job
=
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.
Graphs are Everywhere
Relational Databases are Graphs!
Everything is connected
Some Graph Use Cases
• Social networking• Manufacturing• Map directions• Geo-spatial algorithms• Fraud detection• Multi-tenancy• Dependency mapping• Bioinformatics• Natural language processing
Graphs are "Whiteboard-Friendly"
Nouns => nodes, Verbs => relationships
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
ACL
• Users can belong to groups• Groups can belong to groups• Groups and users have permissions on objects
o reado writeo denied
START u=node:users(name="User 3")MATCH u-[:belongs_to*]->gRETURN g
http://tinyurl.com/cyn3rkx
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
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
Real Life Example
• Companies have brands, locations, location groups• Brands have locations, location groups• Location groups have locations
START c=node:companies(name="Company 1")MATCH c-[:HAS*]->lWHERE l.type = 'location'RETURN l ORDER BY l.name
http://tinyurl.com/cxm4heh
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
Tweet
@chicken_tech
we should be using graph dbs!
But Wait...There's More!
• Mutating Cypher (insert, update)• Indexing (auto, full-text, spatial)• Batches and Transactions• Embedded (for JVM) or REST
Where fore art thou, RDB?
• Aggregation• Ordered data• Truly tabular data• Few or clearly defined relationships
Questions?
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