MongoDB + Spring

44

Transcript of MongoDB + Spring

Page 1: MongoDB + Spring
Page 2: MongoDB + Spring

MongoDB + Spring

Norberto Leite

@nleite

[email protected]

http://www.mongodb.com/norberto

Two leafs of the same tree

Page 3: MongoDB + Spring

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 + Spring

4

Ola, I'm Norberto!

Norberto Leite

Technical Evangelist

Madrid, Spain

@nleite

[email protected]

http://www.mongodb.com/norberto

Page 5: MongoDB + Spring

MongoDB

Page 6: MongoDB + Spring

6

MongoDB

GENERAL PURPOSE DOCUMENT DATABASE OPEN-SOURCE

Page 7: MongoDB + Spring

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 + Spring

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 + Spring

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 + Spring

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 + Spring

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 + Spring

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 + Spring

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 + Spring

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 + Spring

MongoDB is Fully Featured

Page 16: MongoDB + Spring

Spring Framework

Not just a framework!

Page 17: MongoDB + Spring

17

Spring Framework

Page 18: MongoDB + Spring

Spring Projects

Page 19: MongoDB + Spring

Spring & MongoDB

Page 20: MongoDB + Spring

Same Tree?

Page 21: MongoDB + Spring

Let's put these 2 to work!

Page 22: MongoDB + Spring

Build a "cool" App

Page 23: MongoDB + Spring
Page 24: MongoDB + Spring

24

Nice and simple

Page 25: MongoDB + Spring

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 + Spring

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 + Spring

27

Stack

MongoDB

DAL – Database Access Layer

demo.springio.batch REST API

Page 28: MongoDB + Spring

CODE, CODE, CODE

Page 29: MongoDB + Spring

29

DAL

• Decoupling Database

– Rule n1 on good Software Development

• Base Library

• Simple

Page 30: MongoDB + Spring

30

Batch

• Batch Configuration Class

– reader()

– writer()

@Configuration

@EnableBatchProcessing

• ItemProcessor()

– Excellent way to do pre-aggregations,

computations…

Page 31: MongoDB + Spring

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 + Spring

32

REST API

@RestController

@RequestMapping("/…")

@EnableAutoConfiguration

… extends MongoRepository<T, I>

Page 33: MongoDB + Spring

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 + Spring

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

Take Ways

Page 35: MongoDB + Spring

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 + Spring

36

Recap

• DOCUMENTATION!

– Awesome and complete on both projects!

Page 37: MongoDB + Spring

We love enterprise!

Page 38: MongoDB + Spring

38

MongoDB 3.0 is here!

Page 39: MongoDB + Spring

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 + Spring

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 + Spring

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 + Spring

Same Tree!!!

Page 43: MongoDB + Spring

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

Questions?

@nleite

[email protected]

Page 44: MongoDB + Spring