Contenu connexe
Plus de Java Usergroup Berlin-Brandenburg (19)
Graphdatenbanken mit Neo4j
- 1. © All Rights Reserved 2013 | Neo Technology, Inc.
Graphdatenbanken mit Neo4j
Modell - Einsatz - Abfrage mit Cypher - Use-Cases - 2.0
Stefan Plantikow
Freitag, 30. August 13
- 2. © All Rights Reserved 2013 | Neo Technology, Inc.
Guten Tag
• Entwickler bei neo technology
• Kernel und Abfragesprache
• Neo4j 1.9
• Neo4j 2.0-M04
@boggle
github.com/boggle
stefan.plantikow@neotechnology.com
Freitag, 30. August 13
- 3. © All Rights Reserved 2013 | Neo Technology, Inc.
Trends
• Big Data
„data sets so large and complex that it becomes difficult to process using
on-hand database management tools“
• Grösser
• Vernetzter
• Vielfältiger
Freitag, 30. August 13
- 4. © All Rights Reserved 2013 | Neo Technology, Inc.
NoSQL
Komplexität Grösse
Key-Value-Store Gering Gross
Column-Store Mittel Mittel bis gross
Document-Store Mittel Mittel
Graphdatenbank Sehr hoch Mittel
Freitag, 30. August 13
- 5. © All Rights Reserved 2013 | Neo Technology, Inc.
NoSQLComplexity
Column
Family
Size
Key-Value
Store
Document
Databases
Graph
Databases
90%
of
use
cases
RDBMS
Freitag, 30. August 13
- 6. © All Rights Reserved 2013 | Neo Technology, Inc.
Graphs != Charts
Graph DB Kenntnisse
Kennen
Graph DBs
Kennen keine
Graph DBs
Charts != Graphs
Freitag, 30. August 13
- 7. © All Rights Reserved 2013 | Neo Technology, Inc.
Gerichteter Graph
me
Freitag, 30. August 13
- 8. © All Rights Reserved 2013 | Neo Technology, Inc.
Beispielgraphen
diamond butterfly bullstar
franklin horton hall-jankorobertson
Freitag, 30. August 13
- 9. © All Rights Reserved 2013 | Neo Technology, Inc.
Beispielgraphen
Freitag, 30. August 13
- 10. © All Rights Reserved 2013 | Neo Technology, Inc.
Graphen
• Knoten („Dinge“)
• Kanten/Relationships („Beziehungen“, oft gerichtet)
• Mathematik: Multigraphen, Hypergraphen, Probabilistische Graphen, ...
• Graphdatenbanken: Property Graphs (Gerichteter Multigraph mit Properties)
• Key-Value Properties: Name, Letzter Login, Anzahl Posts, ...
• Label: Person, Artikel, Vorgang, ...
• Indices: Exakt nach Namen, Volltext nach Beschreibung, ...
2.0
Freitag, 30. August 13
- 11. © All Rights Reserved 2013 | Neo Technology, Inc.
Property Graphs
name:
Stefan
id: @gmail
title:
Cloud Atlas
type:
Book
:ORDERED
copies: 5
when: 2012
name:
Chris
id: @gmail
:FRIEND
since: 2013
title:
Cloud Atlas
type:
Movie
:BROWSED
times: 100
Freitag, 30. August 13
- 12. © All Rights Reserved 2013 | Neo Technology, Inc.
name:
Stefan
id: @gmail
title:
Cloud Atlas
type:
Book
:ORDERED
copies: 5
when: 2012
name:
Chris
id: @gmail
:FRIEND
since: 2013
title:
Cloud Atlas
type:
Movie
:BROWSED
times: 100
Labeled Property Graphs
Customer
Customer
Article
Article
TopHit
High Volume
2.0
Freitag, 30. August 13
- 13. © All Rights Reserved 2013 | Neo Technology, Inc.
Beispielanfrage
• Für einen Nutzer mit dem Namen
„Stefan“,
finde alle Artikel die sich seine
Freunde mindestens 4x angesehen
haben und
liefere sie nach Häufigkeit sortiert
zurück.
• Ausgehend von Knoten und Kanten
im Graph
Finde Muster im Graph und
berechne daraus das Ergebnis
Freitag, 30. August 13
- 14. © All Rights Reserved 2013 | Neo Technology, Inc.
Beispielanfrage
• START n=node(*)
MATCH (n {name: „Stefan“),
MATCH (n)-[:FRIEND]->(f),
(f)-[r:VIEWED]->(a)
WHERE r.browsed >= 4
RETURN a
ORDER BY r.browsed
• Ausgehend von Knoten und Kanten
im Graph
Finde Muster im Graph und
berechne daraus das Ergebnis
Freitag, 30. August 13
- 15. © All Rights Reserved 2013 | Neo Technology, Inc.
Beispielanfrage: Indexe, Muster, Ergebnis
• Wie kommt man „in den Graph“?
• Knoten- bzw. Kanten-Id
• Indexe:
Exakt, Volltext, Geospatial,
Custom
• Muster
• Einschränkungen an Knoten und
Kanten
und ihre Verbindungen
• Ergebnis
• Sortieren
• Aggregieren
• Filtern & Umwandeln
Freitag, 30. August 13
- 16. © All Rights Reserved 2013 | Neo Technology, Inc.
Vorteile von Graphen
• Natürliches und mächtiges Datenmodell
• Schnelles, problemfreies Object Mapping
• Strukturelle Abfragen sind einfach
• Beziehungen sind Modellelement 1. Klasse (nicht: implizite Joins)
• Erweiterbares einheitliches Modell für alle Daten
• Neue Properties
• Beziehungen zwischen vorher unverbundenen Daten
• Erfordert Umdenken!
Freitag, 30. August 13
- 17. © All Rights Reserved 2013 | Neo Technology, Inc.
Typische Anwendungen
• Social Networks &
Recommendations
• Geospatial
• Infrastructure as a Service
• Business Intelligence
• Content Management
• Access Control
• Bioinformatics
• Genealogie
• Telco
• Finance
• ...
(Fast) Alles ;) aber insbesondere:
Freitag, 30. August 13
- 18. © All Rights Reserved 2013 | Neo Technology, Inc.
Modellierung
• Von der Domäne zum Graph
• Von der (relationalen) Anwendung zum Graph
• Vergleich relationales Modellieren mit Modellierung im Graph
• How-to-Model
Freitag, 30. August 13
- 19. © All Rights Reserved 2013 | Neo Technology, Inc.
Whiteboard Modelling
Am Anfang ist das Whiteboard
Freitag, 30. August 13
- 20. © All Rights Reserved 2013 | Neo Technology, Inc.
Whiteboard Modelling
• Vom Use-Case zum Datenbankschema
Freitag, 30. August 13
- 21. © All Rights Reserved 2013 | Neo Technology, Inc.
Whiteboard Modelling (1)
name:
Adam
image:
lolcat1.gif
name:
Sarah
:SHARES :FRIEND_OF
:LIKES
text:
Funny!
:COMMENTED:ABOUT
Freitag, 30. August 13
- 22. © All Rights Reserved 2013 | Neo Technology, Inc.
Whiteboard Modelling (2)
name:
Adam
id: adam@
image:
lolcat1.gif
downloads:
5.000.000
name:
Sarah
:SHARES
:FRIEND_OF
since: 1995
:LIKES
text:
Funny!
source:
mobile
:COMMENTED
when: 2013:ABOUT
Freitag, 30. August 13
- 23. © All Rights Reserved 2013 | Neo Technology, Inc.
Whiteboard Modelling (3)
name:
Adam
id: adam@
text:
Look at this!
on:
Mar-2013
name:
Sarah
:SHARED
:FRIEND_OF
since: 1995
:LIKES
text:
Funny!
source:
mobile
:COMMENTED
when: 2013
:ABOUT
image:
lolcat1.gif
downloads:
5.000.000
Freitag, 30. August 13
- 24. © All Rights Reserved 2013 | Neo Technology, Inc.
Whiteboard Modelling (4)
name:
Adam
id: adam@
text:
Look at this!
on:
Mar-2013
name:
Sarah
:SHARED
:FRIEND_OF
since: 1995
:LIKES
text:
Funny!
source:
mobile
:COMMENTED
when: 2013
:ABOUT
image:
lolcat1.gif
downloads:
5.000.000
2.0
User
User
Post
CommentPost
FileResource
Freitag, 30. August 13
- 25. © All Rights Reserved 2013 | Neo Technology, Inc.
Graphen aus Relationen (1)
Customers AccountsCustomer_Accounts
143 Alice
326
$100
725
$632
981 $212
143 981
143 725
143 326
Freitag, 30. August 13
- 26. © All Rights Reserved 2013 | Neo Technology, Inc.
Graphen aus Relationen (2)
Alice
$100
$632
$212
143
326
725
981
143 981
143 725
143 326
Freitag, 30. August 13
- 27. © All Rights Reserved 2013 | Neo Technology, Inc.
Graphen aus Relationen (3)
name: Alice
bal: $100
bal: $212
Nodes
Relationships
owns
owns
owns
Freitag, 30. August 13
- 28. © All Rights Reserved 2013 | Neo Technology, Inc.
What language do they speak here?
Language Country
Freitag, 30. August 13
- 29. © All Rights Reserved 2013 | Neo Technology, Inc.
What language do they speak here?
Language Country
Freitag, 30. August 13
- 30. © All Rights Reserved 2013 | Neo Technology, Inc.
Languages in Tables (1)
language_code
language_name
word_count
Language
country_code
country_name
flag_uri
language_code
Country
Freitag, 30. August 13
- 31. © All Rights Reserved 2013 | Neo Technology, Inc.
Languages in Tables (2)
language_code
language_name
word_count
country_code
Language
country_code
country_name
flag_uri
Country
Freitag, 30. August 13
- 32. © All Rights Reserved 2013 | Neo Technology, Inc.
Languages in Tables (3)
language_code
language_name
word_count
Language
country_code
country_name
flag_uri
Country
language_code
country_code
LanguageCountry
Freitag, 30. August 13
- 33. © All Rights Reserved 2013 | Neo Technology, Inc.
Languages in Tables (4)
language_code
language_name
word_count
Language
country_code
country_name
flag_uri
Country
language_code
country_code
primary
LanguageCountry
Freitag, 30. August 13
- 34. © All Rights Reserved 2013 | Neo Technology, Inc.
Languages in Graphs (1)
name
word_count
Language
name
flag_uri
Country
IS_SPOKEN_IN
Freitag, 30. August 13
- 35. © All Rights Reserved 2013 | Neo Technology, Inc.
Languages in Graphs (2)
name
word_count
Language
name
flag_uri
Country
IS_SPOKEN_IN
as_primary
Freitag, 30. August 13
- 36. © All Rights Reserved 2013 | Neo Technology, Inc.
Unterschied: Languages in Tabellen vs. Graphs
language_code
language_name
word_count
Language
country_code
country_name
flag_uri
Country
language_code
country_code
primary
LanguageCountry
IS_SPOKEN_IN
Freitag, 30. August 13
- 37. © All Rights Reserved 2013 | Neo Technology, Inc.
How To Model
• Normalisierung: Erst in einem Knoten sammeln - dann Aufteilen
• ggf. Kanten einfügen um Relevantes zu verbinden
• ggf. Properties in Kanten wandeln und umgekehrt
• Unverbundene Knoten vermeiden
• Zusätzliche Zugriffspfade anlegen, z.B.
• zeitlich sortierte Linked List für Posts
• „Abkürzungen“
Freitag, 30. August 13
- 38. © All Rights Reserved 2013 | Neo Technology, Inc.
Graphdatenbanken
• NoSQL Datenbank
• Speichert Property Graph Modell
• zusätzliche Indexe
• ggf. Label
• Performance Definition:
Bei steigender Knotenanzahl selber Aufwand für einen „Schritt“ im Graphen
(in-memory)
Freitag, 30. August 13
- 39. © All Rights Reserved 2013 | Neo Technology, Inc.
Graphdatenbanken
• Performance Definition:
Bei steigender Knotenanzahl selber Aufwand für einen „Schritt“ im Graphen
(in-memory)
• Social Graph mit 50 „Freunden“ pro Nutzer im Durchschnitte
• Suche nach Freunden 4. Grades nach Datenbank-Warmup
DB # Nutzer Zeit
Relational 1.000 2000 ms
Neo4j 1.000 2 ms
Neo4j 1.000.000 2 ms
Freitag, 30. August 13
- 40. © All Rights Reserved 2013 | Neo Technology, Inc.
Neo4j
• Dual-Licensed: Open Source (AGPL) / Kommerziell
• Transaktional (ACID)
• Read-Committed
(Serializable durch explizites Locking)
• Server mit Web-UI
• High Availability (Online Backup, Replikation)
• Tooling: Shell, Backup, ...
• Umfangreiche Community & Kommerzieller Support
Freitag, 30. August 13
- 41. © All Rights Reserved 2013 | Neo Technology, Inc.
Neo4j Browser
Nach Installation: http://localhost:7474
Freitag, 30. August 13
- 42. © All Rights Reserved 2013 | Neo Technology, Inc.
Neo4j Console
Freitag, 30. August 13
- 43. © All Rights Reserved 2013 | Neo Technology, Inc.
Online Demo: console.neo4j.org
Ausprobieren:
start
n=node(*)
match
n-‐[r:LOVES]-‐>m
return
n,
type(r),
m
(rote
Knoten
sind
Ergebnisknoten)
Freitag, 30. August 13
- 44. © All Rights Reserved 2013 | Neo Technology, Inc.
Neo4j
• Dual-Licensed: Open Source (AGPL) / Kommerziell
• Transaktional (ACID)
• Read-Committed
(Serializable durch explizites Locking)
• Server mit Web-UI
• High Availability
• Tooling: Shell, Backup, Monitoring (JMX), ...
Freitag, 30. August 13
- 45. © All Rights Reserved 2013 | Neo Technology, Inc.
Neo4j Implementation
• Kernel: Java
Web-UI: Javascript
• kaum Dependencies
• kernel.jar: 2MB
• DirectByteBuffer
• GC-resistent custom caches
• Anfragesprache Cypher: Scala 2.10
• Runtime mit Kernel integriert
Freitag, 30. August 13
- 46. © All Rights Reserved 2013 | Neo Technology, Inc.
Einsatz
• Anfragesprache Cypher
• Embedded Database: Java Core API
• Eingebettet in Application Container
oder eigene Middleware
• Server: REST API
• Custom Server Extensions
Freitag, 30. August 13
- 47. © All Rights Reserved 2013 | Neo Technology, Inc.
Schnittstellen
• Neo4j
• Java Core API
• REST API
• Graph Frameworks
• Spring Data Graph
• Blueprints Neo4j
• Third Party Language Drivers
• JDBCS
• Ruby
• Javascript
• .NET
• Clojure
• Python...
Freitag, 30. August 13
- 48. © All Rights Reserved 2013 | Neo Technology, Inc.
Java Core API Operationen
• Transaktionen
• Knoten und Kanten anlegen
• Index Lookup
• Traversierung
• GlobalGraphOperations (alle Knoten etc.)
• Zusätzliche APIs:
A*, Custom Traversers, Batch Insertion, Transaction Handler
Freitag, 30. August 13
- 49. © All Rights Reserved 2013 | Neo Technology, Inc.
Java Core API: Knoten und Kanten einfügen
• GraphDatabaseService = new EmbeddedGraphDatabase( "var/graphDb" );
Transaction tx = gds.beginTransaction();
{
Node stefan = db.createLabeledNode( label(„Person“) );
stefan.setProperty( „name“, „Stefan“ );
Node chris = db.createNode( label(„Person“) );
chris.setProperty( „name“, „Chris“ );
stefan.createRelationshipTo( chris, withName(„FRIEND“) );
tx.success();
}
finally
{
tx.finish();
}
2.0
Freitag, 30. August 13
- 50. © All Rights Reserved 2013 | Neo Technology, Inc.
Java Core API: Kanten traversieren
• GraphDatabaseService = new EmbeddedGraphDatabase( "var/graphDb" );
Transaction tx = gds.beginTransaction();
{
Node stefan = gds.findNodesByLabelAndProperty( label(„Person“), „Stefan“ );
for ( Relationship rel : stefan.getRelationships( withName(„FRIEND“) ) ) {
result.add( rel.getProperty(„name“) );
}
tx.success();
}
finally
{
tx.finish();
}
2.0
Freitag, 30. August 13
- 51. © All Rights Reserved 2013 | Neo Technology, Inc.
REST API
• Core API Operationen via REST / JSON
• Cypher-Anfragen via REST, in 2.0 auch Transaktionen
• Beispiel: Knoten anlegen
curl -XPOST http://localhost:7474/db/data/node -d ,{ „name“: „Stefan“ }‘
• Beispiel: Cypher ausführen
curl -XPOST http://localhost:7474/db/data/cypher -d „Cypher-Query“
Freitag, 30. August 13
- 52. © All Rights Reserved 2013 | Neo Technology, Inc.
Cypher
• Neo4j‘s
• Deklarative
• Graph
• Anfragesprache
• Wie SQL ... nur für Graphen!
Freitag, 30. August 13
- 53. © All Rights Reserved 2013 | Neo Technology, Inc.
Cypher: Basic Example
// get node with id 0
start a=node(0) return a
// traverse from node 1
start a=node(1) match (a)-->(b) return b
// return friends of friends
start a=node(1) match (a)--()--(c) return c
• Deklarative Anfragesprache für Graphdatenbanken mit „SQL-Klausel-Syntax“
• Visuelles Graph Pattern-Matching um zu beschreiben, was man benötigt
• Ergebnis in Tabellenform
Freitag, 30. August 13
- 54. © All Rights Reserved 2013 | Neo Technology, Inc.
Cypher: Friends of a friend that ...
// traverse to find results
start n=node:People(name = „Andreas“)
match (n)--(f)--(foaf) return foaf
name:Andreas
job: talking
name: Tobias
job: coding
knows
since: 2008
knows
since: 2006
name: Peter
job: building
name: Emil
job: plumber
knows
since: 1992
name: Stephen
job: DJ
knows
since: 2002
knows
since: 2006
name: Delia
job: barking
knows
since: 2002
knows
since: 1998
name: Tiberius
job: dancer
knows
since: 2000
name: Allison
job: plumberknows
since: 2002
knows
since: 1998
knows
since: 1996
n
Freitag, 30. August 13
- 55. © All Rights Reserved 2013 | Neo Technology, Inc.
Cypher: Filtern und Sortieren
// lookup all nodes as 'n', constrained to name 'Stefan'
start n=node(*) where n.name='Stefan' return n
// filter nodes where age is less than 30
start n=node(*) where n.age<30 return n
// filter and aggregation using a regular expression
start n=node(*) where n.name =~ “Mat.*“ return count(n)
// find nodes with a property and return first 3 found
start n=node(*) where has(n.name) return n limit 3
• Filtern mit Prädikaten in WHERE-Klausel
• Aggregation, Sortieren, Begrenzen
Freitag, 30. August 13
- 56. © All Rights Reserved 2013 | Neo Technology, Inc.
Cypher: Änderungen an der Datenbanke
// create node
create (a {name : 'Andres'})
// create relationship
create (a {name : 'Andres'})-[:knows]->(b {name: 'Stefan'})
// match and update
start n=node(*) where n.age = 34 set n.age = 35
// find node and delete it
start n=node(*) where n.name = 'Dr. Evil' delete n
• Hinzfügen von Knoten und Kanten
• Aktualisieren von Knoten und Kanten
• Löschen von Knoten und Kanten
Freitag, 30. August 13
- 57. © All Rights Reserved 2013 | Neo Technology, Inc.
Cypher: Viel Mehr
• Shortest Path und Umgang mit Pfadmengen
• Funktionale Techniken: Flat, Map, etc.
• Optionale Muster
• Anfragen kombinieren
• Profiling
• ...
Freitag, 30. August 13
- 58. © All Rights Reserved 2013 | Neo Technology, Inc.
Use-Cases
• Social Networks &
Recommendations
• Geospatial
• Infrastructure as a Service
• Business Intelligence
• Content Management
• Access Control
• Bioinformatics
• Genealogie
• Telco
• Finance
• ...
Freitag, 30. August 13
- 59. © All Rights Reserved 2013 | Neo Technology, Inc.
Use Case: Social Collaboration
• Online Platform für globale
Kollaboration
• Datacenter pro Kontinent
• Hochverfürbar, weltweit
• <1s Antwortzeit für grosse, stark
vernetzte Datenmenge
Asia North America Europe
Asia North America Europe
Freitag, 30. August 13
- 60. © All Rights Reserved 2013 | Neo Technology, Inc.
Use Case: Social Recommendation
• Social Network mit
35.000.000 Nutzern
• Recommendations
• Problem:
• MySQL didn‘t scale
• Batch job >1d, >2d, ...
• Ziel:
Up-to-date Recommendations
• Keine Batch-Jobs
• Lösung
• Real-Time Recommendations
• 8 Wochen
• Skaliert
Freitag, 30. August 13
- 61. © All Rights Reserved 2013 | Neo Technology, Inc.
Configuration and Network Management
Freitag, 30. August 13
- 62. © All Rights Reserved 2013 | Neo Technology, Inc.
Use-Case: Configuration and Network
Management
• Physikalisches und logisches Netzwerk modellieren
• Impaktanalyse
• Konfigurationsmanagement
• Network Inventory
Freitag, 30. August 13
- 63. © All Rights Reserved 2013 | Neo Technology, Inc.
Geospatial
Freitag, 30. August 13
- 64. © All Rights Reserved 2013 | Neo Technology, Inc.
Use-Case:
ACL from Hell
• Utility Company
• > 5.000.000 Nutzer
• Komplexe Regen
• Vorteile
• Flexible dynamisch erweiterbare
Architekur
• Geringe Kosten
• Hohe Performance
Freitag, 30. August 13
- 65. © All Rights Reserved 2013 | Neo Technology, Inc.
Ausblick auf Neo4j 2.0
• Labels
• Unique Constraints
• Neues Index-Handling
• Cypher-Erweiterungen
• ...
Freitag, 30. August 13
- 66. © All Rights Reserved 2013 | Neo Technology, Inc.
Learn More
http://stackoverflow.com/questions/tagged/neo4j
http://groups.google.com/group/neo4j
https://github.com/neo4j/neo4j/issues
Freitag, 30. August 13
- 67. © All Rights Reserved 2013 | Neo Technology, Inc.
Learn More
http://neo4j.meetup.com/
http://www.neo4j.org/participate/
Freitag, 30. August 13
- 68. © All Rights Reserved 2013 | Neo Technology, Inc.
Learn More
http://neo4j.org
Freitag, 30. August 13
- 69. © All Rights Reserved 2013 | Neo Technology, Inc.
Vielen Dank
Fragen?
Freitag, 30. August 13
- 70. © All Rights Reserved 2013 | Neo Technology, Inc.
Graphs are everywhere
Freitag, 30. August 13