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

Post on 13-May-2015

2.513 views 0 download

Tags:

description

MongosSV 2012 Simplify your MongoDB Java cloud apps with Spring Data

Transcript of Thomas risberg mongosv-2012-spring-data-cloud-foundry

Simplify your MongoDB Java cloud apps

with Spring Data Thomas Risberg, Cloud Foundry Team, VMware

trisberg@vmware.com@trisberg

Spring Data

1Tuesday, December 4, 12

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

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

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

Spring Data Supported Technologies:

5Tuesday, December 4, 12

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

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

Spring Data CrudRepository

7Tuesday, December 4, 12

8

Extends “CrudRepository”

Usage:

Paging and Sorting Repository:

Paging and Sorting Repository

8Tuesday, December 4, 12

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

10

MongoDB Repository

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

MongoRepository extends PagingAndSortingRepository,

it’s all free ...

10Tuesday, December 4, 12

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

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

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

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

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

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

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

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

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

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

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

22Tuesday, December 4, 12

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

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

Many cloud providers

25Tuesday, December 4, 12

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 trisberg@springdeveloper.comtarget: 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: trisberg@springdeveloper.com

26Tuesday, December 4, 12

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

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

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

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

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

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

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

Cloud Foundry Integration for Eclipse

34Tuesday, December 4, 12

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

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>cloud@springdeveloper.com</username> <password>secret</password> </server> <server> <id>cloudfoundry</id> <username>trisberg@springdeveloper.com</username> <password>secret</password> </server> </servers>

In ~/.m2/settings.xml:

In your pom.xml:

36Tuesday, December 4, 12

Deploying Spring MongoDB apps to Cloud Foundry & AppFog

LIVE DEMO37Tuesday, December 4, 12

Examples ...Spring MVC app with MongoTemplate:

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

38Tuesday, December 4, 12

RESTful Spring MVC app with Repository:

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

Examples ...

39Tuesday, December 4, 12

41Tuesday, December 4, 12

Plans ...

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

42Tuesday, December 4, 12

Questions?

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

44Tuesday, December 4, 12