Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.
Building Your First Application in Java                               1
is a…• High performance• Highly available• Easily scalable• Easy to use• Feature rich             Document store
Data Model• A Mongo system holds a set of databases• A database holds a set of collections• A collection holds a set of do...
High Availability: Replica Sets•   Initialize -> Election•   Primary + data replication from primary to secondary       No...
Replica Set - Failure•   Primary down/network failure•   Automatic election of new primary if majority exists             ...
Replica Set - Failover•   New primary elected•   Replication established from new primary       Node 1                    ...
Durability• Fire and forget• Wait for error• Wait for journal sync• Wait for flush to disk• Wait for replication
Read Preferences  •   PRIMARY  •   PRIMARY PREFERRED  •   SECONDARY  •   SECONDARY PREFERRED  •   NEAREST
Let’s build a location based surf reporting app!
Let’s build a location based surf reporting app!• Report current conditions
Let’s build a location based surf reporting app!• Report current conditions• Get current local conditions
Let’s build a location based surf reporting app!• Report current conditions• Get current local conditions• Determine best ...
Document Structure{    "_id" : ObjectId("504ceb3d30042d707af96fef"),    "reporter" : "test",    "location" : {            ...
Document Structure{    "_id" : ObjectId("504ceb3d30042d707af96fef"),   Primary Key,    "reporter" : "test",               ...
Document Structure{    "_id" : ObjectId("504ceb3d30042d707af96fef"),   Primary Key,    "reporter" : "test",               ...
Document Structure{    "_id" : ObjectId("504ceb3d30042d707af96fef"),   Primary Key,    "reporter" : "test",               ...
Get local surf conditions db.reports.find(           {           "location.coordinates" : { $near : [-122, 37] ,          ...
Get local surf conditions db.reports.find(           {           "location.coordinates" : { $near : [-122, 37] ,          ...
Get local surf conditions db.reports.find(           {           "location.coordinates" : { $near : [-122, 37] ,          ...
Get local surf conditions db.reports.find(           {           "location.coordinates" : { $near : [-122, 37] ,          ...
Get local surf conditions db.reports.find(           {           "location.coordinates" : { $near : [-122, 37] ,          ...
Get local surf conditions: Connecting
DBObjects Output:            { "name" : "test"}            parsed
Building the query
Results{ "location" : { "name" : "Montara" }, "conditions" : { "height" : 6, "period" : 20, "rating" : 5 } }{ "location" :...
Analysis Features:  Aggregation Framework  What are the best conditions for my local beach?
Pipelining Operations  $match    Match “Linda Mar” $project   Only interested in conditions            Group by rating, av...
Aggregation Framework{ "aggregate" : "reports" ,   "pipeline" : [     { "$match" : { "location.name" : "Linda Mar"}} ,    ...
Aggregation Framework{ "aggregate" : "reports" ,   "pipeline" : [     { "$match" : { "location.name" : "Linda Mar"}} ,    ...
Aggregation Framework{ "aggregate" : "reports" ,   "pipeline" : [     { "$match" : { "location.name" : "Linda Mar"}} ,    ...
Aggregation Framework{ "aggregate" : "reports" ,   "pipeline" : [     { "$match" : { "location.name" : "Linda Mar"}} ,    ...
Aggregation Framework{ "aggregate" : "reports" ,   "pipeline" : [     { "$match" : { "location.name" : "Linda Mar"}} ,    ...
The Aggregation Helper
Scaling• Sharding is the partitioning of data among  multiple machines• Balancing occurs when the load on any one  node gr...
Scaling MongoDB  Sharded cluster                MongoDB              Single Instance                    Or                ...
The Mechanism of Sharding                         Complete Data SetDefine Shard Key on Location Name    Fort Point        ...
The Mechanism of Sharding                 Chunk                                  ChunkDefine Shard Key on Location Name   ...
The Mechanism of Sharding Chunk        Chunk               Chunk               Chunk Fort Point   Linda Mar   Maverick’s  ...
The Mechanism of Sharding Chunk        Chunk             Chunk               Chunk Fort Point   Linda Mar   Maverick’s   O...
The Mechanism of Sharding Chu      Chu nkc      nkc Chu      Chu    Chu   Chu        Chu    Chu   Chu   Chu nkc      nkc  ...
The Mechanism of Sharding                                    Client       Query: Linda Mar           Application Chu      ...
The Mechanism of Sharding                                     Client       Query: Maverick’s           Application Chu    ...
Thanks! Office Hours Thursdays 4-6 pm 555 University Ave. Palo Alto     We’re Hiring !Bryan.reinero@10gen.com
Upcoming SlideShare
Loading in …5
×

Building your first Java Application with MongoDB

2,867 views

Published on

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.

Published in: Technology
  • Be the first to comment

Building your first Java Application with MongoDB

  1. 1. Building Your First Application in Java 1
  2. 2. is a…• High performance• Highly available• Easily scalable• Easy to use• Feature rich Document store
  3. 3. 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 likestring, integer, float, timestamp, binary, etc., a document, or an array of values
  4. 4. High Availability: Replica Sets• Initialize -> Election• Primary + data replication from primary to secondary Node 1 Node 2 Secondary Heartbeat Secondary Node 3 Primary Replication Replication
  5. 5. Replica Set - Failure• Primary down/network failure• Automatic election of new primary if majority exists Primary Election Node 1 Node 2 Secondary Heartbeat Secondary Node 3 Primary
  6. 6. Replica Set - Failover• New primary elected• Replication established from new primary Node 1 Node 2 Secondary Heartbeat Primary Node 3 Primary
  7. 7. Durability• Fire and forget• Wait for error• Wait for journal sync• Wait for flush to disk• Wait for replication
  8. 8. Read Preferences • PRIMARY • PRIMARY PREFERRED • SECONDARY • SECONDARY PREFERRED • NEAREST
  9. 9. Let’s build a location based surf reporting app!
  10. 10. Let’s build a location based surf reporting app!• Report current conditions
  11. 11. Let’s build a location based surf reporting app!• Report current conditions• Get current local conditions
  12. 12. Let’s build a location based surf reporting app!• Report current conditions• Get current local conditions• Determine best conditions per beach
  13. 13. 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")}
  14. 14. Document Structure{ "_id" : ObjectId("504ceb3d30042d707af96fef"), Primary Key, "reporter" : "test", Unique, "location" : { "coordinates" : [ Auto-indexed -122.477222, 37.810556 ], "name" : "Fort Point" }, "conditions" : { "height" : 0, "period" : 9, "rating" : 1 }, "date" : ISODate("2011-11-16T20:17:17.277Z")}
  15. 15. Document Structure{ "_id" : ObjectId("504ceb3d30042d707af96fef"), Primary Key, "reporter" : "test", Unique, "location" : { "coordinates" : [ Autoindexed -122.477222, 37.810556 ], Compound Index, "name" : "Fort Point" Geospacial }, "conditions" : { "height" : 0, "period" : 9, "rating" : 1 }, "date" : ISODate("2011-11-16T20:17:17.277Z")}
  16. 16. Document Structure{ "_id" : ObjectId("504ceb3d30042d707af96fef"), Primary Key, "reporter" : "test", Unique, "location" : { "coordinates" : [ Autoindexed -122.477222, 37.810556 ], Compound Index, "name" : "Fort Point" Geospacial }, "conditions" : { "height" : 0, "period" : 9, "rating" : 1 }, Indexed for "date" : ISODate("2011-11-16T20:17:17.277Z") Time-To-Live}
  17. 17. Get local surf conditions db.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})
  18. 18. Get local surf conditions db.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
  19. 19. Get local surf conditions db.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
  20. 20. Get local surf conditions db.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
  21. 21. Get local surf conditions db.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
  22. 22. Get local surf conditions: Connecting
  23. 23. DBObjects Output: { "name" : "test"} parsed
  24. 24. Building the query
  25. 25. Results{ "location" : { "name" : "Montara" }, "conditions" : { "height" : 6, "period" : 20, "rating" : 5 } }{ "location" : { "name" : "Mavericks" }, "conditions" : { "height" : 5, "period" : 13, "rating" : 3 } }{ "location" : { "name" : "Mavericks" }, "conditions" : { "height" : 3, "period" : 15, "rating" : 3 } }{ "location" : { "name" : "Mavericks" }, "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 } }
  26. 26. Analysis Features: Aggregation Framework What are the best conditions for my local beach?
  27. 27. Pipelining Operations $match Match “Linda Mar” $project Only interested in conditions Group by rating, averaging $group wave height and wave period $sort Order by best conditions
  28. 28. 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}} ]}
  29. 29. 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”
  30. 30. 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
  31. 31. 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
  32. 32. 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
  33. 33. The Aggregation Helper
  34. 34. Scaling• Sharding is the partitioning of data among multiple machines• Balancing occurs when the load on any one node grows out of proportion
  35. 35. Scaling MongoDB Sharded cluster MongoDB Single Instance Or Replica Set Client Application
  36. 36. The Mechanism of Sharding Complete Data SetDefine Shard Key on Location Name Fort Point Linda Mar Maverick’s Ocean Beach Rockaway
  37. 37. The Mechanism of Sharding Chunk ChunkDefine Shard Key on Location Name Fort Point Linda Mar Maverick’s Ocean Beach Rockaway
  38. 38. The Mechanism of Sharding Chunk Chunk Chunk Chunk Fort Point Linda Mar Maverick’s Ocean Beach Rockaway
  39. 39. The Mechanism of Sharding Chunk Chunk Chunk Chunk Fort Point Linda Mar Maverick’s Ocean Beach Rockaway Shard 1 Shard 2 Shard 3 Shard 4
  40. 40. The Mechanism of Sharding Chu Chu nkc nkc Chu Chu Chu Chu Chu Chu Chu Chu nkc nkc nkc nkc nkc nkc nkc nkc Shard 1 Shard 2 Shard 3 Shard 4 40
  41. 41. The Mechanism of Sharding Client Query: Linda Mar Application Chu Chu nkc nkc Chu Chu Chu Chu Chu Chu Chu Chu nkc nkc nkc nkc nkc nkc nkc nkc Shard 1 Shard 2 Shard 3 Shard 4 41
  42. 42. The Mechanism of Sharding Client Query: Maverick’s Application Chu Chu nkc nkc Chu Chu Chu Chu Chu Chu Chu Chu nkc nkc nkc nkc nkc nkc nkc nkc Shard 1 Shard 2 Shard 3 Shard 4 42
  43. 43. Thanks! Office Hours Thursdays 4-6 pm 555 University Ave. Palo Alto We’re Hiring !Bryan.reinero@10gen.com

×