Graph Databases
A little connected tour
!
@fcofdezc
Francisco Fernández Castaño
@fcofdezc Sw Engineer @biicode
Beginning
The old town of Königsberg has seven bridges:
Can you take a walk through town, visiting each part of
the town and crossin...
El origenG = (V, E)
What is a Graph DB?
Graph
Nodes Relationships
Properties
Store
Store
Connect
Have
Have
Written in Java
ACID
Rest interface
Cypher
Why NOSQL?
The value of Relational Databases
Ventajas de BD Relacionales
Concurrencia
Persistencia
Integración
Estándar
Persistence
Ventajas de BD Relacionales
Concurrencia
Persistencia
Integración
Estándar
Concurrency
Ventajas de BD Relacionales
Concurrencia
Persistencia
Integración
Estándar
Integration
Ventajas de BD Relacionales
Concurrencia
Persistencia
Integración
Estándar
Standard
inconveniences Relational DBs
El OrigenImpedance Mismatch
class Client < ActiveRecord::Base	
has_one :address	
has_many :orders	
has_and_belongs_to_many :roles	
end
DesVentajas de BD Relacionales
Fricción!
Interoperabilidad
Adaptación al cambio
Escalabilidad
No está destinada para ciert...
Adaptation to changes
!Scalability
The traditional way in the context
of connected data is artificial
Depth MySQL time (s) Neo4j time (s) Results
2 0.016 0.01 ~2500
3 30.267 0.168 ~110,000
4 1543.505 1.359 ~600,000
5 No Acab...
Person
Id Person
1 Frank
2 John
.. …
99 Alice
PersonFriend
PersonID FriendID
1 2
2 1
.. …
99 2
O(log n)
O(1)
O(m log n)
O(m)
We can transform our domain model in a
natural way
Use cases
Social Networks
Follow
Follow
John Jeff
Douglas
Geospatial problems
Fraud detection
Authorization
Network management
Cypher
Declarative language
ASCII oriented
Pattern matching
Cypher
Cypher
Traverser API
Core API
Kernel
Cypher
a b
(a)-->(b)
Cypher
clapton cream
(clapton)-[:play_in]->(cream)
play_in
Follow
FollowJohn Jeff
Douglas
Cypher
(john:User)-[:FOLLOW]->(jeff:User)	
!
(douglas:User)-[:FOLLOW]->(john:User)
Cypher
clapton
{name: Eric
Clapton}
cream
(clapton)-[:play_in]->(cream)<-[:labeled]-(blues)
play_in
{date: 1968}
Blues
lab...
Cypher
MATCH (a)-—>(b)	
RETURN a,b;
Cypher
MATCH (a)-[:PLAY_IN]—>(b)	
RETURN a,b;
Cypher
MATCH (a)-[:PLAY_IN]—>(g),	
	 	 (g)<-[:LABELED]-(e)	
RETURN a.name,	
	 	 	 t.date,	
	 	 	 e.name;
Cypher
MATCH (c {name: ‘clapton’})-[t:PLAY_IN]—>(g),	
	 (g)<-[:LABELED]-(e)	
RETURN c.name,	
	 	 t.date,	
	 	 e.name;
Cypher
MATCH (c {name: ‘clapton’})-[t:PLAY_IN]—>(g),	
	 (g)<-[:LABELED]-(e {name: ‘blues’})	
RETURN c.name,	
		 e.name	
OR...
Cypher
MATCH (c {name: ‘clapton’})-[r:PLAY_IN | PRODUCE]—>(g),	
	 (g)<-[:LABELED]-(e {name: ‘blues’})	
RETURN c.name,	
	 e...
Cypher
MATCH (carlo)-[:KNOW*5]—>(john)
MATCH p = (startNode:Station {name: ‘Sol’})	
	 -[rels:CONNECTED_TO*]->	
(endNode:Station {name: ‘Retiro’})	
RETURN p AS sh...
Recommendation
System
Social network
Movies social network
Users rate movies
People act in movies
People direct movies
Users follow other users
Movies social network
How do we model it?
Movies social network
Follow
Rate
{stars}
User
Film
User
Actor
Director
Act in
Direct
Movies social network
MATCH (fran:User {name: ‘Fran’})	
	 	 	 -[or:Rate]->	
(pf:Film {title: ‘Pulp Fiction’}),	
!
(pf)<-[:...
Movies social network
Rate
{stars}
Rate
{stars}
User 1
Film
PF
Fran User 2
Rate
{stars}
Film
Film
Rate
{stars}
Rate
{stars}
Movies social network
MATCH (fran:User {name: ‘Fran’})	
	 	 	 -[or:Rate]->	
	 (pf:Film {title: ‘Pulp Fiction’}),	
!
(pf)<-...
Movies social network
MATCH (fran:User {name: ‘Fran’})	
	 	 	 -[or:Rate]->	
	 (pf:Film {title: ‘Pulp Fiction’}),	
!
(pf)<-...
Movies social network
Rate
{star}
User 1
Film
PF
Fran
Rate
{stars}
Film
Follow
Rate
{star}
Movies social network
MATCH (tarantino:User {name: ‘Quentin Tarantino’}),	
(tarantino)-[:DIRECT]->(movie)<-[:ACT_IN]-(tara...
Movies social network
Film Actor
Director
Act_in
Direct
Movies social network
Now you should be able to categorize the movies
Movies social network
Film
SubGenre
Belongs_to
SubGenre
Belongs_to
GenreGenre
Belongs_toBelongs_to
Movies social network
MATCH (fran:User {name: ‘Fran’})	
	 	 -[or:Rate]->	
	 (pf:Film {title: ‘Pulp Fiction’}),	
!
(pf)<-[:...
Neo4J extensions
Managed
Unmanaged
Neo4J extensions
Managed
Unmanaged
Neo4J extensions
Managed
Unmanaged
Drivers/Clients
Instead of just picking a relational database
because everyone does, we need to
understand the nature of the data we’re
st...
References
Neo4J as a service
http://www.graphenedb.com
Grazie
Graph Database, a little connected tour - Castano
Graph Database, a little connected tour - Castano
Graph Database, a little connected tour - Castano
Graph Database, a little connected tour - Castano
Upcoming SlideShare
Loading in...5
×

Graph Database, a little connected tour - Castano

238

Published on

Slides from Francisco Fernandez Castano talk @ codemotion roma 2014

0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total Views
238
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
8
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Graph Database, a little connected tour - Castano

  1. 1. Graph Databases A little connected tour ! @fcofdezc
  2. 2. Francisco Fernández Castaño @fcofdezc Sw Engineer @biicode
  3. 3. Beginning
  4. 4. The old town of Königsberg has seven bridges: Can you take a walk through town, visiting each part of the town and crossing each bridge only once?
  5. 5. El origenG = (V, E)
  6. 6. What is a Graph DB?
  7. 7. Graph Nodes Relationships Properties Store Store Connect Have Have
  8. 8. Written in Java ACID Rest interface Cypher
  9. 9. Why NOSQL?
  10. 10. The value of Relational Databases
  11. 11. Ventajas de BD Relacionales Concurrencia Persistencia Integración Estándar Persistence
  12. 12. Ventajas de BD Relacionales Concurrencia Persistencia Integración Estándar Concurrency
  13. 13. Ventajas de BD Relacionales Concurrencia Persistencia Integración Estándar Integration
  14. 14. Ventajas de BD Relacionales Concurrencia Persistencia Integración Estándar Standard
  15. 15. inconveniences Relational DBs
  16. 16. El OrigenImpedance Mismatch
  17. 17. class Client < ActiveRecord::Base has_one :address has_many :orders has_and_belongs_to_many :roles end
  18. 18. DesVentajas de BD Relacionales Fricción! Interoperabilidad Adaptación al cambio Escalabilidad No está destinada para ciertos escenarios Interoperability
  19. 19. Adaptation to changes
  20. 20. !Scalability
  21. 21. The traditional way in the context of connected data is artificial
  22. 22. Depth MySQL time (s) Neo4j time (s) Results 2 0.016 0.01 ~2500 3 30.267 0.168 ~110,000 4 1543.505 1.359 ~600,000 5 No Acaba 2.132 ~800,000 MySQL vs Neo4j * Neo4J in Action
  23. 23. Person Id Person 1 Frank 2 John .. … 99 Alice PersonFriend PersonID FriendID 1 2 2 1 .. … 99 2
  24. 24. O(log n)
  25. 25. O(1)
  26. 26. O(m log n)
  27. 27. O(m)
  28. 28. We can transform our domain model in a natural way
  29. 29. Use cases
  30. 30. Social Networks Follow Follow John Jeff Douglas
  31. 31. Geospatial problems Fraud detection Authorization Network management
  32. 32. Cypher Declarative language ASCII oriented Pattern matching
  33. 33. Cypher Cypher Traverser API Core API Kernel
  34. 34. Cypher a b (a)-->(b)
  35. 35. Cypher clapton cream (clapton)-[:play_in]->(cream) play_in
  36. 36. Follow FollowJohn Jeff Douglas Cypher (john:User)-[:FOLLOW]->(jeff:User) ! (douglas:User)-[:FOLLOW]->(john:User)
  37. 37. Cypher clapton {name: Eric Clapton} cream (clapton)-[:play_in]->(cream)<-[:labeled]-(blues) play_in {date: 1968} Blues labeled
  38. 38. Cypher MATCH (a)-—>(b) RETURN a,b;
  39. 39. Cypher MATCH (a)-[:PLAY_IN]—>(b) RETURN a,b;
  40. 40. Cypher MATCH (a)-[:PLAY_IN]—>(g), (g)<-[:LABELED]-(e) RETURN a.name, t.date, e.name;
  41. 41. Cypher MATCH (c {name: ‘clapton’})-[t:PLAY_IN]—>(g), (g)<-[:LABELED]-(e) RETURN c.name, t.date, e.name;
  42. 42. Cypher MATCH (c {name: ‘clapton’})-[t:PLAY_IN]—>(g), (g)<-[:LABELED]-(e {name: ‘blues’}) RETURN c.name, e.name ORDER BY t.date
  43. 43. Cypher MATCH (c {name: ‘clapton’})-[r:PLAY_IN | PRODUCE]—>(g), (g)<-[:LABELED]-(e {name: ‘blues’}) RETURN c.name, e.name WHERE r.date > 1968 ORDER BY r.date
  44. 44. Cypher MATCH (carlo)-[:KNOW*5]—>(john)
  45. 45. MATCH p = (startNode:Station {name: ‘Sol’}) -[rels:CONNECTED_TO*]-> (endNode:Station {name: ‘Retiro’}) RETURN p AS shortestPath, reduce(weight=0, r in rels: weight + r.weight) as tWeight ORDER BY tWeight ASC LIMIT 1
  46. 46. Recommendation System
  47. 47. Social network
  48. 48. Movies social network Users rate movies People act in movies People direct movies Users follow other users
  49. 49. Movies social network How do we model it?
  50. 50. Movies social network Follow Rate {stars} User Film User Actor Director Act in Direct
  51. 51. Movies social network MATCH (fran:User {name: ‘Fran’}) -[or:Rate]-> (pf:Film {title: ‘Pulp Fiction’}), ! (pf)<-[:Rate]-(other_users)-[:Rate]->(other_films) ! RETURN distinct other_films.title;
  52. 52. Movies social network Rate {stars} Rate {stars} User 1 Film PF Fran User 2 Rate {stars} Film Film Rate {stars} Rate {stars}
  53. 53. Movies social network MATCH (fran:User {name: ‘Fran’}) -[or:Rate]-> (pf:Film {title: ‘Pulp Fiction’}), ! (pf)<-[:Rate]-(other_users)-[r:Rate]->(other_films) ! WHERE or.stars = r.stars ! RETURN distinct other_films.title;
  54. 54. Movies social network MATCH (fran:User {name: ‘Fran’}) -[or:Rate]-> (pf:Film {title: ‘Pulp Fiction’}), ! (pf)<-[:Rate]-(other_users)-[r:Rate]->(other_films), ! (other_users)-[:FOLLOW]-(fran) ! WHERE or.stars = r.stars ! RETURN distinct other_films.title;
  55. 55. Movies social network Rate {star} User 1 Film PF Fran Rate {stars} Film Follow Rate {star}
  56. 56. Movies social network MATCH (tarantino:User {name: ‘Quentin Tarantino’}), (tarantino)-[:DIRECT]->(movie)<-[:ACT_IN]-(tarantino) RETURN movie.title
  57. 57. Movies social network Film Actor Director Act_in Direct
  58. 58. Movies social network Now you should be able to categorize the movies
  59. 59. Movies social network Film SubGenre Belongs_to SubGenre Belongs_to GenreGenre Belongs_toBelongs_to
  60. 60. Movies social network MATCH (fran:User {name: ‘Fran’}) -[or:Rate]-> (pf:Film {title: ‘Pulp Fiction’}), ! (pf)<-[:Rate]-(other_users)-[r:Rate]->(other_films), (film)->[:BELONGS_TO*3]->(genre)<-[:BELONGS_TO]-(other_films), ! (other_users)-[:FOLLOW]-(fran) ! WHERE or.stars = r.stars ! RETURN distinct other_films.title;
  61. 61. Neo4J extensions Managed Unmanaged
  62. 62. Neo4J extensions Managed Unmanaged
  63. 63. Neo4J extensions Managed Unmanaged
  64. 64. Drivers/Clients
  65. 65. Instead of just picking a relational database because everyone does, we need to understand the nature of the data we’re storing and how we want to manipulate it. Martin Fowler
  66. 66. References
  67. 67. Neo4J as a service http://www.graphenedb.com
  68. 68. Grazie
  1. A particular slide catching your eye?

    Clipping is a handy way to collect important slides you want to go back to later.

×