Java and Mongo

2,588 views

Published on

Internal Presentation about how to use Java and MongoDB

Published in: Technology
  • Be the first to comment

Java and Mongo

  1. Java and MongoDB/** * @author: marcio garcia * @contact: marcio.garcia@rga.com **/ Proprietary & Confidential. © 2012 R/GA All rights reserved.
  2. Agenda• Introduction - 6 min. • WHAT?• Motivations - 6 min. • WHY?• Coding - 12 min. • HOW? Proprietary & Confidential. © 2012 R/GA All rights reserved. /3
  3. /01Introduction Proprietary & Confidential. © 2012 R/GA All rights reserved. /4
  4. What is?and what is not Proprietary & Confidential. © 2012 R/GA All rights reserved. /5
  5. it is….• NoSQL Database• Document based• Cross-platform• Written in C++• BSON (JSON like) - Structure• License: GNU• “Join-less” DB – Performance• Master slave failover – Availability Sharding – Scalability Proprietary & Confidential. © 2012 R/GA All rights reserved. /6
  6. it is….• NoSQL Database• Document based• Cross-platform• Written in C++• BSON (JSON like) - Structure• License: GNU• “Join-less” DB – Improve High Performance• Master slave failover – Improve availability Sharding – Improve scalability Proprietary & Confidential. © 2012 R/GA All rights reserved. /7
  7. it is…. NoSQL DB Document based Database Database A file in your disk Tablespace Collection Bunch of Documents Tables Documents Group of fields Fields Fields String, Integer, Float, name key Timestamp, Binary, Array value value Document (do you remember: joinless?) Proprietary & Confidential. © 2012 R/GA All rights reserved. /8
  8. it is…. BSON (JSON like) - Structure Proprietary & Confidential. © 2012 R/GA All rights reserved. /9
  9. it is…. Sharding – Improving scalabilitySample Document with 2 fields: first and last Sharding function: Shard1: first=“A* to G* Shard2: first=“H* to M*” Shard3: first=“N* to S*” Shard4: first=“T* to Z*” Proprietary & Confidential. © 2012 R/GA All rights reserved. / 10
  10. it is not….• Query based database• All purpose database• ACID (just between documents at the same hierarchy)• Fixed schema• Unlimited storage database• OLAP – it’s not a DW DB! Proprietary & Confidential. © 2012 R/GA All rights reserved. / 11
  11. BONUS!• 60mi of records• JSON format• Circa 2 hours• 15GB database• Circa 40 min to create an index• Finding • find().count() < 18 milliseconds – first time • find({“area”:”11”, “phone_number”:”88888881”}) Proprietary & Confidential. © 2012 R/GA All rights reserved. / 12
  12. /02Motivation Proprietary & Confidential. © 2012 R/GA All rights reserved. / 13
  13. Why? Proprietary & Confidential. © 2012 R/GA All rights reserved. / 14
  14. YES you should use it if…..• Fast response for queries (SELECT)• First database• Store Temporary Data• Share data between apps with different flavors (Java, Shell, Javascript - Node.js)• Data Warehouse Cube• File storage (GridFS)• Horizontally scaling – sharding• Web application Proprietary & Confidential. © 2012 R/GA All rights reserved. / 15
  15. YES … examples• Portal Home page• App on Facebook, share data• Delivering content to different clients. • Web browser, iTunes, Mobile, DTV• Delivering content through a Web Server • Storage device, balancing Proprietary & Confidential. © 2012 R/GA All rights reserved. / 16
  16. Not convinced yet? Doubts?• Drivers (Python, Django, Java, Spring, .Net, PHP, Ruby, Rails, Node.js)• Tools •MongoHub - MacOS •Meclipse – Eclipse Plugin •JMongoBrowser – no restrictions• Monitoring •Munin •Cacti •Ganglia• Serving content from Mongo •NGINX and Lighttp Proprietary & Confidential. © 2012 R/GA All rights reserved. / 17
  17. Not convinced yet? Proprietary & Confidential. © 2012 R/GA All rights reserved. / 18
  18. /03Tech Stuff Proprietary & Confidential. © 2012 R/GA All rights reserved. / 19
  19. How? Proprietary & Confidential. © 2012 R/GA All rights reserved. / 20
  20. Connecting…. Drivers Spring Data for MongoDB DataNucleos Complete list: http://www.mongodb.org/display/DOCS/Java+Language+Center Proprietary & Confidential. © 2012 R/GA All rights reserved. / 21
  21. Connecting…. Drivers • Annotation based • Validation JSR303 • Type-safe • DAO<T,V> access abstraction • Easy to implement • Fast • Lightweight • Source code easy to understand Proprietary & Confidential. © 2012 R/GA All rights reserved. / 22
  22. Installing…. Proprietary & Confidential. © 2012 R/GA All rights reserved. / 23
  23. Connecting…. Source: BaseApp.java Proprietary & Confidential. © 2012 R/GA All rights reserved. / 24
  24. Annotations Class Level• Entity Maps Level Field the class to the collection• Id Can define the namecollection Defines the PK of a of Field Level• Property It’s n ObjectId class collection a field. Defines as param Field level annotation • Serialized Stored in a binary field • Transient Loaded but not stored • NotSaved Not Loaded and not saved Field level annotation • Indexed Create an index with the field Proprietary & Confidential. © 2012 R/GA All rights reserved. / 25
  25. Annotations FK, stores the ObjectID• Entity • Reference• Id Store the Object • Embedded• Property Field level annotation • Serialized Before and After actions • Transient Persisting and Loading • NotSaved • PrePersist • PostPersist Field level annotation • PreLoad • Indexed • PostLoad Proprietary & Confidential. © 2012 R/GA All rights reserved. / 26
  26. Mapping Define a collection: cities Create the PK (ObjectId) Could use @Property to redefine the column name Store the ObjectID from State document outside this Store a list of Neighborhood objects inside the City document Proprietary & Confidential. © 2012 R/GA All rights reserved. / 27
  27. Collectiondb.states.find() db.cities.find() @Id @Reference @Embedded Proprietary & Confidential. © 2012 R/GA All rights reserved. / 28
  28. Creating Create the object Datastore.save(object) Database Proprietary & Confidential. © 2012 R/GA All rights reserved. / 29
  29. Deleting Find the Record Datastore.delete(object | Query) Proprietary & Confidential. © 2012 R/GA All rights reserved. / 30
  30. Querying / Updating Find the objects Apply the update rule Execute Proprietary & Confidential. © 2012 R/GA All rights reserved. / 31
  31. Pre/Post … Persist/Load Pre/Post Persist Pre/Post Load Proprietary & Confidential. © 2012 R/GA All rights reserved. / 32
  32. Concluding RemarksJava• Powerful language, powerful VM• Available on Heroku, GAE• JVM BONUS: JRuby, Groovy, ScalaMongoDB• NoSQL – Document Based• Schema-less• JSON like “multi language”• Horizontally scale - Sharding Proprietary & Confidential. © 2012 R/GA All rights reserved. / 33
  33. Links Delicious Stack: http://www.delicious.com/stacks/view/GmHJ5R Proprietary & Confidential. © 2012 R/GA All rights reserved. / 34
  34. Thanks! Proprietary & Confidential. © 2012 R/GA All rights reserved. / 35

×