mongodb and spring - two leaves of a same tree

44

Upload: mongodb

Post on 15-Jul-2015

987 views

Category:

Technology


2 download

TRANSCRIPT

Page 1: MongoDB and Spring - Two leaves of a same tree
Page 2: MongoDB and Spring - Two leaves of a same tree

MongoDB + Spring

Norberto Leite

@nleite

[email protected]

http://www.mongodb.com/norberto

Two leafs of the same tree

Page 3: MongoDB and Spring - Two leaves of a same tree

3

Agenda

• MongoDB Introduction

– Just in case you've been distracted

• Spring Framework Overview

• Spring + MongoDB

– Spring Data

– Spring Boot

– Spring Batch

Page 4: MongoDB and Spring - Two leaves of a same tree

4

Ola, I'm Norberto!

Norberto Leite

Technical Evangelist

Madrid, Spain

@nleite

[email protected]

http://www.mongodb.com/norberto

Page 5: MongoDB and Spring - Two leaves of a same tree

MongoDB

Page 6: MongoDB and Spring - Two leaves of a same tree

6

MongoDB

GENERAL PURPOSE DOCUMENT DATABASE OPEN-SOURCE

Page 7: MongoDB and Spring - Two leaves of a same tree

7

MONGODB FEATURES

JSON Document Model

with Dynamic Schemas

Auto-Sharding for

Horizontal Scalability

Text Search

Aggregation Framework

and MapReduce

Full, Flexible Index Support

and Rich Queries

Built-In Replication

for High Availability

Advanced Security

Large Media Storage

with GridFS

Page 8: MongoDB and Spring - Two leaves of a same tree

8

THE LARGEST ECOSYSTEM

9,000,000+MongoDB Downloads

250,000+Online Education Registrants

35,000+MongoDB User Group Members

35,000+MongoDB Management Service (MMS) Users

750+Technology and Services Partners

2,000+Customers Across All Industries

Page 9: MongoDB and Spring - Two leaves of a same tree

Document Data Model

Relational MongoDB

{

first_name: ‘Paul’,

surname: ‘Miller’,

city: ‘London’,

location:

[45.123,47.232],

cars: [

{ model: ‘Bentley’,

year: 1973,

value: 100000, … },

{ model: ‘Rolls Royce’,

year: 1965,

value: 330000, … }

]

}

Page 10: MongoDB and Spring - Two leaves of a same tree

10

Documents are Rich Data Structures

{

first_name: ‘Paul’,

surname: ‘Miller’,

cell: ‘+447557505611’

city: ‘London’,

location: [45.123,47.232],

Profession: [banking, finance, trader],

cars: [

{ model: ‘Bentley’,

year: 1973,

value: 100000, … },

{ model: ‘Rolls Royce’,

year: 1965,

value: 330000, … }

]

}

Fields can contain an array of sub-

documents

Fields

Typed field values

Fields can contain

arrays

Page 11: MongoDB and Spring - Two leaves of a same tree

11

Document Model Benefits

Agility and flexibility

Data model supports business change

Rapidly iterate to meet new requirements

Intuitive, natural data representation

Eliminates ORM layer

Developers are more productive

Reduces the need for joins, disk seeks

Programming is more simple

Performance delivered at scale

{

_id : ObjectId("4c4ba5e5e8aabf3"),

employee_name: "Dunham, Justin",

department : "Marketing",

title : "Product Manager, Web",

report_up: "Neray, Graham",

pay_band: “C",

benefits : [

{ type : "Health",

plan : "PPO Plus" },

{ type : "Dental",

plan : "Standard" }

]

}

Page 12: MongoDB and Spring - Two leaves of a same tree

Dynamic Schema

{

policyNum: 123,

type: auto,

customerId: abc,

payment: 899,

deductible: 500,

make: Taurus,

model: Ford,

VIN: 123ABC456,

}

{

policyNum: 456,

type: life,

customerId: efg,

payment: 240,

policyValue: 125000,

start: jan, 1995

end: jan, 2015

}

{

policyNum: 789,

type: home,

customerId: hij,

payment: 650,

deductible: 1000,

floodCoverage: No,

street: “10 Maple Lane”,

city: “Springfield”,

state: “Maryland”

}

Page 13: MongoDB and Spring - Two leaves of a same tree

Query Operators

Conditional Operators $all, $exists, $mod, $ne, $in, $nin, $nor, $or, $size,

$type

$lt, $lte, $gt, $gte

// find customers with any claims

> db.customers.find( {claims: {$exists: true }} )

// find customers matching a regular expression

> db.customers.find( {last: /^rog*/i } )

// count customers by city

> db.customers.find( {city: ‘Philadelphia’} ).count()

Page 14: MongoDB and Spring - Two leaves of a same tree

14

Indexes

// Index nested documents

> db.customers.ensureIndex({“policies.agent”:1} )

> db.customers.find({‘policies.agent’:’Fred’})

// geospatial index

> db.customers.ensureIndex({“property.location”: “2d” } )

> db.customers.find({“property.location” : { $near : [22,42] }} )

// text index

> db.customers.ensureIndex({“policies.notes”: “text” } )

Page 15: MongoDB and Spring - Two leaves of a same tree

MongoDB is Fully Featured

Page 16: MongoDB and Spring - Two leaves of a same tree

Spring Framework

Not just a framework!

Page 17: MongoDB and Spring - Two leaves of a same tree

17

Spring Framework

Page 18: MongoDB and Spring - Two leaves of a same tree

Spring Projects

Page 19: MongoDB and Spring - Two leaves of a same tree

Spring & MongoDB

Page 20: MongoDB and Spring - Two leaves of a same tree

Same Tree?

Page 21: MongoDB and Spring - Two leaves of a same tree

Let's put these 2 to work!

Page 22: MongoDB and Spring - Two leaves of a same tree

Build a "cool" App

Page 23: MongoDB and Spring - Two leaves of a same tree
Page 24: MongoDB and Spring - Two leaves of a same tree

24

Nice and simple

Page 25: MongoDB and Spring - Two leaves of a same tree

25

Video Catalog App

• All videos from our Education platform

– Yes, we have the coolest framework ever for

remote education!

https://university.mongodb.com/

Page 26: MongoDB and Spring - Two leaves of a same tree

26

Video Catalog App

• All videos from our Education platform

– Yes, we have the coolest framework ever for

remote education!

• Load information into MongoDB

• Allow people to vote on videos

• Find videos based on the metadata

• All using Spring Projects

– Let's also look into optimizations

Page 27: MongoDB and Spring - Two leaves of a same tree

27

Stack

MongoDB

DAL – Database Access Layer

demo.springio.batch REST API

Page 28: MongoDB and Spring - Two leaves of a same tree

CODE, CODE, CODE

Page 29: MongoDB and Spring - Two leaves of a same tree

29

DAL

• Decoupling Database

– Rule n1 on good Software Development

• Base Library

• Simple

Page 30: MongoDB and Spring - Two leaves of a same tree

30

Batch

• Batch Configuration Class

– reader()

– writer()

@Configuration

@EnableBatchProcessing

• ItemProcessor()

– Excellent way to do pre-aggregations,

computations…

Page 31: MongoDB and Spring - Two leaves of a same tree

31

Batch

• Things to look for

– chunk(chunkSize)

• Keep an eye on this value to optmise the loading

process

– writer()

• MongoDB bulk insert is here to help

Page 32: MongoDB and Spring - Two leaves of a same tree

32

REST API

@RestController

@RequestMapping("/…")

@EnableAutoConfiguration

… extends MongoRepository<T, I>

Page 33: MongoDB and Spring - Two leaves of a same tree

33

REST API

• Things to have in mind

– MongoRepository is "just" CRUD repository

• Need to autowire MongoTemplate to aggregate

– Updates

• Not a particular issue of SpringData

• General thing of ODM's

Page 34: MongoDB and Spring - Two leaves of a same tree

http://1.bp.blogspot.com/-H40bE-rJUqk/UXo3YatYjqI/AAAAAAAAAho/zjrQVk3WrNA/s1600/paella_03.jpg

Take Ways

Page 35: MongoDB and Spring - Two leaves of a same tree

35

Recap

• Spring has a lot of things out of the box that we do not

need to reinvent the all the time

• MongoDB can easily be integrated with existing Spring

based projects

• Performance is important

• Continuous Improvement is key!

Page 36: MongoDB and Spring - Two leaves of a same tree

36

Recap

• DOCUMENTATION!

– Awesome and complete on both projects!

Page 37: MongoDB and Spring - Two leaves of a same tree

We love enterprise!

Page 38: MongoDB and Spring - Two leaves of a same tree

38

MongoDB 3.0 is here!

Page 39: MongoDB and Spring - Two leaves of a same tree

http://www.humanandnatural.com/data/media/178/badan_jaran_desert_oasis_china.jpghttp://www.tinypm.com/blog/wp-content/uploads/2015/01/hammer.jpg

Fully Available!

3.0.2 https://www.mongodb.org/downloads

http://www.mongodb.com/norberto

Page 40: MongoDB and Spring - Two leaves of a same tree

40

Register now: mongodbworld.com

Early Bird Ends May 1!

Use Code NorbertoLeite for additional 25% Off*Come as a group of 3 or more – Save another 25%

Page 41: MongoDB and Spring - Two leaves of a same tree

We’re Always Looking for Top Talent

What are employees saying?

“Working with a group of individuals who you know will have your back is

one of the reasons I love working at MongoDB”

“Every day, we get to solve hard problems that make distributed databases

more accessible to developers all over the world”

“MongoDB lets you tackle real problems that affect hundreds of thousands

of users”

Visit us at www.mongodb.com/careers to see a full

list of opportunities or email your resume to

[email protected]

What are we hiring for?

• Technical Services Engineers (Dublin)

• Consulting Engineers (UK OR France)

• Solution Architects (France, Spain, Germany)

• Enterprise Account Executives ( France, Italy, UK, Germany)

• Corporate Account Executives (Dublin)

• Renewals Account Managers (Dublin)

Page 42: MongoDB and Spring - Two leaves of a same tree

Same Tree!!!

Page 43: MongoDB and Spring - Two leaves of a same tree

http://www.humanandnatural.com/data/media/178/badan_jaran_desert_oasis_china.jpg

Questions?

@nleite

[email protected]

Page 44: MongoDB and Spring - Two leaves of a same tree