SlideShare a Scribd company logo
1 of 36
Java and MongoDB


/**
 * @author: marcio garcia
 * @contact: marcio.garcia@rga.com
 **/


                                     Proprietary & Confidential. © 2012 R/GA All rights reserved.
Agenda

• Introduction - 6 min.
   •   WHAT?


• Motivations - 6 min.
   •   WHY?


• Coding       - 12 min.
   •   HOW?


                           Proprietary & Confidential. © 2012 R/GA All rights reserved.   /3
/01
Introduction



               Proprietary & Confidential. © 2012 R/GA All rights reserved.   /4
What is?
and what is not




           Proprietary & Confidential. © 2012 R/GA All rights reserved.   /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
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
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
it is…. BSON   (JSON like) - Structure




                  Proprietary & Confidential. © 2012 R/GA All rights reserved.   /9
it is…. Sharding   – Improving scalability
Sample 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
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
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
/02
Motivation



             Proprietary & Confidential. © 2012 R/GA All rights reserved.   / 13
Why?

   Proprietary & Confidential. © 2012 R/GA All rights reserved.   / 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
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
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
Not convinced yet?




            Proprietary & Confidential. © 2012 R/GA All rights reserved.   / 18
/03
Tech Stuff



             Proprietary & Confidential. © 2012 R/GA All rights reserved.   / 19
How?

   Proprietary & Confidential. © 2012 R/GA All rights reserved.   / 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
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
Installing….




               Proprietary & Confidential. © 2012 R/GA All rights reserved.   / 23
Connecting….




                     Source: BaseApp.java

           Proprietary & Confidential. © 2012 R/GA All rights reserved.   / 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
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
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
Collection
db.states.find()                            db.cities.find()                          @Id




                   @Reference




                   @Embedded




                       Proprietary & Confidential. © 2012 R/GA All rights reserved.         / 28
Creating
                   Create the object



                      Datastore.save(object)




                              Database




           Proprietary & Confidential. © 2012 R/GA All rights reserved.   / 29
Deleting
                                                       Find the Record




                                 Datastore.delete(object | Query)




           Proprietary & Confidential. © 2012 R/GA All rights reserved.   / 30
Querying / Updating
                                                 Find the objects




                                                                    Apply the update rule

           Execute




             Proprietary & Confidential. © 2012 R/GA All rights reserved.                   / 31
Pre/Post … Persist/Load


                                                                           Pre/Post Persist




                                                                            Pre/Post Load




            Proprietary & Confidential. © 2012 R/GA All rights reserved.                      / 32
Concluding Remarks
Java
• Powerful language, powerful VM
• Available on Heroku, GAE
• JVM BONUS: JRuby, Groovy, Scala

MongoDB
• NoSQL – Document Based
• Schema-less
• JSON like “multi language”
• Horizontally scale - Sharding



                        Proprietary & Confidential. © 2012 R/GA All rights reserved.   / 33
Links


                         Delicious Stack:
        http://www.delicious.com/stacks/view/GmHJ5R




              Proprietary & Confidential. © 2012 R/GA All rights reserved.   / 34
Thanks!

    Proprietary & Confidential. © 2012 R/GA All rights reserved.   / 35
Java and Mongo

More Related Content

Viewers also liked

Viewers also liked (7)

Emerging markets eu
Emerging markets euEmerging markets eu
Emerging markets eu
 
Apresentacao Interna GAE
Apresentacao Interna GAEApresentacao Interna GAE
Apresentacao Interna GAE
 
AWS_Deploy - GURU-SP - 33o. encontro
AWS_Deploy - GURU-SP - 33o. encontroAWS_Deploy - GURU-SP - 33o. encontro
AWS_Deploy - GURU-SP - 33o. encontro
 
Budidaya lele sangkuriang
Budidaya lele sangkuriangBudidaya lele sangkuriang
Budidaya lele sangkuriang
 
Matematika xii
Matematika xiiMatematika xii
Matematika xii
 
Summer training sales & logistics (1)
Summer training   sales & logistics (1)Summer training   sales & logistics (1)
Summer training sales & logistics (1)
 
Homework
HomeworkHomework
Homework
 

Similar to Java and Mongo

Mobile Development Meets Semantic Technology
Mobile Development Meets Semantic TechnologyMobile Development Meets Semantic Technology
Mobile Development Meets Semantic TechnologyBlue Slate Solutions
 
Neo4 + Grails
Neo4 + GrailsNeo4 + Grails
Neo4 + Grailsstasimus
 
MongoDB in FS
MongoDB in FSMongoDB in FS
MongoDB in FSMongoDB
 
Webinar: Applying REST to Network Management – An Implementor’s View
Webinar: Applying REST to Network Management – An Implementor’s View Webinar: Applying REST to Network Management – An Implementor’s View
Webinar: Applying REST to Network Management – An Implementor’s View Tail-f Systems
 
Development without Constraint
Development without ConstraintDevelopment without Constraint
Development without ConstraintChad Davis
 
Linked in nosql_atnetflix_2012_v1
Linked in nosql_atnetflix_2012_v1Linked in nosql_atnetflix_2012_v1
Linked in nosql_atnetflix_2012_v1Sid Anand
 
Using Spring with NoSQL databases (SpringOne China 2012)
Using Spring with NoSQL databases (SpringOne China 2012)Using Spring with NoSQL databases (SpringOne China 2012)
Using Spring with NoSQL databases (SpringOne China 2012)Chris Richardson
 
Standardizing the Data Distribution Service (DDS) API for Modern C++
Standardizing the Data Distribution Service (DDS) API for Modern C++Standardizing the Data Distribution Service (DDS) API for Modern C++
Standardizing the Data Distribution Service (DDS) API for Modern C++Sumant Tambe
 
Spring one2gx2010 spring-nonrelational_data
Spring one2gx2010 spring-nonrelational_dataSpring one2gx2010 spring-nonrelational_data
Spring one2gx2010 spring-nonrelational_dataRoger Xia
 
IBM Java PackedObjects
IBM Java PackedObjectsIBM Java PackedObjects
IBM Java PackedObjectsMarcel Mitran
 
EDB's Migration Portal - Migrate from Oracle to Postgres
EDB's Migration Portal - Migrate from Oracle to PostgresEDB's Migration Portal - Migrate from Oracle to Postgres
EDB's Migration Portal - Migrate from Oracle to PostgresEDB
 
Non Relational Databases
Non Relational DatabasesNon Relational Databases
Non Relational DatabasesChris Baglieri
 
Understanding the Value and Architecture of Apache Drill
Understanding the Value and Architecture of Apache DrillUnderstanding the Value and Architecture of Apache Drill
Understanding the Value and Architecture of Apache DrillDataWorks Summit
 
Hadoop Summit - Hausenblas 20 March
Hadoop Summit - Hausenblas 20 MarchHadoop Summit - Hausenblas 20 March
Hadoop Summit - Hausenblas 20 MarchMapR Technologies
 

Similar to Java and Mongo (20)

Mobile Development Meets Semantic Technology
Mobile Development Meets Semantic TechnologyMobile Development Meets Semantic Technology
Mobile Development Meets Semantic Technology
 
Neo4J and Grails
Neo4J and GrailsNeo4J and Grails
Neo4J and Grails
 
Neo4 + Grails
Neo4 + GrailsNeo4 + Grails
Neo4 + Grails
 
MongoDB in FS
MongoDB in FSMongoDB in FS
MongoDB in FS
 
Webinar: Applying REST to Network Management – An Implementor’s View
Webinar: Applying REST to Network Management – An Implementor’s View Webinar: Applying REST to Network Management – An Implementor’s View
Webinar: Applying REST to Network Management – An Implementor’s View
 
Development without Constraint
Development without ConstraintDevelopment without Constraint
Development without Constraint
 
Introduction to Apache Drill
Introduction to Apache DrillIntroduction to Apache Drill
Introduction to Apache Drill
 
Linked in nosql_atnetflix_2012_v1
Linked in nosql_atnetflix_2012_v1Linked in nosql_atnetflix_2012_v1
Linked in nosql_atnetflix_2012_v1
 
Using Spring with NoSQL databases (SpringOne China 2012)
Using Spring with NoSQL databases (SpringOne China 2012)Using Spring with NoSQL databases (SpringOne China 2012)
Using Spring with NoSQL databases (SpringOne China 2012)
 
Mongodb my
Mongodb myMongodb my
Mongodb my
 
MongoDB
MongoDBMongoDB
MongoDB
 
MongoDB
MongoDBMongoDB
MongoDB
 
Standardizing the Data Distribution Service (DDS) API for Modern C++
Standardizing the Data Distribution Service (DDS) API for Modern C++Standardizing the Data Distribution Service (DDS) API for Modern C++
Standardizing the Data Distribution Service (DDS) API for Modern C++
 
Spring one2gx2010 spring-nonrelational_data
Spring one2gx2010 spring-nonrelational_dataSpring one2gx2010 spring-nonrelational_data
Spring one2gx2010 spring-nonrelational_data
 
Hadoop, Taming Elephants
Hadoop, Taming ElephantsHadoop, Taming Elephants
Hadoop, Taming Elephants
 
IBM Java PackedObjects
IBM Java PackedObjectsIBM Java PackedObjects
IBM Java PackedObjects
 
EDB's Migration Portal - Migrate from Oracle to Postgres
EDB's Migration Portal - Migrate from Oracle to PostgresEDB's Migration Portal - Migrate from Oracle to Postgres
EDB's Migration Portal - Migrate from Oracle to Postgres
 
Non Relational Databases
Non Relational DatabasesNon Relational Databases
Non Relational Databases
 
Understanding the Value and Architecture of Apache Drill
Understanding the Value and Architecture of Apache DrillUnderstanding the Value and Architecture of Apache Drill
Understanding the Value and Architecture of Apache Drill
 
Hadoop Summit - Hausenblas 20 March
Hadoop Summit - Hausenblas 20 MarchHadoop Summit - Hausenblas 20 March
Hadoop Summit - Hausenblas 20 March
 

Recently uploaded

08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking MenDelhi Call girls
 
SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024Scott Keck-Warren
 
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...HostedbyConfluent
 
A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024Results
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationSafe Software
 
Handwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsHandwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsMaria Levchenko
 
Understanding the Laravel MVC Architecture
Understanding the Laravel MVC ArchitectureUnderstanding the Laravel MVC Architecture
Understanding the Laravel MVC ArchitecturePixlogix Infotech
 
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | DelhiFULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhisoniya singh
 
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024BookNet Canada
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Miguel Araújo
 
Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slidevu2urc
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Drew Madelung
 
Unblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen FramesUnblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen FramesSinan KOZAK
 
Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...Alan Dix
 
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 3652toLead Limited
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerThousandEyes
 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptxHampshireHUG
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationRadu Cotescu
 
The Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxThe Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxMalak Abu Hammad
 
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking MenDelhi Call girls
 

Recently uploaded (20)

08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men
 
SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024
 
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...
 
A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
 
Handwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsHandwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed texts
 
Understanding the Laravel MVC Architecture
Understanding the Laravel MVC ArchitectureUnderstanding the Laravel MVC Architecture
Understanding the Laravel MVC Architecture
 
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | DelhiFULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
 
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
 
Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slide
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
 
Unblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen FramesUnblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen Frames
 
Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...
 
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organization
 
The Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxThe Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptx
 
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
 

Java and Mongo

  • 1.
  • 2. Java and MongoDB /** * @author: marcio garcia * @contact: marcio.garcia@rga.com **/ Proprietary & Confidential. © 2012 R/GA All rights reserved.
  • 3. Agenda • Introduction - 6 min. • WHAT? • Motivations - 6 min. • WHY? • Coding - 12 min. • HOW? Proprietary & Confidential. © 2012 R/GA All rights reserved. /3
  • 4. /01 Introduction Proprietary & Confidential. © 2012 R/GA All rights reserved. /4
  • 5. What is? and what is not Proprietary & Confidential. © 2012 R/GA All rights reserved. /5
  • 6. 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
  • 7. 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
  • 8. 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
  • 9. it is…. BSON (JSON like) - Structure Proprietary & Confidential. © 2012 R/GA All rights reserved. /9
  • 10. it is…. Sharding – Improving scalability Sample 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
  • 11. 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
  • 12. 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
  • 13. /02 Motivation Proprietary & Confidential. © 2012 R/GA All rights reserved. / 13
  • 14. Why? Proprietary & Confidential. © 2012 R/GA All rights reserved. / 14
  • 15. 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
  • 16. 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
  • 17. 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
  • 18. Not convinced yet? Proprietary & Confidential. © 2012 R/GA All rights reserved. / 18
  • 19. /03 Tech Stuff Proprietary & Confidential. © 2012 R/GA All rights reserved. / 19
  • 20. How? Proprietary & Confidential. © 2012 R/GA All rights reserved. / 20
  • 21. 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
  • 22. 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
  • 23. Installing…. Proprietary & Confidential. © 2012 R/GA All rights reserved. / 23
  • 24. Connecting…. Source: BaseApp.java Proprietary & Confidential. © 2012 R/GA All rights reserved. / 24
  • 25. 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
  • 26. 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
  • 27. 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
  • 28. Collection db.states.find() db.cities.find() @Id @Reference @Embedded Proprietary & Confidential. © 2012 R/GA All rights reserved. / 28
  • 29. Creating Create the object Datastore.save(object) Database Proprietary & Confidential. © 2012 R/GA All rights reserved. / 29
  • 30. Deleting Find the Record Datastore.delete(object | Query) Proprietary & Confidential. © 2012 R/GA All rights reserved. / 30
  • 31. Querying / Updating Find the objects Apply the update rule Execute Proprietary & Confidential. © 2012 R/GA All rights reserved. / 31
  • 32. Pre/Post … Persist/Load Pre/Post Persist Pre/Post Load Proprietary & Confidential. © 2012 R/GA All rights reserved. / 32
  • 33. Concluding Remarks Java • Powerful language, powerful VM • Available on Heroku, GAE • JVM BONUS: JRuby, Groovy, Scala MongoDB • NoSQL – Document Based • Schema-less • JSON like “multi language” • Horizontally scale - Sharding Proprietary & Confidential. © 2012 R/GA All rights reserved. / 33
  • 34. Links Delicious Stack: http://www.delicious.com/stacks/view/GmHJ5R Proprietary & Confidential. © 2012 R/GA All rights reserved. / 34
  • 35. Thanks! Proprietary & Confidential. © 2012 R/GA All rights reserved. / 35

Editor's Notes

  1. Thank you all for coming.My name isMarcio GarciaI’m Sw Engineer my core language is Java, but I’m also developing in +Python, +Ruby, +Groove, +Shell scriptInterested in programming languages, static and dynamics and infrastructure and automation I’ve started at R/GA Last january in SP office. Btw, I’d like to say thanks to Edson, Will Turnage and CristhianRauh for the opportunity.
  2. This is the agenda for todayI’ll Attempt to answer 3 basic questions: WHAT an introduction about MongoDB, I’m expecting to spend 6 minutesWHY, some motivations why you should pick Mongo to a web project, also 6 minutesand HOW, how to integrate and some nice stuff about Java and Mongo, will take approximately 12 minutes.
  3. NoSQL DB / Document based: Different (not better nor worse) just different, for different tasks for different approachesCross platform – linux, windows and macos are welcome, 32 or 64 bits as wellWritten in C++Structured in BSON, it’s a JSON Like protocol and structureUnder GNU licenseJoinless higher performance – it’s a problem on regular SQL db, most of the times, working with Oracle, we should use ‘hints’, reindex indexes to get a satisfied time to execute the query. Actually this is one of the characteristics to a DB be considered a NoSQL.You can use join, but it is highly discouraged. If you’d like to keep thinks like you’re used to you shouldn’t move to a NoSQL DB, quite obvious doesn’t it?Master slave failoverSharding. I’ll talk a little bit more about this later
  4. I’d like to talk more deeply about 3 aspects…..NoSQL Database document basedStructureAnd Sharding
  5. This is a white belt skills needed if you like to start developing with mongo, the differences btween a regular SQL DB and MongoDBThis is a comparison from what we’re used to understand about a regular RDBMS like Oracle, Postgre, MySQL and Mongo.First Database, a database in a RDMBS is quite the same in Mongo, that is represented as a file (or bunch of them) in your Hard Drive.Second, Tablespace on a RDBMS is a Collection in the Mondo side, that is a bunch of documentsThird, Tables on RDBMS is mapped as Documents on Mongo, that have the same conclusion, a bunch of fields togetherFields, is almost the same in both worlds, they can have types like: String, Integer, Float, Timestamp, Binary, Array and including a special one: Document.
  6. This is a Blue belt skills………….You can manipulate a regular RDBMS using SQL statements. Create records, update, remove, retrieve records and all DML and DDLs are SQL basedOn the other hand, on a mongodb world you don’t have sql statements, you should use BSON/JSON to retrive, update and create documents. You don’t have DDLs, the definitions commands in mongo, at not for creating collections and documents, mongo is schemaless so, if you’d like to create a document simple create that json, and fire the save command. If a document with that specific fields already exists fine, if not, it will be created.
  7. Bleck belt skill – Sharding…From MongoDB page: Sharding is the ability to distribute a peace of data through many MongoDB instances.So, to understand how it works in a simple example….Giving this document (click) with two fields, first and last name.Supposing that I’ve a such high volume of queries been fired against the db asking, updating and creating documents, I could choose to distribute the load on different mongo instances based on a function. (click).My function here is: the first letter of the first name will be distributed to different mongo db instances. (click). This is how my infrastructure should looks like.** Present the sharding (blue box :: mongodb instances , yellow box :: mongo instances acting as configuration servers , green box :: mongo router and in gray I have a client firing the request do retrieve, create, remove or delete documents.The workload starting from the client requests, are distributed to a different mongo shard, based on the sharding function.This is just a example of a sharding approach.
  8. Query based dbAll purpose dbACID, this is a important point. Mongo is not ACID through multiple documents, but it is ACID on the same document, if you have a document inside other, this is ACID.Fixed schema, it doesn’t mean that you can do whatever you want with this without a punishment. Punishment in disk space, memory usage.Unlimited storage database: limited by your OS and disk space. On a 32 bits Linux the file size is 16GB. On a 64bits the disk is your limit. I got a database with 130GB with a miliseconds to store or retrive data from the collection. Without any join, of course.
  9. Last week we’ve made a test. And these are the numbers that we got from this.It was around 60mi of records (3 fields)Loaded using JSON formatTooks around 2 hours to load it on my machineIt generated 15GB of data locallyIt took around 40 minutes to create an index with two fieldsFinding records, after the index, It’took less then 18 miliseconds, before the index it took around 1’16’’The performance here is nothing extraordinary, any MySQL well configured and tuned could do the same.
  10. You want a fast response for your queries without care about extra commands and tunning queries…It’s your first db (direct contact with the client)Store temp data, it’s not cache, you have better options for that, like redis and memcacheShare data between apps with diff flavors (java, shell, js)DW cubesFile storage (for instance using Nginx plugin)Horizontally scaling making usage of shardingIf we are talking about an app with web app characteristics like: high volume of data (retrieving, storing and update data), temporary data, asyncrhonous calls, this should fit you.
  11. Portal Home Page, storing data from the backend database to be displayed to a high volume access page. You could use Redis for that, the problem is the queries criteria. (Nike+ and NikeFuel)App on Facebook, for instance sharing score, number of victories, on a social game. Volatille data in the app, like last comments, ranking page, recipes pageDelivering content do different clients – Delivering content to a backend app its more a database job, but putting the power of JSON structure and easy way to connect, makes Mongo share data not only to a backend app, but also to a front end one, using Node.js or just a single jQueryplugin.Delivering content to WebServer – NGINX and lighttp have plugins to instead delivery content from file system, delivery it from a Mongo. You can take advantages of the sharding acting as a load balancer for the content. Advantages of this approach? You could use this instead of a expensive EMC storage devices.
  12. OK guys, not yet convinced after the tech approach…… these are some companies that are using mongo on at least one project.
  13. Falar sobre...
  14. To connect a java app to the mongodb you can use the prosaic way of opening a socket directly to the mongodatabase, create your own json, send it to the db, read the return, parse it, provide a good message to the client…….. After 5 days using this approach write on your blog how bad was your experience using Java and MongoDB. Or….. you can use a driver ……There are some players that have created some….. Spring created the Spring data for MongoDB, DataNucleos also has one….. Both of them looks to be nice, robust, but also full of features that you probably will not use…… I pick a very simple one… named: Morphia…..
  15. Annotationbased driverU can use the JSR-303 for ValidationIt’s type safeDAO access abstraction using the genericsEasy to include in your project, it’s not needed a lot of dependences (just one jar file)It’s fast…. It uses reflection at first time and then cache all the objects structureLightweightThe source is easy to understand
  16. Two basic ways…. Download the jar file and put it in your classpath… orUsing maven… and downloading the internet to your machine…… both ways are pretty straightforward …..
  17. This is the code to get connected to a Mongo db.First you have to have an instance of a Mongo object (doing this on line 17) with that you can create a datasource connected to the your database (line 18).In this case, my database name is “TEST”
  18. For me it looks like a workaround when your want to put some business rules on a Pre or Post methods.It looks like some technical stuff that you have to act after of before a business rule, like for instance setting basic content for fields, like updated_date or created_date..But of course we should take a look at the specific case.
  19. Warning! By using the @Reference you are creating a FK. And a NoSQL DB doesn’t care if your record will gonna be orphans.This FK is not controlled by the DB. This is controlled by the Driver, so it’s not a good approach.A solution to avoid creating this sort of thing should be create temporary collections, loaded from others collections, joining the fields.
  20. 1 – Create the Java object2 – fide the method save from the DatastoreThis is how this record will looks like in mongo
  21. Removing a record, is quite simple as well, You have to have the reference from the DB loaded into the object and fire the delete method from the datastore.The point here is that, the method delete receives two types of parameters, an objects and a Query
  22. Follow the same approach of the delete, you have to have a reference from that record loaded into an object.The second way is to use the Query and UpdateOperations that I’ll show here….. You can use the same approach to delete records, just using the Query.1 – Find the objects to be updated, using the Query2 – Apply the update rule, in this case I’m changing the name of the city to “São Paulo”3 – Then execute the update by firing Datastore.update, including as parameter the query and the update operations)
  23. Mostrar crono