44
Neo4j Daten natürlich modelieren und verarbeiten mit Neo4j SBB IT Developer Day 2012 | BEA Bern Expo | 11.12.2012

Neo4j - bedcon...• Polyglot Persistence ... • VMware/SpringSource Certified Instructor (Spring Trainer) • Spring Framework, OSGi & Agile Engineering Practices

  • Upload
    others

  • View
    17

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Neo4j - bedcon...• Polyglot Persistence ... • VMware/SpringSource Certified Instructor (Spring Trainer) • Spring Framework, OSGi & Agile Engineering Practices

Neo4j• Daten natürlich modelieren und verarbeiten mit

Neo4j

• SBB IT Developer Day 2012 | BEA Bern Expo | 11.12.2012

Page 2: Neo4j - bedcon...• Polyglot Persistence ... • VMware/SpringSource Certified Instructor (Spring Trainer) • Spring Framework, OSGi & Agile Engineering Practices

2

Agenda

• Speaker Profil

• Einführung

• Usecases

• Neo4j

• Querying

• Import / Export

• Tools und APIs

• Polyglot Persistence

• Highlights & Challenges beim Einsatz

• Q & A

Page 3: Neo4j - bedcon...• Polyglot Persistence ... • VMware/SpringSource Certified Instructor (Spring Trainer) • Spring Framework, OSGi & Agile Engineering Practices

3

Speaker Profile

Patrick Baumgartner• Senior Software Consultant | Partner

• VMware/SpringSource Certified Instructor (Spring Trainer)

• Spring Framework, OSGi & Agile Engineering Practices

• Co-Autor von „OSGi für Praktiker“ (Hanser)

Swiftmind GmbH http://www.swiftmind.com

• Enterprise Java, Spring & OSGi Consulting

• Spring & OSGi Workshops & Trainings

• Agile Engineering Practices Coaching

Page 4: Neo4j - bedcon...• Polyglot Persistence ... • VMware/SpringSource Certified Instructor (Spring Trainer) • Spring Framework, OSGi & Agile Engineering Practices

4

New Types of Data Stores

Page 5: Neo4j - bedcon...• Polyglot Persistence ... • VMware/SpringSource Certified Instructor (Spring Trainer) • Spring Framework, OSGi & Agile Engineering Practices

5

NoSQL

Datengrösse

Datenkomplexität

Key-Value Stores

Column Family

Document Databases

Graph Databases

Relational Databases

90% aller Usecases

Page 6: Neo4j - bedcon...• Polyglot Persistence ... • VMware/SpringSource Certified Instructor (Spring Trainer) • Spring Framework, OSGi & Agile Engineering Practices

6

Property Graph Modell

• Graphen bestehen aus

– Knoten / Nodes

– Beziehungen / Relations

– Eigenschafen / Properties

Alice

Bob

Car

name:Bobage:63sex:male

name:Aliceage:21sex:female

type:carvendor:tesla

type:KNOWS

time:42 days typ

e:OW

NStype:DRIVES

Page 7: Neo4j - bedcon...• Polyglot Persistence ... • VMware/SpringSource Certified Instructor (Spring Trainer) • Spring Framework, OSGi & Agile Engineering Practices

Drag picture to placeholder or click icon to add

7

• Wo finde ich meinen Graphen?

• Usecases

Page 8: Neo4j - bedcon...• Polyglot Persistence ... • VMware/SpringSource Certified Instructor (Spring Trainer) • Spring Framework, OSGi & Agile Engineering Practices

8

Social Data

AliceMat

KNOWS

Luke

Bob

Sam

PeteKNOWS

KN

OW

S

KNOWS

KNOWSKNOWS

KNOWS

KNOW

S

Page 9: Neo4j - bedcon...• Polyglot Persistence ... • VMware/SpringSource Certified Instructor (Spring Trainer) • Spring Framework, OSGi & Agile Engineering Practices

Drag picture to placeholder or click icon to add

9

• Ein einziger Anwendungsfall?

• Social Graph

Page 10: Neo4j - bedcon...• Polyglot Persistence ... • VMware/SpringSource Certified Instructor (Spring Trainer) • Spring Framework, OSGi & Agile Engineering Practices

10

Spatial Data

12

type:ROADlength:5km

5

3

13

8ROAD

RO

AD

ROAD

ROADtype:ROADlength:3km

ROAD

type:Hotelname:Radisson Blulat:47.452549 long:8.564615

type:Hotelname:Widderlat:47.373517 long:8.539252

ROAD RO

AD

Page 11: Neo4j - bedcon...• Polyglot Persistence ... • VMware/SpringSource Certified Instructor (Spring Trainer) • Spring Framework, OSGi & Agile Engineering Practices

11

Social & Spatial Data

AliceMat

type:LIKESweight:5

5

Bob

13

8ROAD

RO

AD

ROAD

LIKESKNOWS

SIBLING

type:Hotelname:Radisson Blulat:47.452549 long:8.564615

KNOW

S

Page 12: Neo4j - bedcon...• Polyglot Persistence ... • VMware/SpringSource Certified Instructor (Spring Trainer) • Spring Framework, OSGi & Agile Engineering Practices

12

Financial Data

AliceHotel Luke

Bank

ATM

PeteWITHDRAW

TRANSFE

R

WITHDRAW

TRANSFEROWNS

DEPOSIT

DEPOSIT

Page 13: Neo4j - bedcon...• Polyglot Persistence ... • VMware/SpringSource Certified Instructor (Spring Trainer) • Spring Framework, OSGi & Agile Engineering Practices

13

Eine beliebige Business Domäne

Process

KPI

Device

Activity

Function

ServiceDEPENDS_OND

EPE

ND

S_O

N

RUNS_ON

DEPENDS_ON

DEPENDS_ON

DEPENDS_ON

Page 14: Neo4j - bedcon...• Polyglot Persistence ... • VMware/SpringSource Certified Instructor (Spring Trainer) • Spring Framework, OSGi & Agile Engineering Practices

14

Schienennetz der Schweiz

Page 15: Neo4j - bedcon...• Polyglot Persistence ... • VMware/SpringSource Certified Instructor (Spring Trainer) • Spring Framework, OSGi & Agile Engineering Practices

15

Neo4jThe Big Picture

Page 16: Neo4j - bedcon...• Polyglot Persistence ... • VMware/SpringSource Certified Instructor (Spring Trainer) • Spring Framework, OSGi & Agile Engineering Practices

16

Neo4j – Big Picture

• Die verbreiteste Graphdatenbank der Welt

– Robust: Im Betrieb 24/7 seit 2003

– Reifegrad: Vielfach in Produktion ausgerollt

– Community: Ecosystem mit Tools, Bindings, Frameworks

• Lizenzen

– AGPLv3 Community Edition – OpenSource

– Advanced/Enterprise – Für kommerzielle Anwendungen

• Neo Technology

– Entwicklung & Support

– ~ 45 Personen / 6 Länder / 3 Kontinente

– Total 24 Mio $ VC Finanzierung

Page 17: Neo4j - bedcon...• Polyglot Persistence ... • VMware/SpringSource Certified Instructor (Spring Trainer) • Spring Framework, OSGi & Agile Engineering Practices

17

Eigenschaften

• Objekt-orientiert, flexible Netzwerkstruktur

• Support für ACID Transaktionen

• Horizontal skalierbar

• Java API

– Anbindung mittels Java, Groovy, Scala, JRuby

• Runtime

– Standalone Server

– Embedded Database

• Diskbasierter Storage Manager

• Support für Erweiterungen und Testing

Page 18: Neo4j - bedcon...• Polyglot Persistence ... • VMware/SpringSource Certified Instructor (Spring Trainer) • Spring Framework, OSGi & Agile Engineering Practices

Drag picture to placeholder or click icon to add

18

• Wie komm ich an meine Daten?

• Querying

Page 19: Neo4j - bedcon...• Polyglot Persistence ... • VMware/SpringSource Certified Instructor (Spring Trainer) • Spring Framework, OSGi & Agile Engineering Practices

19

Graph Querying

• Wie erhalte ich die richtigen Nodes aus dem Graphen?

– Gib mir alle Freunde und Freunde von Freunden welche den Film XY mit mehr als 3 Sternen bewertet haben.

– ...

• Queryingmethoden

– Traversing mit Neo4j API

– Traversal Descriptions

– Graphalgorithmen

– Index Queries

– Cypher Queries

Page 20: Neo4j - bedcon...• Polyglot Persistence ... • VMware/SpringSource Certified Instructor (Spring Trainer) • Spring Framework, OSGi & Agile Engineering Practices

20

Neo4j API

Node bob = ...;

for ((Relationship rel: bob.getRelationships(RelTypes.KNOWS)) { Node friend = rel.getEndNode(); System.out.println(friend.getProperty("name"));}

• Einfache, aber sehr „low level“ API• Zu „verbose“ für komplexe Traversals

Page 21: Neo4j - bedcon...• Polyglot Persistence ... • VMware/SpringSource Certified Instructor (Spring Trainer) • Spring Framework, OSGi & Agile Engineering Practices

21

Traversal API

• Beschreibt Bewegungen durch den Graphen

• Callback basiert

• Abfrage API für Graphen

• Fluent API

• Konstrukte

• Neo4j implementiert folgende Algorithmen:

– A*

– Dijkstra

– pathsWithLength

– shortestPath

– allSimplePaths

– allPaths

Page 22: Neo4j - bedcon...• Polyglot Persistence ... • VMware/SpringSource Certified Instructor (Spring Trainer) • Spring Framework, OSGi & Agile Engineering Practices

22

Traversal API – Konstrukte

Page 23: Neo4j - bedcon...• Polyglot Persistence ... • VMware/SpringSource Certified Instructor (Spring Trainer) • Spring Framework, OSGi & Agile Engineering Practices

23

Beispiel: Traversal API

TraversalDescription directFriends = Traversal.description()

.breadthFirst()

.relationships(RelTypes.KNOWS)

.evaluator(Evaluators.toDepth(1))

.evaluator(Evaluators.excludeStartPosition());

for(Path p : directFriends.traverse(hans)) {

System.out.println(p.endNode().getProperty(„firstname“));

}

Page 24: Neo4j - bedcon...• Polyglot Persistence ... • VMware/SpringSource Certified Instructor (Spring Trainer) • Spring Framework, OSGi & Agile Engineering Practices

24

Index Queries

• Unterstützung von Node und Relationship Indices

• Lucene als Standard-implementierung

node id property value

15 name Alice

15 yearOfBirth 1972

16 name Bob

46 name Carol

46 yearOfBirth 1983

Page 25: Neo4j - bedcon...• Polyglot Persistence ... • VMware/SpringSource Certified Instructor (Spring Trainer) • Spring Framework, OSGi & Agile Engineering Practices

25

Cypher Query Language

• Graph Query Language ähnlich SQL

• Deklarative Sprache

• Definiert das „Was“ – nicht das „Wie“

• Nutzt Pattern Matching

• Support für

– Filterung

– Paginierung

• Update-Funktionalität

Page 26: Neo4j - bedcon...• Polyglot Persistence ... • VMware/SpringSource Certified Instructor (Spring Trainer) • Spring Framework, OSGi & Agile Engineering Practices

26

Cypher Query Language – Elemente

Element Bedeutung

START Startelement(e) (Angabe von Index- o. Id-Lookup)

MATCH Pattern für zu findende Knoten, beschrieben durch Pfade auf Identifiern (a) –[knows]-> (b)

WHERE Resultatsfilterung (boolean-Ausdruck)

SKIP LIMIT Paginierung

RETURN Definiert Rückgabe-Elemente

ORDER BY Sortierung nach Properties

PARAMETERS Parameter-Map die im Cypher mittels Key oder Position verwendet werden kann

CREATE Erzeugt einen Knoten

Page 27: Neo4j - bedcon...• Polyglot Persistence ... • VMware/SpringSource Certified Instructor (Spring Trainer) • Spring Framework, OSGi & Agile Engineering Practices

27

Beispiel: Cypher Query

String query = "START student=node(12) “

+ “MATCH student-[:KNOWS]-friend “

+ “RETURN friend“;

ExecutionEngine engine = new ExecutionEngine(graphDb);

ExecutionResult result = engine.execute(query);

Iterator<Object> column = result.columnAs("friend");

while(column.hasNext()) {

Node friendNode = (Node)column.next();

System.out.println(friendNode.getProperty(PROP_FIRSTNAME));

}

Page 28: Neo4j - bedcon...• Polyglot Persistence ... • VMware/SpringSource Certified Instructor (Spring Trainer) • Spring Framework, OSGi & Agile Engineering Practices

28

Beispiel: Cypher Create Query

CREATE (carol {name:“Carol“}) return carol;

+------------------------+

| carol |

+------------------------+

| Node[1]{name:“carol“} |

+------------------------+

START carol=node(1) CREATE (bob {name:“Bob"}),

(bob)-[:knows]->(carol) return carol;

START carol=node(1)

CREATE (apple {name:“Apple“,typ:“Fruit“}),

(carol)-[:owns {number: 5}]->(apple);

Page 29: Neo4j - bedcon...• Polyglot Persistence ... • VMware/SpringSource Certified Instructor (Spring Trainer) • Spring Framework, OSGi & Agile Engineering Practices

Drag picture to placeholder or click icon to add

29

• Wie lese und schreibe ich Daten?

• Import / Export

Page 30: Neo4j - bedcon...• Polyglot Persistence ... • VMware/SpringSource Certified Instructor (Spring Trainer) • Spring Framework, OSGi & Agile Engineering Practices

30

Erzeugung von Nodes mit Java

org.neo4j.graphdb.GraphDatabaseService graphDb = new GraphDatabaseFactory().newEmbeddedDatabase(DB_PATH);

Transaction tx = graphDb.beginTx();

try {

Node peter = createNode(graphDb, "Peter");

Node hans = createNode(graphDb, "Hans");

peter.createRelationshipTo(hans, RelTypes.KNOWS);

..

tx.success();

} finally {tx.finish();}

public Node createNode(GraphDatabaseService graphDb, String firstname) {

Node student = graphDb.createNode();

student.setProperty(„firstname“, firstname);

return student;

}

Page 31: Neo4j - bedcon...• Polyglot Persistence ... • VMware/SpringSource Certified Instructor (Spring Trainer) • Spring Framework, OSGi & Agile Engineering Practices

31

NeoClipse

Page 32: Neo4j - bedcon...• Polyglot Persistence ... • VMware/SpringSource Certified Instructor (Spring Trainer) • Spring Framework, OSGi & Agile Engineering Practices

32

Thinkerpop – Blueprint

Thinkerpop Blueprint Extensions• GraphML Reader/Writer

Library• GraphSon Reader/Writer

Library• GML Reader/Writer Library• Java Universal

Network/Graph Framework

Page 33: Neo4j - bedcon...• Polyglot Persistence ... • VMware/SpringSource Certified Instructor (Spring Trainer) • Spring Framework, OSGi & Agile Engineering Practices

33

GraphML

• GraphML (http://graphml.graphdrawing.org/ )

– Standardisierte Beschreibungssprache für Graphen

– Modellierungswerkzeuge

– XML

<graph id="G" edgedefault="directed">

<node id="n0"/>

<node id="n1"/>

<edge source="n0" target="n1"/>

</graph>

Page 34: Neo4j - bedcon...• Polyglot Persistence ... • VMware/SpringSource Certified Instructor (Spring Trainer) • Spring Framework, OSGi & Agile Engineering Practices

34

Neo4j Bordmittel

• GraphML

– Gremlin Plugin

• CSV

– BatchInserter

– ParallelBatchInserter

Page 35: Neo4j - bedcon...• Polyglot Persistence ... • VMware/SpringSource Certified Instructor (Spring Trainer) • Spring Framework, OSGi & Agile Engineering Practices

Drag picture to placeholder or click icon to add

35

• Nützliche Helfer für die tägliche Arbeit

• Tools & APIs

Page 36: Neo4j - bedcon...• Polyglot Persistence ... • VMware/SpringSource Certified Instructor (Spring Trainer) • Spring Framework, OSGi & Agile Engineering Practices

36

Tools & APIs (2)

• Admin Web-Interface

– Überblick über Daten

– Abfrage

– Commandline Tool

• Neo4j Shell

– Commandline Tool

– Send UNIX-like Commands via RMI (cd, ls, pwd)

– Support für Cypher Queries

• REST API

– Zugriff mittels HTTP und JSON

– Clients für div. Sprache (Java, .NET, PHP, Ruby,...)

• Neoclipse

– Eclipse basiertes Tool für graphische Darstellung

– Filteroptionen für Teilgraphendarstellung

Page 37: Neo4j - bedcon...• Polyglot Persistence ... • VMware/SpringSource Certified Instructor (Spring Trainer) • Spring Framework, OSGi & Agile Engineering Practices

37

Tools & APIs

• Gremlin Plugin

– Groovy basierte graph traversal language via REST API

– Graph importer

• Indexing

– Lucene

• Third Party Libraries

– Thinkerpop

– Spring Data Neo4j (SDN)

• ...

Page 38: Neo4j - bedcon...• Polyglot Persistence ... • VMware/SpringSource Certified Instructor (Spring Trainer) • Spring Framework, OSGi & Agile Engineering Practices

Drag picture to placeholder or click icon to add

38

• Gibt es nur EINEN zentralen Datentopf?

• Polyglot Persistence

Page 39: Neo4j - bedcon...• Polyglot Persistence ... • VMware/SpringSource Certified Instructor (Spring Trainer) • Spring Framework, OSGi & Agile Engineering Practices

39

Polyglot Persistence

WebShop Application

User Sessions Shopping Card Recommendations

Product Catalog Analytics Financial Data

Redis Riak Neo4j

MongoDB Casandra MySQL

Page 40: Neo4j - bedcon...• Polyglot Persistence ... • VMware/SpringSource Certified Instructor (Spring Trainer) • Spring Framework, OSGi & Agile Engineering Practices

40

Challenges and Highlights

Page 41: Neo4j - bedcon...• Polyglot Persistence ... • VMware/SpringSource Certified Instructor (Spring Trainer) • Spring Framework, OSGi & Agile Engineering Practices

41

Challenges

• Developer Onboarding

• Indexierung

• “Schemaänderungen” -> Migrations

• Extrem viele Daten

• Oft neue Neo4j Releases

• Bugs – keine Showstopper

• Übersicht über das Modell behalten

– grafischeModelle aktuell halten

Page 42: Neo4j - bedcon...• Polyglot Persistence ... • VMware/SpringSource Certified Instructor (Spring Trainer) • Spring Framework, OSGi & Agile Engineering Practices

42

Highlights

• Community mit guten Support

• Sourcen auf Github

• Performance

• Whiteboard friendliness

• Spring Programmier-Modell mit Spring Data Neo4j

Page 43: Neo4j - bedcon...• Polyglot Persistence ... • VMware/SpringSource Certified Instructor (Spring Trainer) • Spring Framework, OSGi & Agile Engineering Practices

43

Q&A

Page 44: Neo4j - bedcon...• Polyglot Persistence ... • VMware/SpringSource Certified Instructor (Spring Trainer) • Spring Framework, OSGi & Agile Engineering Practices

44

Herzlichen Dank!

Patrick Baumgartner, @patbaumgartner

patrick.baumgartner [at] swiftmind [dot] com

http://www.swiftmind.com @swiftmind