SlideShare una empresa de Scribd logo
Graph Databases
A little connected tour
!

@fcofdezc
El origen
Graph databases, a little connected tour
Dado el mapa de Königsberg, con el río Pregolya dividiendo
el plano en cuatro regiones distintas, que están unidas a
través de los siete puentes,
¿es posible dar un paseo comenzando desde cualquiera de
estas regiones, pasando por todos los puentes, recorriendo
sólo una vez cada uno, y regresando al mismo punto de
partida?
= (V, E)
ElG origen
¿Qué es?
Graph databases, a little connected tour
rda
Gua

a

Relaciones

e

Propiedades

Tie
n

e

Tie
n

Organiza

ard
Gu

Nodos

Grafo
Escrita en java
ACID
Interfaz REST
Cypher
¿Por qué NOSQL?
Ventajas BD Relacionales
Ventajas de BD Relacionales
Concurrencia
Persistencia

Persistencia
Integración
Estándar
Ventajas de BD Relacionales
Concurrencia
Persistencia

Concurrencia
Integración
Estándar
Ventajas de BD Relacionales
Concurrencia
Persistencia

Integración
Integración
Estándar
Ventajas de BD Relacionales
Concurrencia
Persistencia

Estándar

Integración
Estándar
DESVentajas BD Relacionales
Fricción
El Origen
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
Interoperabilidad
Escalabilidad
No está destinada para ciertos escenarios
Adaptación al cambio
!Escalabilidad
MySQL vs Neo4j
Profundidad Tiempo MySQL(s) Tiempo Neo4j (s)

Nº Resultados

2
3

0.016
30.267

0.01
0.168

~2500
~110,000

4

1543.505

1.359

~600,000

5

No Acaba

2.132

~800,000
* Neo4J in Action
El enfoque tradicional en el contexto
de datos conectados es artificial
Podemos trasladar el modelo del dominio
que estamos tratando de forma natural
Graph databases, a little connected tour
Casos de uso
Redes Sociales
Sigue
Juan

Jose
Sigue
María
Problemas Geoespaciales
Detección de fraude
Gestión de permisos
Gestión de redes
Cypher
Lenguaje declarativo
ASCII oriented
Pattern matching
Cypher
Cypher
Traverser API
Core API
Kernel
Cypher

a

b

(a)-->(b)
Cypher
toca_en
clapton

cream

(clapton)-[:toca_en]->(cream)
Cypher
Sigue
Juan

Jose

Sigue
María

(juan:Persona)-[:sigue]->(jose:Persona)	
!

(maria:Persona)-[:sigue]->(juan:Persona)
Cypher
toca_en

etiquetado

{fecha: 1968}

clapton
{nombre: Eric
Clapton}

cream

Blues

(clapton)-[:toca_en]->(cream)<-[:etiquetado]-(blues)
Cypher
MATCH (a)-—>(b)	
RETURN a,b;
Cypher
MATCH (a)-[:TOCA_EN]—>(b)	
RETURN a,b;
Cypher
MATCH (a)-[t:TOCA_EN]—>(g),	
	 	 (g)<-[:ETIQUETADO]-(e)	
RETURN a.nombre,	
	 	 	 t.fecha,	
	 	 	 e.nombre;
Cypher
START c=node:node_auto_index(nombre=‘clapton’)	
MATCH (c)-[t:TOCA_EN]—>(g),	
	
(g)<-[:ETIQUETADO]-(e)	
RETURN c.nombre,	
	 	 t.fecha,	
	 	 e.nombre;
Cypher
START c=node:node_auto_index(nombre=‘clapton’)	
	
e=node:node_auto_index(nombre=‘blues’)	
MATCH (c)-[t:TOCA_EN]—>(g),	
	
(g)<-[:ETIQUETADO]-(e)	
RETURN c.nombre,	
	 	 e.nombre	
ORDER BY t.fecha
Cypher
START c=node:node_auto_index(nombre=‘clapton’)	
	
e=node:node_auto_index(nombre=‘blues’)	
MATCH (c)-[t:TOCA_EN | PRODUCE]—>(g),	
	
(g)<-[:ETIQUETADO]-(e)	
WHERE t.fecha > 1968	
RETURN c.nombre,	
	 	 e.nombre
Cypher

MATCH (juan)-[:CONOCE*5]—>(pepe)
START startNode=node:node_auto_index(name = ‘Sol'),
endNode=node:node_auto_index(name = ‘Cuzco')
MATCH p = (startNode)-[rels:CONNECTED_TO]->(endNode)
RETURN p AS shortestPath,
reduce(weight=0, r in rels: weight + r.weight) as tWeight
ORDER BY tWeight ASC
LIMIT 1
Sistema de
recomendación
Red social de cine
Red social de cine
Usuarios puntúan películas
Personas actúan en películas
Personas dirigen películas
Usuarios siguen a otros usuarios
Red social de cine
¿Cómo lo modelamos?
Red social de cine
Sigue
User

User
Puntúa
{nota}

Actua en
Film

Actor
Dirige
Director
Red social de cine
START fran=node:Persona(name='Fran'),
film=node:Peliculas(title=‘Pulp Fiction')
MATCH fran-[or:PUNTUA]->film<-[:PUNTUA]-otro_user-[r:PUNTUA]->otras_pelis
RETURN distinct otras_pelis.title;
Red social de cine
Puntúa
{nota}
Puntúa
{nota}

Film
Film

User 1

Fran

User 2
Puntúa
{nota}

Film
PF

Puntúa
{nota}

Puntúa
{nota}
Red social de cine
START fran=node:Persona(name='Fran'),
film=node:Peliculas(title=‘Pulp Fiction')
MATCH fran-[or:PUNTUA]->film<-[:PUNTUA]-otro_user-[r:PUNTUA]->otras_pelis
WHERE or.stars = r.stars
RETURN distinct otras_pelis.title;
Red social de cine
START fran=node:Persona(name='Fran'),
film=node:Peliculas(title=‘Pulp Fiction')
MATCH fran-[or:PUNTUA]->film<-[:PUNTUA]-otro_user-[r:PUNTUA]->otras_pelis,
otro_user-[:SIGUE]-fran
WHERE or.stars = r.stars
RETURN distinct otras_pelis.title;
Red social de cine
Puntúa
{nota}
Sigue

User 1
Puntúa
{nota}

Fran
Puntúa
{nota}

Film
PF

Film
Red social de cine
START tarantino=node:Persona(name='Quentin Tarantino')
MATCH tarantino-[:DIRIGE]->peli<-[:ACTUA_EN]-tarantino
RETURN movie.title;
!
Red social de cine
Actua en
Film

Actor
Dirige
Director
Red social de cine
Ahora se deben poder categorizar las películas
Red social de cine
Genero

Genero
Pertenece_a

Pertenece_a
SubGenero

SubGenero

Pertenece_a

Pertenece_a
Film
Red social de cine
START fran=node:Persona(name='Fran'),
film=node:Peliculas(title=‘Pulp Fiction')
MATCH fran-[or:PUNTUA]->film<-[:PUNTUA]-otro_user-[r:PUNTUA]->otras_pelis,
film-[:PERTENECE:*3]->genero<-[:PERTENECE]-otras_pelis
WHERE or.stars = r.stars
RETURN distinct otras_pelis.title;
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
Referencias
Neo4J as a service

http://www.graphenedb.com
Neo4J Spain
20 Febrero

http://www.meetup.com/graphdb-spain/
Graph databases, a little connected tour
Gracias

Más contenido relacionado

Graph databases, a little connected tour