codecentric AG 1
MongoDB Einführung
Tobias.Trelle@codecentric.de
@tobiastrelle
codecentric AG 2
 Senior IT Consultant @ codecentric AG
 Java / JEE / EAI / RDBMS background
 Committer @ Spring Data MongoDB
 Organizer of MongoDB User Group Düsseldorf
ttrelle @tobiastrelle
codecentric AG 3
MongoDB
humongous, engl. = gigantisch, riesig
codecentric AG 4
„It‘s not my fault the chapters are
short, MongoDB is just easy to learn“
aus „The Little MongoDB book“
codecentric AG 5
MongoDB?
 NoSQL-Datenbank / Open Source
 Dokumentenorientiert
 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
Relationales
Pendant Aber …
Flexibles
Schema
- Arrays
- Rekursiv
codecentric AG 7
MongoDB-Server Struktur
codecentric AG 8
MongoDB-Server: Storage
codecentric AG 9
Document
{ "_id" : ObjectId("53e3663ccb3bd259f9252f67"),
"typ" : ["gastro", "kultur"],
"name" : "Unperfekthaus",
"tags" : "uph unperfekt perfekt haus essen",
"desc" : "Im Unperfekthaus bekommen Künstler & Gründer ... ",
"adresse" : { "str" : "Friedrich-Ebert-Straße 18",
"plz" : 45127,
"ort" : "Essen"
},
"location" : { "type" : "Point",
"coordinates" : [ 7.0075, 51.45902 ]
}
}
codecentric AG 10
Document
 Document = Geordnete Menge von Key/Value-Paaren
 Key = UTF-8-String
 Value = Einfacher Datentyp | Array[Value] | Document
 Einfacher Datentyp = String
| Integer32
| Integer64
| Double
| Timestamp
…
codecentric AG 11
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 12
BSON Beispiel
{ hello: "MongoDB" }
x18x00x00x00
x02
hellox00
x08x00x00x00MongoDBx00
x00
codecentric AG 13
CRUD = IFUR
insert(…)
find(…), findOne(…)
update(…)
remove()
codecentric AG 14
Indexe
 Primary Key auf Feld _id: 12 Byte lange ObjectId
 Bis zu 63 zusätzliche Sekundär-Indexe
 Sekundär-Index einfach oder auf bis zu 31 Feldern
 Spezielle Indexe für
 Geodaten-Suche
 Volltextsuche
codecentric AG 15
Queries
 Operieren auf Daten genau einer Collection
 Query bei Example
 db.pois.find( {"adresse.stadt": "Bochum"} )
 Aggregation
 Map/Reduce
codecentric AG 16
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änden
und Shapes
 Details:
http://blog.codecentric.de/en/2012/02/spring-data-mongodb-geospatial-queries/
codecentric AG 22
MongoDB Replikation: Replica Sets
Grundprinzip
 Dynamisches Master/Slave + Auto Election
(Master = Primary, Slave = Secondary)
 Secondaries wählen automatisch neuen Primary bei Ausfall
 Writes nur auf Primary
 Reads ggfs. auch von Secondaries (Eventual Consistency)
Ziel: Ausfallsicherheit
codecentric AG 23
MongoDB Replikation: Replica Sets
codecentric AG 24
MongoDB Replikation: Replica Sets
codecentric AG 25
MongoDB Replikation: Replica Sets
WriteConcern
Gewünschte Grad an Durabilität bei Schreiboperationen
 Anzahl an Replica Set Members (n oder „majority“)
 Timeout
 Warten auf Journaling (true/false)

codecentric AG 26
MongoDB Replikation: Sharding
Grundprinzip
 Verteilung der Datenmenge auf n Knoten (Partitionierung)
 Jedes Dokument landet auf genau einem Knoten
 Zugriff über sog. ShardKey
(identifiziert Knoten für Lese- und Schreibzugriff)
Ziel: Horizontale Skalierbarkeit

codecentric AG 27
MongoDB Replikation: Sharding
Shard Key

codecentric AG 28
MongoDB Replikation: Sharding
Server-Prozesse
 Konfig-Server:
Buchhaltung von Shards
und Chunks
 Router:
Scatter/Gather bei Queries
Ziel Shard für Inserts
 Shard:
Eigentliche Datenhaltung

codecentric AG 29
MongoDB Replikation: Sharding
Produktives System

codecentric AG 30
MongoDB Replikation: Sharding
Horizontale Skalierung von Writes und Reads
 Writes treffen genau einen Shard
 Reads mit Shard Key  Subset an Shards
 Reads ohne Shard Key  Scatter/Gather auf allen Shards

codecentric AG 32
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 33
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
codecentric AG 34
QUESTIONS?
Tobias Trelle
codecentric AG
Merscheider Str. 1
42699 Solingen
tel +49 (0) 212.233628.47
fax +49 (0) 212.233628.79
mail Tobias.Trelle@codecentric.de
twitter @tobiastrelle
www.codecentric.de
blog.codecentric.de/en/author/tobias-trelle
www.xing.com/net/mongodb-dus

MongoDB Einführung

  • 1.
    codecentric AG 1 MongoDBEinführung Tobias.Trelle@codecentric.de @tobiastrelle
  • 2.
    codecentric AG 2 Senior IT Consultant @ codecentric AG  Java / JEE / EAI / RDBMS background  Committer @ Spring Data MongoDB  Organizer of MongoDB User Group Düsseldorf ttrelle @tobiastrelle
  • 3.
    codecentric AG 3 MongoDB humongous,engl. = gigantisch, riesig
  • 4.
    codecentric AG 4 „It‘snot my fault the chapters are short, MongoDB is just easy to learn“ aus „The Little MongoDB book“
  • 5.
    codecentric AG 5 MongoDB? NoSQL-Datenbank / Open Source  Dokumentenorientiert  Hochperformant, horizontal skalierbar (scale-out)  Replication & Sharding out-of-the-box  Map/Reduce  Geospatial Indexes / Queries
  • 6.
    codecentric AG 6 GrundkonzeptMongoDB-Server Server Database Collection Document Field Tabelle Zeile Spalte Relationales Pendant Aber … Flexibles Schema - Arrays - Rekursiv
  • 7.
  • 8.
  • 9.
    codecentric AG 9 Document {"_id" : ObjectId("53e3663ccb3bd259f9252f67"), "typ" : ["gastro", "kultur"], "name" : "Unperfekthaus", "tags" : "uph unperfekt perfekt haus essen", "desc" : "Im Unperfekthaus bekommen Künstler & Gründer ... ", "adresse" : { "str" : "Friedrich-Ebert-Straße 18", "plz" : 45127, "ort" : "Essen" }, "location" : { "type" : "Point", "coordinates" : [ 7.0075, 51.45902 ] } }
  • 10.
    codecentric AG 10 Document Document = Geordnete Menge von Key/Value-Paaren  Key = UTF-8-String  Value = Einfacher Datentyp | Array[Value] | Document  Einfacher Datentyp = String | Integer32 | Integer64 | Double | Timestamp …
  • 11.
    codecentric AG 11 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!
  • 12.
    codecentric AG 12 BSONBeispiel { hello: "MongoDB" } x18x00x00x00 x02 hellox00 x08x00x00x00MongoDBx00 x00
  • 13.
    codecentric AG 13 CRUD= IFUR insert(…) find(…), findOne(…) update(…) remove()
  • 14.
    codecentric AG 14 Indexe Primary Key auf Feld _id: 12 Byte lange ObjectId  Bis zu 63 zusätzliche Sekundär-Indexe  Sekundär-Index einfach oder auf bis zu 31 Feldern  Spezielle Indexe für  Geodaten-Suche  Volltextsuche
  • 15.
    codecentric AG 15 Queries Operieren auf Daten genau einer Collection  Query bei Example  db.pois.find( {"adresse.stadt": "Bochum"} )  Aggregation  Map/Reduce
  • 16.
    codecentric AG 16 GeospatialQueries  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änden und Shapes  Details: http://blog.codecentric.de/en/2012/02/spring-data-mongodb-geospatial-queries/
  • 17.
    codecentric AG 22 MongoDBReplikation: Replica Sets Grundprinzip  Dynamisches Master/Slave + Auto Election (Master = Primary, Slave = Secondary)  Secondaries wählen automatisch neuen Primary bei Ausfall  Writes nur auf Primary  Reads ggfs. auch von Secondaries (Eventual Consistency) Ziel: Ausfallsicherheit
  • 18.
    codecentric AG 23 MongoDBReplikation: Replica Sets
  • 19.
    codecentric AG 24 MongoDBReplikation: Replica Sets
  • 20.
    codecentric AG 25 MongoDBReplikation: Replica Sets WriteConcern Gewünschte Grad an Durabilität bei Schreiboperationen  Anzahl an Replica Set Members (n oder „majority“)  Timeout  Warten auf Journaling (true/false) 
  • 21.
    codecentric AG 26 MongoDBReplikation: Sharding Grundprinzip  Verteilung der Datenmenge auf n Knoten (Partitionierung)  Jedes Dokument landet auf genau einem Knoten  Zugriff über sog. ShardKey (identifiziert Knoten für Lese- und Schreibzugriff) Ziel: Horizontale Skalierbarkeit 
  • 22.
    codecentric AG 27 MongoDBReplikation: Sharding Shard Key 
  • 23.
    codecentric AG 28 MongoDBReplikation: Sharding Server-Prozesse  Konfig-Server: Buchhaltung von Shards und Chunks  Router: Scatter/Gather bei Queries Ziel Shard für Inserts  Shard: Eigentliche Datenhaltung 
  • 24.
    codecentric AG 29 MongoDBReplikation: Sharding Produktives System 
  • 25.
    codecentric AG 30 MongoDBReplikation: Sharding Horizontale Skalierung von Writes und Reads  Writes treffen genau einen Shard  Reads mit Shard Key  Subset an Shards  Reads ohne Shard Key  Scatter/Gather auf allen Shards 
  • 26.
    codecentric AG 32 MongoDBSharding 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
  • 27.
    codecentric AG 33 MongoDBSharding 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
  • 28.
    codecentric AG 34 QUESTIONS? TobiasTrelle codecentric AG Merscheider Str. 1 42699 Solingen tel +49 (0) 212.233628.47 fax +49 (0) 212.233628.79 mail Tobias.Trelle@codecentric.de twitter @tobiastrelle www.codecentric.de blog.codecentric.de/en/author/tobias-trelle www.xing.com/net/mongodb-dus

Hinweis der Redaktion

  • #4 3
  • #5 Was ist „Soft State“?
  • #6 Was ist „Soft State“?
  • #7 Was ist „Soft State“?
  • #8 Was ist „Soft State“?
  • #9 Was ist „Soft State“?
  • #14 Was ist „Soft State“?
  • #17 Was ist „Soft State“?
  • #18 Was ist „Soft State“?
  • #19 Was ist „Soft State“?
  • #20 Was ist „Soft State“?
  • #21 Was ist „Soft State“?
  • #22 Was ist „Soft State“?
  • #23 Was ist „Soft State“?
  • #24 Was ist „Soft State“?
  • #25 Was ist „Soft State“?
  • #26 Was ist „Soft State“?
  • #27 Was ist „Soft State“?
  • #28 Was ist „Soft State“?
  • #29 Was ist „Soft State“?
  • #30 Was ist „Soft State“?
  • #31 Was ist „Soft State“?
  • #32 Was ist „Soft State“?
  • #33 Was ist „Soft State“?
  • #34 Was ist „Soft State“?