Handwritten Text Recognition for manuscripts and early printed texts
Thomas risberg mongosv-2012-spring-data-cloud-foundry
1. Simplify your
MongoDB Java cloud apps
with Spring Data
Thomas Risberg, Cloud Foundry Team,VMware
trisberg@vmware.com
@trisberg
Spring Data
Tuesday, December 4, 12 1
2. 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
Tuesday, December 4, 12 2
3. 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
Tuesday, December 4, 12 3
4. 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.
Tuesday, December 4, 12 4
5. Spring Data
Supported Technologies:
Tuesday, December 4, 12 5
6. 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
interface
findAll
findByName ...
Pagination and sorting support
Currently has JPA and Mongo implementations
Tuesday, December 4, 12 6
7. Spring Data CrudRepository
• Interface for for a specific type
a repository
generic CRUD operations on
Tuesday, December 4, 12 7
8. Paging and Sorting Repository
Paging and Sorting Repository: Extends “CrudRepository”
Usage:
8
Tuesday, December 4, 12 8
10. MongoDB Repository
public interface DeveloperRepository
extends MongoRepository<Developer, String> {
}
MongoRepository extends
PagingAndSortingRepository,
it’s all free ...
10
Tuesday, December 4, 12 10
11. MongoDB Repository
or more control ...
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);
}
11
Tuesday, December 4, 12 11
12. Spring Data and MongoDB Repository
App deployed to cloud in 10 steps ...
LIVE DEMO
https://github.com/trisberg/mongosv-books/tree/master/demo
Tuesday, December 4, 12 12
13. 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
Tuesday, December 4, 12 13
14. 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
Tuesday, December 4, 12 14
15. MongoTemplate CRUD
Commonly 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
Tuesday, December 4, 12 15
16. 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
Tuesday, December 4, 12 16
17. 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.
Tuesday, December 4, 12 17
18. 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]
Tuesday, December 4, 12 18
23. 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
Tuesday, December 4, 12 23
24. Lots of choices
Ap
.js
pli
ce
Private
ca
rfa
Data Services
tio
nte
Clouds
n
rI
Se
ide
rvi
Public
ov
ce
Msg Services
Pr
Clouds Partners
Int
d
ou
er
.COM
fa
Cl
Micro
ce
Other
Services
Clouds
Tuesday, December 4, 12 24
26. Cloud Foundry - login & info
$ gem install vmc
...
$ gem install tunnel-vmc-plugin
...
$ vmc target api.cloudfoundry.com
Setting target to https://api.cloudfoundry.com... OK
$ vmc login trisberg@springdeveloper.com
target: https://api.cloudfoundry.com
Password> *****
Authenticating... OK
$ vmc info
VMware's Cloud Application Platform
target: https://api.cloudfoundry.com
version: 0.999
support: http://support.cloudfoundry.com
user: trisberg@springdeveloper.com
Tuesday, December 4, 12 26
27. Cloud Foundry - runtimes & frameworks
$ vmc info --runtimes --frameworks
Getting runtimes... OK
Getting frameworks... OK
runtime description
java 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 description
grails
java_web
lift
node
play
rack
rails3
sinatra
spring
standalone
Tuesday, December 4, 12 27
28. Cloud Foundry - services
$ vmc info --services
Getting 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 services
Getting services... OK
name service version
mongodb-vtoons mongodb 1.8
mysql-books mysql 5.1
rabbit-test rabbitmq 2.4
postgresql-eebf5 postgresql 9.0
Tuesday, December 4, 12 28
29. Cloud Foundry - provision a service
$ vmc create-service mongodb mongosv-books
Creating service mongosv-books... OK
$ vmc services
Getting services... OK
name service version
mongosv-books mongodb 2.0
mongodb-vtoons mongodb 1.8
mysql-books mysql 5.1
rabbit-test rabbitmq 2.4
postgresql-eebf5 postgresql 9.0
Tuesday, December 4, 12 29
30. Cloud Foundry - tunnel into a service
$ vmc tunnel mongosv-books
1: none
2: mongo
3: mongodump
4: mongorestore
Which client would you like to start?> 2
Opening tunnel on port 10000... OK
Waiting for local tunnel to become available... OK
MongoDB shell version: 2.0.4
connecting to: localhost:10000/db
> show collections
author
book
system.indexes
system.users
>
Tuesday, December 4, 12 30
31. Cloud Foundry - push app
$ vmc push mongosv-web --path target
Instances> 1
1: spring
2: other
Framework> spring
1: java
2: java7
3: other
Runtime> java7
...
Memory Limit> 512M
URL> mongosv-web.cloudfoundry.com
Create services for application?> n
Bind other services to application?> y
1: mongosv-books
Which service instance?> 1
Binding mongosv-books to mongosv-web... OK
Bind another service?> n
Save configuration?> n
Uploading mongosv-web... OK
Starting mongosv-web... OK
Checking mongosv-web... OK
Tuesday, December 4, 12 31
32. Cloud Foundry - scale app
$ vmc scale mongosv-web --instances 2
Scaling mongosv-web... OK
$ vmc app mongosv-web
mongosv-web: running
platform: spring on java7
usage: 512M × 2 instances
urls: mongosv-web.cloudfoundry.com
services: mongosv-books
Tuesday, December 4, 12 32
33. 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)
Tuesday, December 4, 12 33