Upload
fcofdezc
View
384
Download
1
Tags:
Embed Size (px)
DESCRIPTION
Citation preview
Graph DatabasesA little connected tour
!
@fcofdezc
El origen
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?
El origenG = (V, E)
¿Qué es?
Grafo
Nodos Relaciones
Propiedades
Guar
daGuarda
Organiza
Tiene
Tiene
Escrita en java
ACID
Interfaz REST
Cypher
¿Por qué NOSQL?
Ventajas BD Relacionales
Ventajas de BD Relacionales
ConcurrenciaPersistenciaIntegraciónEstándar
Persistencia
Ventajas de BD Relacionales
ConcurrenciaPersistenciaIntegraciónEstándar
Concurrencia
Ventajas de BD Relacionales
ConcurrenciaPersistenciaIntegraciónEstándar
Integración
Ventajas de BD Relacionales
ConcurrenciaPersistenciaIntegraciónEstándar
Estándar
DESVentajas BD Relacionales
El OrigenFricción
class Client < ActiveRecord::Base has_one :address has_many :orders has_and_belongs_to_many :rolesend
DesVentajas de BD Relacionales
Fricción!InteroperabilidadAdaptación al cambioEscalabilidadNo está destinada para ciertos escenarios
Interoperabilidad
Adaptación al cambio
!Escalabilidad
Profundidad Tiempo MySQL(s) Tiempo Neo4j (s) Nº Resultados
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
El enfoque tradicional en el contexto de datos conectados es artificial
Podemos trasladar el modelo del dominio que estamos tratando de forma natural
Casos de uso
Redes Sociales
Sigue
Sigue
Juan Jose
María
Problemas Geoespaciales
Detección de fraude
Gestión de permisos
Gestión de redes
CypherLenguaje declarativo
ASCII oriented
Pattern matching
CypherCypher
Traverser API
Core API
Kernel
Cypher
a b
(a)-->(b)
Cypher
clapton cream
(clapton)-[:toca_en]->(cream)
toca_en
Sigue
SigueJuan Jose
María
Cypher
(juan:Persona)-[:sigue]->(jose:Persona)!
(maria:Persona)-[:sigue]->(juan:Persona)
Cypher
clapton {nombre: Eric Clapton}
cream
(clapton)-[:toca_en]->(cream)<-[:etiquetado]-(blues)
toca_en {fecha: 1968}
Blues
etiquetado
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.nombreORDER 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 > 1968RETURN 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
Puntúa {nota}
User
Film
User
Actor
Director
Actua en
Dirige
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}
User 1
Film PF
Fran User 2
Puntúa {nota}
Film
Film
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}
User 1
Film PF
FranPuntúa {nota}
Film
Sigue
Puntúa {nota}
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
Film Actor
Director
Actua en
Dirige
Red social de cine
Ahora se deben poder categorizar las películas
Red social de cine
Film
SubGenero
Pertenece_a
SubGenero
Pertenece_a
GeneroGenero
Pertenece_aPertenece_a
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
http://www.meetup.com/graphdb-spain/
20 Febrero
Gracias