MongoDB OOP 2013 - sigs.desigs.de/download/oop_2013/files/Do1-1-1_Trelle.pdf · 1 codecentric AG 1...

15
1 codecentric AG 1 OOP 2013: Praktische Einführung in MongoDB [email protected] @tobiastrelle codecentric AG 2 Tobias Trelle - Senior IT Consultant codecentric AG - MongoDB User-Gruppe Düsseldorf - MongoDB-Buch

Transcript of MongoDB OOP 2013 - sigs.desigs.de/download/oop_2013/files/Do1-1-1_Trelle.pdf · 1 codecentric AG 1...

Page 1: MongoDB OOP 2013 - sigs.desigs.de/download/oop_2013/files/Do1-1-1_Trelle.pdf · 1 codecentric AG 1 OOP 2013: Praktische Einführung ...

1

codecentric AG 1

OOP 2013:Praktische Einführung in MongoDB

[email protected]

@tobiastrelle

codecentric AG 2

Tobias Trelle

- Senior IT Consultant codecentric AG

- MongoDB User-Gruppe Düsseldorf

- MongoDB-Buch

Page 2: MongoDB OOP 2013 - sigs.desigs.de/download/oop_2013/files/Do1-1-1_Trelle.pdf · 1 codecentric AG 1 OOP 2013: Praktische Einführung ...

2

codecentric AG 3

„It‘s not my fault the chapters are

short, MongoDB is just easy to learn“

aus „The Little MongoDB book“

codecentric AG 4

MongoDB?

„humongous“ = gigantisch, wahnsinnig groß

http://www.mongodb.org

Page 3: MongoDB OOP 2013 - sigs.desigs.de/download/oop_2013/files/Do1-1-1_Trelle.pdf · 1 codecentric AG 1 OOP 2013: Praktische Einführung ...

3

codecentric AG 5

MongoDB?

− NoSQL-Datenbank

− Dokumenten-orientiert

− Hochperformant, horizontal skalierbar (scale-out)

− Replication & Sharding out-of-the-box

− Map/Reduce

− Geospatial Indexes / Queries

codecentric AG 6

Grundkonzept MongoDB-Server

Server

Database

Collection

Document

Field

Tabelle

Zeile

Spalte

RelationalesPendant Aber …

Flexibles Schema

- Arrays- Rekursiv

Page 4: MongoDB OOP 2013 - sigs.desigs.de/download/oop_2013/files/Do1-1-1_Trelle.pdf · 1 codecentric AG 1 OOP 2013: Praktische Einführung ...

4

codecentric AG 7

Document

{

title: „Praxisbuch Mongo“,

version: 0.1,

copies_sold: 0,

authors: [

{ name: „Uwe Seiler“,

email: „[email protected]“ },

{ name: „Tobias Trelle“,

email: „[email protected]“}

]

}

codecentric AG 8

Document

− Document = Geordnete Menge von Key/Value-Paaren

− Key = UTF-String

− Value = Einfacher Datentyp | Array[Document] | Document

− Einfacher Datentyp = String| Integer32| Integer64| Double| Timestamp…

Page 5: MongoDB OOP 2013 - sigs.desigs.de/download/oop_2013/files/Do1-1-1_Trelle.pdf · 1 codecentric AG 1 OOP 2013: Praktische Einführung ...

5

codecentric AG 9

BSON-Format

− Dokumente werden im BSON-Format verwaltet und gespeichert

http://bsonspec.org/#/specification

− BSON = Binary JSON (!= JSON)

− Teilweise größer als JSON, aber besser traversierbar

− Keys stehen in jedem Dokument --> kurz fassen!

codecentric AG 10

BSON Beispiel

{ hello: "MongoDB" }

\x18\x00\x00\x00

\x02

hello\x00

\x08\x00\x00\x00MongoDB\x00

\x00

Page 6: MongoDB OOP 2013 - sigs.desigs.de/download/oop_2013/files/Do1-1-1_Trelle.pdf · 1 codecentric AG 1 OOP 2013: Praktische Einführung ...

6

codecentric AG 11

Live Session

−CRUD Operations

−Queries

−Geospatial Queries

−Map/Reduce

−Replication

−Sharding

−Raw Java API & Spring Data API

codecentric AG 12

CRUD = IFUR

insert(…)

find(…), findOne(…)

update(…)

remove()

Page 7: MongoDB OOP 2013 - sigs.desigs.de/download/oop_2013/files/Do1-1-1_Trelle.pdf · 1 codecentric AG 1 OOP 2013: Praktische Einführung ...

7

codecentric AG 13

Geospatial Queries

− Index auf 2-dimensionalen Koordinaten

− _id: "A", position: [0.001, -0.002]_id: "B", position: [0.75, 0.75]_id: "C", position: [0.5, 0.5]_id: "D", position: [-0.5, -0.5]

− Queries basieren auf Abständenund Shapes

− Details:http://blog.codecentric.de/en/2012/02/spring-data-mongodb-geospatial-queries/

codecentric AG 14

Map/Reduce

−Algorithmus/Framework für Berechnungen auf Datenmengen basierend auf zwei Phasen:

Map & Reduce

−Die Map-Phase kann nebenläufig und verteilt (im Cluster) auf großen Datenmengen (Petabytes) ausgeführt werden

� Apache Hadoop

Page 8: MongoDB OOP 2013 - sigs.desigs.de/download/oop_2013/files/Do1-1-1_Trelle.pdf · 1 codecentric AG 1 OOP 2013: Praktische Einführung ...

8

codecentric AG 15

Map/Reduce mit MongoDB

−Wird auf einer Untermenge / allen Dokumenten einer Collection ausgeführt

−Map / Reduce-Funktionen sind in JavaScript implementiert

−Ergebnisse sind Dokumente in einer Ziel-Collection

codecentric AG 16

Map/Reduce Beispiel

− Aufgabe: Vorkommen von Tags zählen:

{name: „Doc 1“, tags: [ „cc“, „mongodb“, „nosql“ ] }{name: „Doc 2“, tags: [ „cc“, „agile“ ] }{name: „Doc 3“, tags: [ „cc“, „nosql“ ] }

− map = function() { this.tags.forEach( function(tag) {emit( tag, 1 );})

}

− reduce = function(key, values) {var result = 0;values.forEach(function(value) {

result += value; });return result;

}

Map output:

key = „cc“, value = 1

key = „mongodb“, value = 1

key = „nosql“, value = 1

key = „cc“, value = 1

key = „agile“, value = 1

key = „cc“, value = 1

key = „nosql“, value = 1

Reduce input:

key = „cc“, values = [ 1, 1, 1 ]

key = „mongodb“, values = [ 1 ]

key = „nosql“, values = [ 1,1 ]

key = „agile“, values = [ 1 ]

Page 9: MongoDB OOP 2013 - sigs.desigs.de/download/oop_2013/files/Do1-1-1_Trelle.pdf · 1 codecentric AG 1 OOP 2013: Praktische Einführung ...

9

codecentric AG 17

Aggregation Framework

−Seit Version 2.2

−Alternative zu Map/Reduce

−db.docs.aggregate( {$project:{_id:0,tags:1}}, {$unwind: "$tags"}, {$group:{_id:"$tags", n:{$sum:1}}}

)

codecentric AG 18

MongoDB Replikation: Replica Sets

−Grundprinzip: Master/Slave + Election

−Writes nur auf den Master, Reads ggfs. auch von Slaves

−Slaves wählen automatisch neuen Master bei Ausfall

Replica Set, n = 3

Master

Slave 1

Slave 2

Client

Page 10: MongoDB OOP 2013 - sigs.desigs.de/download/oop_2013/files/Do1-1-1_Trelle.pdf · 1 codecentric AG 1 OOP 2013: Praktische Einführung ...

10

codecentric AG 19

MongoDB Sharding

−Fragmentierung der Daten auf n Sharding-Knoten,jedes Dokument wird einmal persistiert

−Config Server = Buchhalter für Meta-Daten

−Switch: Gateway für die Clients Sharding Setup

Switch

Shard 1

Shard 2Client

Config

Server

codecentric AG 20

Node 2 Node 3

MongoDB Produktion: Sharding + Replica Sets

Node 1

<<Switch>>

mongos

App Server

<<Config>>

mongod<<Config>>

mongod

<<Config>>

mongod

Shard 1

Shard 2<<Master>>

mongod

<<Slave>>

mongod

<<Slave>>

mongod<<Slave>>

mongod

<<Master>>

mongod<<Slave>>

mongod

<<Switch>>

mongos

App Server

<<Switch>>

mongos

App Server

Page 11: MongoDB OOP 2013 - sigs.desigs.de/download/oop_2013/files/Do1-1-1_Trelle.pdf · 1 codecentric AG 1 OOP 2013: Praktische Einführung ...

11

codecentric AG 21

MongoDB Sharding Beispiel: Initialer Zustand

mongos> sh.status()

--- Sharding Status ---

sharding version: { "_id" : 1, "version" : 3 }

shards:

{ "_id" : "shard0000", "host" : "tmp-pc:9000" }

{ "_id" : "shard0001", "host" : "tmp-pc:9001" }

databases:

{ "_id" : "admin", "partitioned" : false, "primary" : "config" }

{ "_id" : "data", "partitioned" : true, "primary" : "shard0000" }

data.foo chunks:

shard0000 1

{ "age" : { $minKey : 1 } } -->> { "age" : { $maxKey : 1 } } on : shard0000 { "t" : 1000,

"i" : 0 }

2 Shards

codecentric AG 22

MongoDB Sharding Beispiel: Chunks verteilen sich

mongos> sh.status()

--- Sharding Status ---

sharding version: { "_id" : 1, "version" : 3 }

shards:

{ "_id" : "shard0000", "host" : "tmp-pc:9000" }

{ "_id" : "shard0001", "host" : "tmp-pc:9001" }

databases:

{ "_id" : "admin", "partitioned" : false, "primary" : "config" }

{ "_id" : "data", "partitioned" : true, "primary" : "shard0000" }

data.foo chunks:

shard0001 4

shard0000 5

{ "age" : { $minKey : 1 } } -->> { "age" : 50 } on : shard0001 { "t" : 2000, "i" : 0 }

{ "age" : 50 } -->> { "age" : 53 } on : shard0001 { "t" : 3000, "i" : 0 }

{ "age" : 53 } -->> { "age" : 54 } on : shard0001 { "t" : 4000, "i" : 0 }

{ "age" : 54 } -->> { "age" : 58 } on : shard0001 { "t" : 5000, "i" : 0 }

{ "age" : 58 } -->> { "age" : 60 } on : shard0000 { "t" : 5000, "i" : 1 }

{ "age" : 60 } -->> { "age" : 63 } on : shard0000 { "t" : 1000, "i" : 14 }

{ "age" : 63 } -->> { "age" : 65 } on : shard0000 { "t" : 1000, "i" : 11 }

{ "age" : 65 } -->> { "age" : 69 } on : shard0000 { "t" : 1000, "i" : 12 }

{ "age" : 69 } -->> { "age" : { $maxKey : 1 } } on : shard0000 { "t" : 1000, "i" : 4 }

2 Shards

Chunks

are equally

distributed

Page 12: MongoDB OOP 2013 - sigs.desigs.de/download/oop_2013/files/Do1-1-1_Trelle.pdf · 1 codecentric AG 1 OOP 2013: Praktische Einführung ...

12

codecentric AG 23

MongoDB Treiber

− Ein Wire-Protokoll für alle Client-Sprachen

− Pro Sprache ein Treiber

− Übersicht: http://www.mongodb.org/display/DOCS/Drivers

codecentric AG 24

MongoDB Treiber

Beispiele

Sprache Struktur

JavaScript, Python {“hello": “MongoDB" }

Ruby, Perl {“hello" => “MongoDB" }

C++ BSONObj p = BSON(“hello" << “MongoDB");

Java DBObject obj = new BasicDBObject();

obj.put(“hello", “MongoDB");

Page 13: MongoDB OOP 2013 - sigs.desigs.de/download/oop_2013/files/Do1-1-1_Trelle.pdf · 1 codecentric AG 1 OOP 2013: Praktische Einführung ...

13

codecentric AG 25

MongoDB Treibers

Wesentliche Aufgaben eines Treibers

− Konvertierung der sprachabh. Datenstrukturen � BSON-Format

− Generierung der _id-Werte

− Treiber macht möglichst viel, um Server zu entlasten

codecentric AG 26

MongoDB Java-Treiber

− Einzelnes JAR ohne weitere Abhängigkeiten

<dependency>

<groupId>org.mongodb</groupId>

<artifactId>mongo-java-driver</artifactId>

<version>2.9.3</version>

</dependency>

− github: https://github.com/mongodb/mongo-java-driver

Page 14: MongoDB OOP 2013 - sigs.desigs.de/download/oop_2013/files/Do1-1-1_Trelle.pdf · 1 codecentric AG 1 OOP 2013: Praktische Einführung ...

14

codecentric AG 27

Spring Data MongoDB

Spring Data

RDBMS MongoDB Neo4j …

Spring Data

JPA

CrudRepository PagingAndSortingRepository

JpaRepository

JPA

JDBC

Spring Data

MongoDB

MongoRepository

MongoTemplate

Spring Data

Neo4j

Spring Data

GraphRepository

Neo4jTemplate

Mongo Java Driver

Embedded REST

codecentric AG 28

Spring Data MongoDB

Template

− Abstraktion vom MongoDB Java Driver

− Konfiguration von mongod / mongos-Instanzen und Collections

− Verwaltung von Collections

− Map/Reduce-Aufrufe

Object Mapping

− Annotationen @Document, @Field, @Index etc.

− Klassen werden auf Collections abgebildet, Java-Objekte auf Dokumente

Repository Support

− Schlüsselwörter werden auf Query-Operatoren abgebildet

− Geospatial Queries

Page 15: MongoDB OOP 2013 - sigs.desigs.de/download/oop_2013/files/Do1-1-1_Trelle.pdf · 1 codecentric AG 1 OOP 2013: Praktische Einführung ...

15

codecentric AG 29

MongoDB User-Gruppe Düsseldorf

https://www.xing.com/net/mongodb-dus

@MongoDUS

MongoDB User-Gruppe Frankfurt/Main

https://www.xing.com/net/mongodb-ffm

@MongoFFM

MongoDB User-Gruppe München

http://www.meetup.com/Muenchen-MongoDB-User-Group/

@mongomuc

MongoDB User-Gruppen (MUGs)

MongoDB User-Gruppe Berlin

http://www.meetup.com/MUGBerlin/

@MUGBerlin

Hamburg MongoDB User Grouphttps://www.xing.com/net/mugh

codecentric AG 30

QUESTION?

Tobias Trelle

codecentric AGMerscheider Str. 142699 Solingen

tel +49 (0) 212.233628.47fax +49 (0) 212.233628.79mail [email protected]

twitter @tobiastrelle

www.codecentric.de

blog.codecentric.de/en/author/tobias-trelle

www.xing.com/net/mongodb-dus