thomas risberg mongosv-2012-spring-data-cloud-foundry

44
Simplify your MongoDB Java cloud apps with Spring Data Thomas Risberg, Cloud Foundry Team,VMware [email protected] @trisberg Spring Data 1 Tuesday, December 4, 12

Upload: trisberg

Post on 13-May-2015

2.510 views

Category:

Technology


0 download

DESCRIPTION

MongosSV 2012 Simplify your MongoDB Java cloud apps with Spring Data

TRANSCRIPT

Page 1: Thomas risberg mongosv-2012-spring-data-cloud-foundry

Simplify your MongoDB Java cloud apps

with Spring Data Thomas Risberg, Cloud Foundry Team, VMware

[email protected]@trisberg

Spring Data

1Tuesday, December 4, 12

Page 2: Thomas risberg mongosv-2012-spring-data-cloud-foundry

About me

Currently working on the Cloud Foundry team at VMware

•Member of Spring Data Team‣ Co-author of “Spring Data” from O’Reilly

• Joined Spring Framework project in 2003‣ Co-author of “Professional Java Development with the Spring Framework” from Wrox

2Tuesday, December 4, 12

Page 3: Thomas risberg mongosv-2012-spring-data-cloud-foundry

Introducing - Spring Data

• Data access landscape has changed considerably

• RDBMS are still important and predominant‣ but no longer considered a “one size fits all” solution

• Spring has always provided excellent data access support‣ Transaction Management

‣ Portable data access exception hierarchy

‣ JDBC – JdbcTemplate

‣ ORM - Hibernate, JPA, JDO, Ibatis support

‣ Cache support (Spring 3.1)

• Spring Data project goal is to “refresh” Spring’s data support

3Tuesday, December 4, 12

Page 4: Thomas risberg mongosv-2012-spring-data-cloud-foundry

Spring Data - Mission Statement

Provide a familiar and consistent Spring-based programming model for Big Data, NoSQL, and relational stores while retaining store-specific features and capabilities.

4Tuesday, December 4, 12

Page 5: Thomas risberg mongosv-2012-spring-data-cloud-foundry

Spring Data Supported Technologies:

5Tuesday, December 4, 12

Page 6: Thomas risberg mongosv-2012-spring-data-cloud-foundry

Spring Data Repository

Spring Data Repository basics:Generic repository implementation Basic CRUD (create, read, update and delete) methods

Generating code for queries defined in repository interfacefindAllfindByName ...Pagination and sorting supportCurrently has JPA and Mongo implementations

6Tuesday, December 4, 12

Page 7: Thomas risberg mongosv-2012-spring-data-cloud-foundry

• Interface for generic CRUD operations on a repository for a specific type

Spring Data CrudRepository

7Tuesday, December 4, 12

Page 8: Thomas risberg mongosv-2012-spring-data-cloud-foundry

8

Extends “CrudRepository”

Usage:

Paging and Sorting Repository:

Paging and Sorting Repository

8Tuesday, December 4, 12

Page 9: Thomas risberg mongosv-2012-spring-data-cloud-foundry

9

Repository Finder Methods:

Keyword Sample Logical resultGreaterThan findByAgeGreaterThan(int age) {"age" : {"$gt" : age}}

LessThan findByAgeLessThan(int age) {"age" : {"$lt" : age}}

Between findByAgeBetween(int from, int to) {"age" : {"$gt" : from, "$lt" : to}}

NotNull findByFirstnameNotNull() {”firstname" : {"$ne" : null}}

Null findByFirstnameNull() {”firstname" : null}

Like findByFirstnameLike(String pattern) { "firstname" : { "$regex" : "pattern"}}

Keyword examples :

Repository Finders

9Tuesday, December 4, 12

Page 10: Thomas risberg mongosv-2012-spring-data-cloud-foundry

10

MongoDB Repository

public interface DeveloperRepository extends MongoRepository<Developer, String> {}

MongoRepository extends PagingAndSortingRepository,

it’s all free ...

10Tuesday, December 4, 12

Page 11: Thomas risberg mongosv-2012-spring-data-cloud-foundry

11

MongoDB Repository

public interface BookRepository extends Repository<Book, String> { Book save(Book book); Book findOne(String isbn); void delete(String isbn); List<Book> findAll();

List<Book> findByAuthors(Author author);

List<Book> findByPublishedGreaterThan(Date date); List<Book> findByCategoriesIn(String[] categories);

List<Book> findByPublishedGreaterThanAndCategoriesIn(Date date, String[] categories); }

or more control ...

11Tuesday, December 4, 12

Page 12: Thomas risberg mongosv-2012-spring-data-cloud-foundry

Spring Data and MongoDB Repository App deployed to cloud in 10 steps ...

LIVE DEMOhttps://github.com/trisberg/mongosv-books/tree/master/demo

12Tuesday, December 4, 12

Page 13: Thomas risberg mongosv-2012-spring-data-cloud-foundry

MongoTemplate

Spring Data support for MongoDB:

• MongoTemplate✓MongoConverter interface for mapping Mongo

documents• Built-in Advanced Mapping

– Annotation based (@Document, @Id, @DbRef)• MappingMongoConverter for POJO mapping support• Leverage Spring 3.0 TypeConverters and SpEL

✓Exception translation✓Java based Query, Criteria, and Update DSLs

13Tuesday, December 4, 12

Page 14: Thomas risberg mongosv-2012-spring-data-cloud-foundry

Mapping POJOs

Annotations for mapping POJOs to documents:• @Document✓Identifies a domain object to be persisted to MongoDB

• @Id✓Identifies a field should be used as the _id of the document

• @Field✓define custom metadata for document fields‣ value defines key to be used to store the field in the document‣order specifies in which order various fields shall be stored

• @DbRef✓indicates the annotated field is to be stored using a DBRef

14Tuesday, December 4, 12

Page 15: Thomas risberg mongosv-2012-spring-data-cloud-foundry

MongoTemplate CRUDCommonly used MongoTemplate methods:• insert(object)✓Inserts a mapped POJO as a document

• save(object)✓Inserts or updates a mapped POJO as a document

• updateFirst(query, update, entityClass)• updateMulti(query, update, entityClass) ✓Executes an update statement for the collection of the entityClass

-- either the first or multiple documents matching the query• remove(object) ✓Remove the given object from the collection by id

15Tuesday, December 4, 12

Page 16: Thomas risberg mongosv-2012-spring-data-cloud-foundry

MongoTemplate Finders

Commonly used MongoTemplate finder methods:• findById(id, entityClass)✓Returns a document with the given id mapped onto the given

entityClass• findOne(query, entityClass) ✓Returns the first document from the results of an ad-hoc query

mapped onto the given entityClass• find(query, entityClass) ✓Returns a List of documents from the results of an ad-hoc query

mapped onto the given entityClass• findAll(entityClass) ✓Returns a List of documents from the collection used by the entity

class mapped onto the given entity class

16Tuesday, December 4, 12

Page 17: Thomas risberg mongosv-2012-spring-data-cloud-foundry

Working with queries

Commonly used Query and Criteria methods:• searchCriteria = Criteria.where("published").gte(startDate)

✓Creates a Criteria where a field is greater or equal to specified date• searchCriteria.and("categories").in((Object[])categoriesToMatch) ✓adds to the criteria a clause where a fields value included in

passed in array of values• Query query = new Query(searchCriteria)

✓Creates a Query based on the Criteria built above.• mongoTemplate.find(query, Book.class)

✓Uses the Query built above to find all book documents that match the Criteria.

17Tuesday, December 4, 12

Page 18: Thomas risberg mongosv-2012-spring-data-cloud-foundry

MongoTemplate Example

Direct Usage of the Mongo Template:

Insert into “Person” Collection

findOne using query: { "name" : "Joe"} in db.collection: database.Person

Drop collection [database.person]

18Tuesday, December 4, 12

Page 19: Thomas risberg mongosv-2012-spring-data-cloud-foundry

ConfigurationNamespace support for MongoDB

<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mongo="http://www.springframework.org/schema/data/mongo" xsi:schemaLocation="http://www.springframework.org/schema/data/mongo http://www.springframework.org/schema/data/mongo/spring-mongo-1.0.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd"> <bean id="mongoTemplate" class="org.springframework.data.mongodb.core.MongoTemplate"> <constructor-arg name="mongoDbFactory" ref="mongoDbFactory"/> </bean>

<mongo:db-factory host="localhost" port="27017" dbname="db"/> </beans>

19Tuesday, December 4, 12

Page 20: Thomas risberg mongosv-2012-spring-data-cloud-foundry

Using a custom mapperCreate a custom mapper:

Register it in your app context:

@Componentpublic class UriWriteConverter implements Converter<URI, DBObject> {

public DBObject convert(URI source) { DBObject dbo = new BasicDBObject(); dbo.put("URI", source.toString()); return dbo; }}

<mongo:mapping-converter id="mongoConverter"> <mongo:custom-converters base-package="org.springframework.data.demo.convert" /></mongo:mapping-converter>

<bean id="mongoTemplate" class="org.springframework.data.mongodb.core.MongoTemplate"> <constructor-arg name="mongoDbFactory" ref="mongoDbFactory" /> <constructor-arg name="mongoConverter" ref="mongoConverter"/></bean>

20Tuesday, December 4, 12

Page 21: Thomas risberg mongosv-2012-spring-data-cloud-foundry

Cloud Foundry configurationNamespace support for MongoDB

<cloud:mongo-db-factory id="dbFactory" write-concern="SAFE">    <cloud:mongo-options connections-per-host="10" max-wait-time="2000" /></cloud:mongo-db-factory>

<dependency>    <groupId>org.cloudfoundry</groupId>    <artifactId>cloudfoundry-runtime</artifactId>    <version>0.8.2</version></dependency>

<repository>    <id>org.springsource.maven.milestone</id>    <name>Spring Framework Maven Milestone Repository</name>    <url>http://repo.springsource.org/libs-milestone</url></repository>

21Tuesday, December 4, 12

Page 22: Thomas risberg mongosv-2012-spring-data-cloud-foundry

22Tuesday, December 4, 12

Page 23: Thomas risberg mongosv-2012-spring-data-cloud-foundry

What is Cloud Foundry?• Cloud Foundry is a PaaS

– The application platform will be delivered as a service in the cloud era

– The industry calls this platform as a service (PaaS)• PaaS makes it much easier to deploy, run and scale

applications• Cloud Foundry - the application platform for the cloud era

– Integrated software stack– Application execution engine– Self-service application deployment – Automated application infrastructure provisioning – Curated, updated and operated as a service

23Tuesday, December 4, 12

Page 24: Thomas risberg mongosv-2012-spring-data-cloud-foundry

Lots of choices

Clou

d Pr

ovide

r Inte

rface

Application Service Interface

Private Clouds

PublicClouds

MicroClouds

Data Services

Other Services

Msg Services

.js

.COM

Partners

24Tuesday, December 4, 12

Page 25: Thomas risberg mongosv-2012-spring-data-cloud-foundry

Many cloud providers

25Tuesday, December 4, 12

Page 26: Thomas risberg mongosv-2012-spring-data-cloud-foundry

Cloud Foundry - login & info

$ gem install vmc...$ gem install tunnel-vmc-plugin...$ vmc target api.cloudfoundry.comSetting target to https://api.cloudfoundry.com... OK

$ vmc login [email protected]: https://api.cloudfoundry.com

Password> *****

Authenticating... OK

$ vmc infoVMware's Cloud Application Platform

target: https://api.cloudfoundry.com version: 0.999 support: http://support.cloudfoundry.com

user: [email protected]

26Tuesday, December 4, 12

Page 27: Thomas risberg mongosv-2012-spring-data-cloud-foundry

Cloud Foundry - runtimes & frameworks

$ vmc info --runtimes --frameworksGetting runtimes... OKGetting frameworks... OK

runtime descriptionjava 1.6.0_24 java7 1.7.0_04 node 0.4.12 node06 0.6.8 node08 0.8.2 ruby18 1.8.7p357 ruby19 1.9.2p180

framework descriptiongrails java_web lift node play rack rails3 sinatra spring standalone

27Tuesday, December 4, 12

Page 28: Thomas risberg mongosv-2012-spring-data-cloud-foundry

Cloud Foundry - services

$ vmc info --servicesGetting services... OK

service version provider description mongodb 2.0 core MongoDB NoSQL store mysql 5.1 core MySQL database service postgresql 9.0 core PostgreSQL database service (vFabric)rabbitmq 2.4 core RabbitMQ message queue redis 2.2 core Redis key-value store service redis 2.4 core Redis key-value store service redis 2.6 core Redis key-value store service

$ vmc servicesGetting services... OK

name service versionmongodb-vtoons mongodb 1.8 mysql-books mysql 5.1 rabbit-test rabbitmq 2.4 postgresql-eebf5 postgresql 9.0

28Tuesday, December 4, 12

Page 29: Thomas risberg mongosv-2012-spring-data-cloud-foundry

Cloud Foundry - provision a service

$ vmc create-service mongodb mongosv-booksCreating service mongosv-books... OK

$ vmc servicesGetting services... OK

name service versionmongosv-books mongodb 2.0 mongodb-vtoons mongodb 1.8 mysql-books mysql 5.1 rabbit-test rabbitmq 2.4 postgresql-eebf5 postgresql 9.0

29Tuesday, December 4, 12

Page 30: Thomas risberg mongosv-2012-spring-data-cloud-foundry

Cloud Foundry - tunnel into a service

$ vmc tunnel mongosv-books1: none2: mongo3: mongodump4: mongorestoreWhich client would you like to start?> 2

Opening tunnel on port 10000... OKWaiting for local tunnel to become available... OKMongoDB shell version: 2.0.4connecting to: localhost:10000/db> show collectionsauthorbooksystem.indexessystem.users>

30Tuesday, December 4, 12

Page 31: Thomas risberg mongosv-2012-spring-data-cloud-foundry

Cloud Foundry - push app$ vmc push mongosv-web --path targetInstances> 1

1: spring2: otherFramework> spring

1: java2: java73: otherRuntime> java7

...Memory Limit> 512MURL> mongosv-web.cloudfoundry.comCreate services for application?> nBind other services to application?> y

1: mongosv-booksWhich service instance?> 1

Binding mongosv-books to mongosv-web... OKBind another service?> nSave configuration?> n

Uploading mongosv-web... OKStarting mongosv-web... OKChecking mongosv-web... OK

31Tuesday, December 4, 12

Page 32: Thomas risberg mongosv-2012-spring-data-cloud-foundry

Cloud Foundry - scale app

$ vmc scale mongosv-web --instances 2Scaling mongosv-web... OK

$ vmc app mongosv-webmongosv-web: running platform: spring on java7 usage: 512M × 2 instances urls: mongosv-web.cloudfoundry.com services: mongosv-books

32Tuesday, December 4, 12

Page 33: Thomas risberg mongosv-2012-spring-data-cloud-foundry

Tools

• IDE

‣ Cloud Foundry Integration for Eclipse

✓ Spring Tool Suite / Groovy & Grails Tool Suite

• Build Tools

‣ Maven plug-in, Gradle plug-in

• RAD Tools

‣ Grails (Groovy) + Roo (Spring)

33Tuesday, December 4, 12

Page 34: Thomas risberg mongosv-2012-spring-data-cloud-foundry

Cloud Foundry Integration for Eclipse

34Tuesday, December 4, 12

Page 35: Thomas risberg mongosv-2012-spring-data-cloud-foundry

Maven plug-in<plugin> <groupId>org.cloudfoundry</groupId> <artifactId>cf-maven-plugin</artifactId> <version>1.0.0.M4</version> <configuration> <server>cloudfoundry</server> <target>http://api.cloudfoundry.com</target> <url>mongosv-web.cloudfoundry.com</url> <memory>512</memory> <services> <service> <name>mongosv-books</name> <vendor>mongodb</vendor> <version>2.0</version> </service> </services> </configuration></plugin>

<profiles> <profile> <id>appfog</id> <activation> <activeByDefault>false</activeByDefault> </activation> <build> <plugins> <plugin> <groupId>org.cloudfoundry</groupId> <artifactId>cf-maven-plugin</artifactId> <configuration> <server>appfog</server> <target>https://api.appfog.com</target> <url>mongosv-web.aws.af.cm</url> <memory>1024</memory> </configuration> </plugin> </plugins> </build> </profile></profiles>

35Tuesday, December 4, 12

Page 36: Thomas risberg mongosv-2012-spring-data-cloud-foundry

Maven plug-in configuration<pluginRepositories> <pluginRepository> <id>repository.springsource.maven.milestone</id> <name>Spring Framework Maven Milestone Repository</name> <url>http://repo.springsource.org/libs-milestone</url> </pluginRepository></pluginRepositories>

<servers> <server> <id>appfog</id> <username>[email protected]</username> <password>secret</password> </server> <server> <id>cloudfoundry</id> <username>[email protected]</username> <password>secret</password> </server> </servers>

In ~/.m2/settings.xml:

In your pom.xml:

36Tuesday, December 4, 12

Page 37: Thomas risberg mongosv-2012-spring-data-cloud-foundry

Deploying Spring MongoDB apps to Cloud Foundry & AppFog

LIVE DEMO37Tuesday, December 4, 12

Page 38: Thomas risberg mongosv-2012-spring-data-cloud-foundry

Examples ...Spring MVC app with MongoTemplate:

https://github.com/trisberg/mongosv-books/tree/master/mongosv-web

38Tuesday, December 4, 12

Page 39: Thomas risberg mongosv-2012-spring-data-cloud-foundry

RESTful Spring MVC app with Repository:

https://github.com/trisberg/mongosv-books/tree/master/mongosv-rest

Examples ...

39Tuesday, December 4, 12

Page 41: Thomas risberg mongosv-2012-spring-data-cloud-foundry

41Tuesday, December 4, 12

Page 42: Thomas risberg mongosv-2012-spring-data-cloud-foundry

Plans ...

https://jira.springsource.org/browse/DATAMONGO-584

42Tuesday, December 4, 12

Page 44: Thomas risberg mongosv-2012-spring-data-cloud-foundry

Questions?

http://www.sxc.hu/photo/860327

44Tuesday, December 4, 12