mongodb einführung
TRANSCRIPT
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 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
RelationalesPendant Aber …
Flexibles Schema
- Arrays- Rekursiv
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" }
\x18\x00\x00\x00\x02
hello\x00\x08\x00\x00\x00MongoDB\x00
\x00
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ändenund 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 25
MongoDB Replikation: Replica Sets
WriteConcernGewü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 28
MongoDB Replikation: Sharding
Server-Prozesse
Konfig-Server:Buchhaltung von Shardsund Chunks
Router:Scatter/Gather bei QueriesZiel Shard für Inserts
Shard:Eigentliche Datenhaltung
-
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
Chunksare equally distributed
codecentric AG 34
QUESTIONS?
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