building your first java application with mongodb
DESCRIPTION
This webinar will introduce how to build your first Java application with MongoDB by walking you through how one can build a simple location based application. The talk will cover the basics of MongoDB's document model, query language, aggregation framework and deployment architecture. New features, fixes and improvements in the latest release will also be covered.TRANSCRIPT
![Page 2: Building your first Java Application with MongoDB](https://reader036.vdocuments.site/reader036/viewer/2022081413/547afe2bb4af9f4c738b462c/html5/thumbnails/2.jpg)
is a…
• High performance• Highly available • Easily scalable• Easy to use• Feature rich
Document store
![Page 3: Building your first Java Application with MongoDB](https://reader036.vdocuments.site/reader036/viewer/2022081413/547afe2bb4af9f4c738b462c/html5/thumbnails/3.jpg)
Data Model
• A Mongo system holds a set of databases
• A database holds a set of collections
• A collection holds a set of documents
• A document is a set of fields
• A field is a key-value pair
• A key is a name (string)
• A value is a
basic type like string, integer, float, timestamp, binary, etc.,
a document, or
an array of values
![Page 4: Building your first Java Application with MongoDB](https://reader036.vdocuments.site/reader036/viewer/2022081413/547afe2bb4af9f4c738b462c/html5/thumbnails/4.jpg)
High Availability: Replica Sets
• Initialize -> Election
• Primary + data replication from primary to secondary
Node 1Secondar
y
Node 2Secondar
y
Node 3Primary ReplicationReplication
Heartbeat
![Page 5: Building your first Java Application with MongoDB](https://reader036.vdocuments.site/reader036/viewer/2022081413/547afe2bb4af9f4c738b462c/html5/thumbnails/5.jpg)
Replica Set - Failure
• Primary down/network failure
• Automatic election of new primary if majority exists
Node 1Secondar
y
Node 2Secondar
y
Node 3Primary
Heartbeat
Primary Election
![Page 6: Building your first Java Application with MongoDB](https://reader036.vdocuments.site/reader036/viewer/2022081413/547afe2bb4af9f4c738b462c/html5/thumbnails/6.jpg)
Replica Set - Failover
• New primary elected
• Replication established from new primary
Node 1Secondar
y
Node 2Primary
Node 3Primary
Heartbeat
![Page 7: Building your first Java Application with MongoDB](https://reader036.vdocuments.site/reader036/viewer/2022081413/547afe2bb4af9f4c738b462c/html5/thumbnails/7.jpg)
Durability
• Fire and forget• Wait for error • Wait for journal sync • Wait for flush to disk• Wait for replication
![Page 8: Building your first Java Application with MongoDB](https://reader036.vdocuments.site/reader036/viewer/2022081413/547afe2bb4af9f4c738b462c/html5/thumbnails/8.jpg)
Read Preferences
• PRIMARY • PRIMARY PREFERRED • SECONDARY • SECONDARY PREFERRED • NEAREST
![Page 9: Building your first Java Application with MongoDB](https://reader036.vdocuments.site/reader036/viewer/2022081413/547afe2bb4af9f4c738b462c/html5/thumbnails/9.jpg)
Let’s build a location based surf reporting app!
![Page 10: Building your first Java Application with MongoDB](https://reader036.vdocuments.site/reader036/viewer/2022081413/547afe2bb4af9f4c738b462c/html5/thumbnails/10.jpg)
Let’s build a location based surf reporting app!
• Report current conditions
![Page 11: Building your first Java Application with MongoDB](https://reader036.vdocuments.site/reader036/viewer/2022081413/547afe2bb4af9f4c738b462c/html5/thumbnails/11.jpg)
Let’s build a location based surf reporting app!
• Report current conditions• Get current local conditions
![Page 12: Building your first Java Application with MongoDB](https://reader036.vdocuments.site/reader036/viewer/2022081413/547afe2bb4af9f4c738b462c/html5/thumbnails/12.jpg)
Let’s build a location based surf reporting app!
• Report current conditions• Get current local conditions • Determine best conditions per beach
![Page 13: Building your first Java Application with MongoDB](https://reader036.vdocuments.site/reader036/viewer/2022081413/547afe2bb4af9f4c738b462c/html5/thumbnails/13.jpg)
Document Structure{
"_id" : ObjectId("504ceb3d30042d707af96fef"),"reporter" : "test","location" : {
"coordinates" : [-122.477222,37.810556
],"name" : "Fort Point"
},"conditions" : {
"height" : 0,"period" : 9,"rating" : 1
},"date" : ISODate("2011-11-16T20:17:17.277Z")
}
![Page 14: Building your first Java Application with MongoDB](https://reader036.vdocuments.site/reader036/viewer/2022081413/547afe2bb4af9f4c738b462c/html5/thumbnails/14.jpg)
Document Structure{
"_id" : ObjectId("504ceb3d30042d707af96fef"),"reporter" : "test","location" : {
"coordinates" : [-122.477222,37.810556
],"name" : "Fort Point"
},"conditions" : {
"height" : 0,"period" : 9,"rating" : 1
},"date" : ISODate("2011-11-16T20:17:17.277Z")
}
Primary Key, Unique, Auto-indexed
![Page 15: Building your first Java Application with MongoDB](https://reader036.vdocuments.site/reader036/viewer/2022081413/547afe2bb4af9f4c738b462c/html5/thumbnails/15.jpg)
Document Structure{
"_id" : ObjectId("504ceb3d30042d707af96fef"),"reporter" : "test","location" : {
"coordinates" : [-122.477222,37.810556
],"name" : "Fort Point"
},"conditions" : {
"height" : 0,"period" : 9,"rating" : 1
},"date" : ISODate("2011-11-16T20:17:17.277Z")
}
Primary Key, Unique, Autoindexed
Compound Index,Geospacial
![Page 16: Building your first Java Application with MongoDB](https://reader036.vdocuments.site/reader036/viewer/2022081413/547afe2bb4af9f4c738b462c/html5/thumbnails/16.jpg)
Document Structure{
"_id" : ObjectId("504ceb3d30042d707af96fef"),"reporter" : "test","location" : {
"coordinates" : [-122.477222,37.810556
],"name" : "Fort Point"
},"conditions" : {
"height" : 0,"period" : 9,"rating" : 1
},"date" : ISODate("2011-11-16T20:17:17.277Z")
}
Primary Key, Unique, Autoindexed
Compound Index,Geospacial
Indexed forTime-To-Live
![Page 17: Building your first Java Application with MongoDB](https://reader036.vdocuments.site/reader036/viewer/2022081413/547afe2bb4af9f4c738b462c/html5/thumbnails/17.jpg)
Get local surf conditionsdb.reports.find(
{"location.coordinates" : { $near : [-122, 37] , $maxDistance : 0.9}, date : { $gte : new Date(2012, 8, 9)} }, {"date" : 1, "location.name" :1, _id : 0,
"conditions" :1}).sort({"conditions.rating" : -1})
![Page 18: Building your first Java Application with MongoDB](https://reader036.vdocuments.site/reader036/viewer/2022081413/547afe2bb4af9f4c738b462c/html5/thumbnails/18.jpg)
Get local surf conditionsdb.reports.find(
{"location.coordinates" : { $near : [-122, 37] , $maxDistance : 0.9}, date : { $gte : new Date(2012, 8, 9)} }, {"date" : 1, "location.name" :1, _id : 0,
"conditions" :1}).sort({"conditions.rating" : -1})
• Get local reports
![Page 19: Building your first Java Application with MongoDB](https://reader036.vdocuments.site/reader036/viewer/2022081413/547afe2bb4af9f4c738b462c/html5/thumbnails/19.jpg)
Get local surf conditionsdb.reports.find(
{"location.coordinates" : { $near : [-122, 37] , $maxDistance : 0.9}, date : { $gte : new Date(2012, 8, 9)} }, {"date" : 1, "location.name" :1, _id : 0,
"conditions" :1}).sort({"conditions.rating" : -1})
• Get local reports• Get today’s reports
![Page 20: Building your first Java Application with MongoDB](https://reader036.vdocuments.site/reader036/viewer/2022081413/547afe2bb4af9f4c738b462c/html5/thumbnails/20.jpg)
Get local surf conditionsdb.reports.find(
{"location.coordinates" : { $near : [-122, 37] , $maxDistance : 0.9}, date : { $gte : new Date(2012, 8, 9)} }, {"location.name" :1, _id : 0, "conditions" :1}
).sort({"conditions.rating" : -1})
• Get local reports• Get today’s reports• Return only the relevant info
![Page 21: Building your first Java Application with MongoDB](https://reader036.vdocuments.site/reader036/viewer/2022081413/547afe2bb4af9f4c738b462c/html5/thumbnails/21.jpg)
Get local surf conditionsdb.reports.find(
{"location.coordinates" : { $near : [-122, 37] , $maxDistance : 0.9}, date : { $gte : new Date(2012, 8, 9)} }, {"location.name" :1, _id : 0, "conditions" :1}
).sort({"conditions.rating" : -1})
• Get local reports• Get today’s reports• Return only the relevant info• Show me the best surf first
![Page 22: Building your first Java Application with MongoDB](https://reader036.vdocuments.site/reader036/viewer/2022081413/547afe2bb4af9f4c738b462c/html5/thumbnails/22.jpg)
Get local surf conditions: Connecting
![Page 23: Building your first Java Application with MongoDB](https://reader036.vdocuments.site/reader036/viewer/2022081413/547afe2bb4af9f4c738b462c/html5/thumbnails/23.jpg)
DBObjects
Output:
{ "name" : "test"}parsed
![Page 24: Building your first Java Application with MongoDB](https://reader036.vdocuments.site/reader036/viewer/2022081413/547afe2bb4af9f4c738b462c/html5/thumbnails/24.jpg)
Building the query
![Page 25: Building your first Java Application with MongoDB](https://reader036.vdocuments.site/reader036/viewer/2022081413/547afe2bb4af9f4c738b462c/html5/thumbnails/25.jpg)
Results{ "location" : { "name" : "Montara" }, "conditions" : { "height" : 6, "period" : 20, "rating" : 5 } }{ "location" : { "name" : "Maverick's" }, "conditions" : { "height" : 5, "period" : 13, "rating" : 3 } }{ "location" : { "name" : "Maverick's" }, "conditions" : { "height" : 3, "period" : 15, "rating" : 3 } }{ "location" : { "name" : "Maverick's" }, "conditions" : { "height" : 3, "period" : 16, "rating" : 2 } }{ "location" : { "name" : "Montara" }, "conditions" : { "height" : 0, "period" : 8, "rating" : 1 } }{ "location" : { "name" : "Linda Mar" }, "conditions" : { "height" : 3, "period" : 10, "rating" : 1 } }{ "location" : { "name" : "Sharp Park" }, "conditions" : { "height" : 1, "period" : 15, "rating" : 1 } }{ "location" : { "name" : "Sharp Park" }, "conditions" : { "height" : 5, "period" : 6, "rating" : 1 } }{ "location" : { "name" : "South Ocean Beach" }, "conditions" : { "height" : 1, "period" : 6, "rating" : 1 } }{ "location" : { "name" : "South Ocean Beach" }, "conditions" : { "height" : 0, "period" : 10, "rating" : 1 } }{ "location" : { "name" : "South Ocean Beach" }, "conditions" : { "height" : 4, "period" : 6, "rating" : 1 } }{ "location" : { "name" : "South Ocean Beach" }, "conditions" : { "height" : 0, "period" : 14, "rating" : 1 } }
![Page 26: Building your first Java Application with MongoDB](https://reader036.vdocuments.site/reader036/viewer/2022081413/547afe2bb4af9f4c738b462c/html5/thumbnails/26.jpg)
Analysis Features:Aggregation Framework
What are the best conditions for my local beach?
![Page 27: Building your first Java Application with MongoDB](https://reader036.vdocuments.site/reader036/viewer/2022081413/547afe2bb4af9f4c738b462c/html5/thumbnails/27.jpg)
Pipelining Operations
$match
$project
$group
Match “Linda Mar”
Only interested in conditions
Group by rating, averagingwave height and wave period
$sort Order by best conditions
![Page 28: Building your first Java Application with MongoDB](https://reader036.vdocuments.site/reader036/viewer/2022081413/547afe2bb4af9f4c738b462c/html5/thumbnails/28.jpg)
Aggregation Framework{ "aggregate" : "reports" , "pipeline" : [ { "$match" : { "location.name" : "Linda Mar"}} , { "$project" : { "conditions" : 1}} , { "$group" : { "_id" : "$conditions.rating" , "average height" : { "$avg" : "$conditions.height"} , "average period" : { "$avg" : "$conditions.period"}}} , { "$sort" : { "_id" : -1}} ]}
![Page 29: Building your first Java Application with MongoDB](https://reader036.vdocuments.site/reader036/viewer/2022081413/547afe2bb4af9f4c738b462c/html5/thumbnails/29.jpg)
Aggregation Framework{ "aggregate" : "reports" , "pipeline" : [ { "$match" : { "location.name" : "Linda Mar"}} , { "$project" : { "conditions" : 1}} , { "$group" : { "_id" : "$conditions.rating" , "average height" : { "$avg" : "$conditions.height"} , "average period" : { "$avg" : "$conditions.period"}}} , { "$sort" : { "_id" : -1}} ]} Match “Linda Mar”
![Page 30: Building your first Java Application with MongoDB](https://reader036.vdocuments.site/reader036/viewer/2022081413/547afe2bb4af9f4c738b462c/html5/thumbnails/30.jpg)
Aggregation Framework{ "aggregate" : "reports" , "pipeline" : [ { "$match" : { "location.name" : "Linda Mar"}} , { "$project" : { "conditions" : 1}} , { "$group" : { "_id" : "$conditions.rating" , "average height" : { "$avg" : "$conditions.height"} , "average period" : { "$avg" : "$conditions.period"}}} , { "$sort" : { "_id" : -1}} ]} Only interested in conditions
![Page 31: Building your first Java Application with MongoDB](https://reader036.vdocuments.site/reader036/viewer/2022081413/547afe2bb4af9f4c738b462c/html5/thumbnails/31.jpg)
Aggregation Framework{ "aggregate" : "reports" , "pipeline" : [ { "$match" : { "location.name" : "Linda Mar"}} , { "$project" : { "conditions" : 1}} , { "$group" : { "_id" : "$conditions.rating" , "average height" : { "$avg" : "$conditions.height"} , "average period" : { "$avg" : "$conditions.period"}}} , { "$sort" : { "_id" : -1}} ]}Group by rating & average conditions
![Page 32: Building your first Java Application with MongoDB](https://reader036.vdocuments.site/reader036/viewer/2022081413/547afe2bb4af9f4c738b462c/html5/thumbnails/32.jpg)
Aggregation Framework{ "aggregate" : "reports" , "pipeline" : [ { "$match" : { "location.name" : "Linda Mar"}} , { "$project" : { "conditions" : 1}} , { "$group" : { "_id" : "$conditions.rating" , "average height" : { "$avg" : "$conditions.height"} , "average period" : { "$avg" : "$conditions.period"}}} , { "$sort" : { "_id" : -1}} ]} Show me best conditions first
![Page 33: Building your first Java Application with MongoDB](https://reader036.vdocuments.site/reader036/viewer/2022081413/547afe2bb4af9f4c738b462c/html5/thumbnails/33.jpg)
The Aggregation Helper
![Page 34: Building your first Java Application with MongoDB](https://reader036.vdocuments.site/reader036/viewer/2022081413/547afe2bb4af9f4c738b462c/html5/thumbnails/34.jpg)
• Sharding is the partitioning of data among multiple machines
• Balancing occurs when the load on any one node grows out of proportion
Scaling
![Page 35: Building your first Java Application with MongoDB](https://reader036.vdocuments.site/reader036/viewer/2022081413/547afe2bb4af9f4c738b462c/html5/thumbnails/35.jpg)
Scaling MongoDB
Client Applicatio
n
Single InstanceOr
Replica Set
MongoDB
Sharded cluster
![Page 36: Building your first Java Application with MongoDB](https://reader036.vdocuments.site/reader036/viewer/2022081413/547afe2bb4af9f4c738b462c/html5/thumbnails/36.jpg)
The Mechanism of Sharding
Complete Data Set
Maverick’s RockawayFort Point Ocean BeachLinda Mar
Define Shard Key on Location Name
![Page 37: Building your first Java Application with MongoDB](https://reader036.vdocuments.site/reader036/viewer/2022081413/547afe2bb4af9f4c738b462c/html5/thumbnails/37.jpg)
The Mechanism of Sharding
Chunk
Define Shard Key on Location Name
Chunk
Maverick’s RockawayFort Point Ocean BeachLinda Mar
![Page 38: Building your first Java Application with MongoDB](https://reader036.vdocuments.site/reader036/viewer/2022081413/547afe2bb4af9f4c738b462c/html5/thumbnails/38.jpg)
The Mechanism of Sharding
Chunk Chunk ChunkChunk
Maverick’s RockawayFort Point Ocean BeachLinda Mar
![Page 39: Building your first Java Application with MongoDB](https://reader036.vdocuments.site/reader036/viewer/2022081413/547afe2bb4af9f4c738b462c/html5/thumbnails/39.jpg)
The Mechanism of Sharding
Chunk
Maverick’s RockawayFort Point Ocean BeachLinda Mar
Chunk ChunkChunk
Shard 1 Shard 2 Shard 3 Shard 4
![Page 40: Building your first Java Application with MongoDB](https://reader036.vdocuments.site/reader036/viewer/2022081413/547afe2bb4af9f4c738b462c/html5/thumbnails/40.jpg)
The Mechanism of Sharding
40
Shard 1 Shard 2 Shard 3 Shard 4
Chunkc Chunkc Chunkc ChunkcChunkc Chunkc
Chunkc
Chunkc Chunkc
Chunkc
![Page 41: Building your first Java Application with MongoDB](https://reader036.vdocuments.site/reader036/viewer/2022081413/547afe2bb4af9f4c738b462c/html5/thumbnails/41.jpg)
The Mechanism of Sharding
41
Shard 1 Shard 2 Shard 3 Shard 4
Chunkc Chunkc Chunkc ChunkcChunkc Chunkc
Chunkc
Chunkc Chunkc
Chunkc
Client Applicatio
nQuery: Linda Mar
![Page 42: Building your first Java Application with MongoDB](https://reader036.vdocuments.site/reader036/viewer/2022081413/547afe2bb4af9f4c738b462c/html5/thumbnails/42.jpg)
The Mechanism of Sharding
42
Shard 1 Shard 2 Shard 3 Shard 4
Chunkc Chunkc Chunkc ChunkcChunkc Chunkc
Chunkc
Chunkc Chunkc
Client Applicatio
nQuery: Maverick’s
Chunkc