View
2.263
Download
1
Tags:
Embed Size (px)
DESCRIPTION
Internal Presentation about how to use Java and MongoDB
Citation preview
Proprietary & Confidential. © 2012 R/GA All rights reserved.
Java and MongoDB
/** * @author: marcio garcia * @contact: [email protected] **/
3Proprietary & 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. 4/
01Introduction
Proprietary & Confidential. © 2012 R/GA All rights reserved. 5/
What is? and what is not
6Proprietary & Confidential. © 2012 R/GA All rights reserved. /
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
7Proprietary & Confidential. © 2012 R/GA All rights reserved. /
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
8Proprietary & Confidential. © 2012 R/GA All rights reserved. /
it is…. NoSQL DB Document based
Fields name value
Fields key value
String, Integer, Float, Timestamp, Binary, ArrayDocument (do you remember: joinless?)
Database Database A file in your disk
Tablespace Collection Bunch of Documents
Tables Documents Group of fields
9Proprietary & Confidential. © 2012 R/GA All rights reserved. /
it is…. BSON (JSON like) - Structure
10Proprietary & Confidential. © 2012 R/GA All rights reserved. /
it is…. Sharding – Improving scalability
Sharding function:
Shard1: first=“A* to G*Shard2: first=“H* to M*”Shard3: first=“N* to S*”Shard4: first=“T* to Z*”
Sample Document with 2 fields: first and last
11Proprietary & Confidential. © 2012 R/GA All rights reserved. /
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!
12Proprietary & Confidential. © 2012 R/GA All rights reserved. /
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. 13/
02Motivation
Proprietary & Confidential. © 2012 R/GA All rights reserved. 14/
Why?
15Proprietary & Confidential. © 2012 R/GA All rights reserved. /
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
16Proprietary & Confidential. © 2012 R/GA All rights reserved. /
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
17Proprietary & Confidential. © 2012 R/GA All rights reserved. /
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
18Proprietary & Confidential. © 2012 R/GA All rights reserved. /
Not convinced yet?
/
Proprietary & Confidential. © 2012 R/GA All rights reserved. 19/
03Tech Stuff
Proprietary & Confidential. © 2012 R/GA All rights reserved. 20/
How?
21Proprietary & Confidential. © 2012 R/GA All rights reserved. /
Connecting…. Drivers
Spring Data for MongoDB
DataNucleos
Complete list: http://www.mongodb.org/display/DOCS/Java+Language+Center
22Proprietary & Confidential. © 2012 R/GA All rights reserved. /
Connecting…. Drivers
• Annotation based• Validation JSR303• Type-safe• DAO<T,V> access abstraction• Easy to implement• Fast• Lightweight• Source code easy to understand
23Proprietary & Confidential. © 2012 R/GA All rights reserved. /
Installing….
24Proprietary & Confidential. © 2012 R/GA All rights reserved. /
Connecting….
Source: BaseApp.java
25Proprietary & Confidential. © 2012 R/GA All rights reserved. /
Annotations
• Entity• Id• Property
Class LevelMaps the class to the collectionCan define the name of collection as param
Field LevelDefines the PK of a collectionIt’s n ObjectId classField LevelDefines a field.
• Serialized• Transient• NotSaved
Field level annotation
Stored in a binary fieldLoaded but not storedNot Loaded and not saved
• Indexed
Field level annotation
Create an index with the field
26Proprietary & Confidential. © 2012 R/GA All rights reserved. /
Annotations
• Entity• Id• Property
• PrePersist• PostPersist• PreLoad• PostLoad
• Reference• Embedded
• Serialized• Transient• NotSaved
Field level annotation
FK, stores the ObjectID
• Indexed
Field level annotation
Store the Object
Before and After actionsPersisting and Loading
27Proprietary & Confidential. © 2012 R/GA All rights reserved. /
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
28Proprietary & Confidential. © 2012 R/GA All rights reserved. /
Collectiondb.states.find() db.cities.find()
@Reference
@Embedded
@Id
FK Detected!
29Proprietary & Confidential. © 2012 R/GA All rights reserved. /
Creating
Create the object
Datastore.save(object)
Database
30Proprietary & Confidential. © 2012 R/GA All rights reserved. /
Deleting
Find the Record
Datastore.delete(object | Query)
31Proprietary & Confidential. © 2012 R/GA All rights reserved. /
Querying / Updating
Find the objects
Apply the update rule
Execute
32Proprietary & Confidential. © 2012 R/GA All rights reserved. /
Pre/Post … Persist/Load
Pre/Post Persist
Pre/Post Load
33Proprietary & Confidential. © 2012 R/GA All rights reserved. /
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
34Proprietary & Confidential. © 2012 R/GA All rights reserved. /
Links
Delicious Stack:
http://www.delicious.com/stacks/view/GmHJ5R
Proprietary & Confidential. © 2012 R/GA All rights reserved. 35/
Thanks!