using Spring and MongoDB on Cloud Foundry

1,578 views

Published on

This talk introduces how to build MongoDB applications with Spring Data MongoDB on Cloud Foundry. Spring Data provides rich support for easily building applications that work on multiple data stores.

Published in: Technology
  • Be the first to comment

using Spring and MongoDB on Cloud Foundry

  1. 1. Using Spring and MongoDB withCloudFoundryJosh Long@starbuxmanSpring Developer AdvocateSpringSource, a division of VMware © 2009 VMware Inc. All rights reserved
  2. 2. About Josh Long Spring Developer Advocate @starbuxman josh.long@springsource.com th si is im po rta nt ! Free Book! SpringSource.org/roo 2
  3. 3. Spring 3
  4. 4. Spring’s aim: bring simplicity to Java development data web tier integration batch access & service tier & mobile processing / NoSQL / RIA messaging Big Data The Spring frameworkthe cloud: lightweight traditional CloudFoundry WebSphere tc Server VMForce JBoss AS Tomcat Google App Engine WebLogic Jetty Amazon Web Services (on legacy versions, too!) 4
  5. 5. Spring Data 5
  6. 6. Data Access Challenge #1: Scale Horizontally 6
  7. 7. Data Access Challenge #2: Heterogeneous data access needs 7
  8. 8. New demands on data access•... until we neededinexpensive horizontalscaling for some large webbased applications ...• ... and we needed todeploy our apps in thecloud ... * image courtesy of Bitcurrent 8
  9. 9. NoSQL offers several data store categoriesKey-Value Column Document Graph Redis, Cassandra, MongoDB Neo4J Riak HBase 9
  10. 10. NoSQL offers several data store categoriesKey-Value Column Document Graph MongoDB (who cares about the rest?) 10
  11. 11. Spring Framework built-in data access support•Transaction abstractions•Common data access exception hierarchy•JDBC - JdbcTemplate•ORM - Hibernate, JPA support•OXM - Object to XML mapping•Serializer/Deserializer strategies (Spring 3.0)•Cache support (Spring 3.1) 11
  12. 12. http://www.springsource.org/spring-data•Spring Data Key-value•Spring Data Document•Spring Data Graph•Spring Data Column•Spring Data Blob•Spring Data JPA Repository / JDBC Extensions•Spring Gemfire / Spring Hadoop ...•Grails iNcOnSeQuentiaL 12
  13. 13. Spring Data Building Blocks•Low level data access APIs ✓MongoTemplate, RedisTemplate ...•Object Mapping (Java and GORM)•Cross Store Persistence Programming model•Generic Repository support•Productivity support in Roo and Grails 13
  14. 14. Spring MongoDB 14
  15. 15. Spring Data Document Mongo•MongoTemplate interface for mapping Mongo documents •MongoConverter •SimpleMongoConverter for basic POJO mapping support •Leverage Spring 3.0 TypeConverters and SpEL •Exception translation•Advanced Mapping(@Document, @Id, @DbRef) •Annotation based•MongoRepository •Built on Hades support for JPA Repositories 15
  16. 16. Simple Domain Class 16
  17. 17. Mongo TemplateDirect Usage of the Mongo Template: 17
  18. 18. Mongo TemplateDirect Usage of the Mongo Template: Insert into “Person” Collection 17
  19. 19. Mongo TemplateDirect Usage of the Mongo Template: findOne using query: { "name" : "Joe"} in db.collection: database.Person 17
  20. 20. Mongo TemplateDirect Usage of the Mongo Template: Dropped collection [database.person] 17
  21. 21. Generic RepositoryInterface for generic CRUD operations on a repository for a specific type 18
  22. 22. Paging and Sorting RepositoryPaging and Sorting Repository: Extends “CrudRepository” 19
  23. 23. Paging and Sorting RepositoryPaging and Sorting Repository: Extends “CrudRepository” Usage: 19
  24. 24. Custom RepositoryCustom Repository: 20
  25. 25. Custom RepositoryCustom Repository:Keywords :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 name) "firstname" : firstname} (regex) 20
  26. 26. Cross Store 21
  27. 27. JPA and MongoDB JPA “Customer” with a “SurveyInfo” Document 22
  28. 28. Using a Cross-Store Saving a Customer with a SurveryInfo 23
  29. 29. Using a Cross-Store Saving a Customer with a SurveryInfo Create Customer 23
  30. 30. Using a Cross-Store Saving a Customer with a SurveryInfo Create SurveyInfo 23
  31. 31. Using a Cross-Store Saving a Customer with a SurveryInfo Assign Survey to Customer 23
  32. 32. Using a Cross-Store Saving a Customer with a SurveryInfo Save 23
  33. 33. Using a Cross-Store Saving a Customer with a SurveryInfo Save Mongo Document: 23
  34. 34. Cloud Foundry 24
  35. 35. Cloud Foundry github.com/cloudfoundry cloudfoundry.org @cloudfoundry Supports: • Languages: Java, Scala, Node.js, Grails, Ruby/Rails/Sinatra, etc. • Services: MongoDB, Redis, MySQL, PostgreSQL, RabbitMQ ecosystem forks: • AppFog - PHP • Stacato: Python, Perl, etc., from ActiveState 25
  36. 36. Choice of clouds .js ce rfa Ap te Data Private p In lic Services Clouds er at io vid n Public o Se Msg Pr Services r Clouds vic d ou e In Cl Micro te Other r Services Clouds fa e c 26
  37. 37. Choice of clouds .js ce rfa Ap te Data Private p In lic Services Clouds … er at io vid n Public o Se Msg Pr Services r Clouds vic d ou e In .COM Cl Micro te Other r Services Clouds fa e c 26
  38. 38. Setting up Cloud Foundry Demo 27
  39. 39. Accessing Services Bound to Cloud Foundry (producer)private String mongoDatabaseServiceName = "survey-mongo";@Beanpublic CloudEnvironment cloudEnvironment() { return new CloudEnvironment();}@Beanpublic MongoServiceInfo mongoServiceInfo() { return cloudEnvironment().getServiceInfo( mongoDatabaseServiceName, MongoServiceInfo.class);}@Beanpublic MongoDbFactory mongoDbFactory() { MongoServiceCreator msc = new MongoServiceCreator(); MongoDbFactory db = msc.createService(mongoServiceInfo());} 28
  40. 40. Accessing Services Bound to Cloud Foundry (consumption)@Inject private Mongo mongo ;@Inject private MongoTemplate mongoTemplate; 29
  41. 41. Cloud Foundry MongoDB Demo 30
  42. 42. Where to Find More Spring Data Project: http://bit.ly/spring-data CloudFoundry Samples: http://bit.ly/cloudfoundry-samples MicroCloud Foundry for Spring Developers http://bit.ly/mcf4spring Spring Data Mongo on Cloud Foundry (webinar, 12/01/2011) http://bit.ly/spring-mongo-cloudfoundry 31

×