Top Banner
36

Java and Mongo

Oct 19, 2014

Download

Technology

Internal Presentation about how to use Java and MongoDB
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Java and Mongo
Page 2: Java and Mongo

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

Java and MongoDB

/** * @author: marcio garcia * @contact: [email protected] **/

Page 3: Java and Mongo

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

Agenda

• Introduction - 6 min.

• WHAT?

• Motivations - 6 min.

• WHY?

• Coding - 12 min.

• HOW?

Page 4: Java and Mongo

/

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

01Introduction

Page 5: Java and Mongo

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

What is? and what is not

Page 6: Java and Mongo

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

Page 7: Java and Mongo

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

Page 8: Java and Mongo

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

Page 9: Java and Mongo

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

it is…. BSON (JSON like) - Structure

Page 10: Java and Mongo

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

Page 11: Java and Mongo

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!

Page 12: Java and Mongo

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”})

Page 13: Java and Mongo

/

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

02Motivation

Page 14: Java and Mongo

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

Why?

Page 15: Java and Mongo

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

Page 16: Java and Mongo

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

Page 17: Java and Mongo

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

Page 18: Java and Mongo

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

Not convinced yet?

Page 19: Java and Mongo

/

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

03Tech Stuff

Page 20: Java and Mongo

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

How?

Page 21: Java and Mongo

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

Page 22: Java and Mongo

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

Page 23: Java and Mongo

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

Installing….

Page 24: Java and Mongo

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

Connecting….

Source: BaseApp.java

Page 25: Java and Mongo

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

Page 26: Java and Mongo

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

Page 27: Java and Mongo

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

Page 28: Java and Mongo

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

Collectiondb.states.find() db.cities.find()

@Reference

@Embedded

@Id

FK Detected!

Page 29: Java and Mongo

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

Creating

Create the object

Datastore.save(object)

Database

Page 30: Java and Mongo

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

Deleting

Find the Record

Datastore.delete(object | Query)

Page 31: Java and Mongo

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

Querying / Updating

Find the objects

Apply the update rule

Execute

Page 32: Java and Mongo

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

Pre/Post … Persist/Load

Pre/Post Persist

Pre/Post Load

Page 33: Java and Mongo

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

Page 34: Java and Mongo

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

Links

Delicious Stack:

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

Page 35: Java and Mongo

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

Thanks!

Page 36: Java and Mongo