Hands-On Nosql: Mongodb & Spring Integration
Hands-On Nosql: Mongodb & Spring Integration
4/28/12
http://automateddeveloper.blogsp
Im a professional Software Engineer based in the UK. Neither I, nor this presentation, are in any way affiliated with SpringSource or MongoDB Still want more?
4/28/12
What is NoSQL?
The NoSQL umbrella covers a range of non-relational databases, including keyvalue, document , wide column store and 4/28/12 graph based databases
Why NoSQL?
NoSQLs recent increase in popularity being largely driven by the explosion in amount of data that is now being created/captured on the web Many leading web 2.0 sites now use NoSQL implementations:
Facebook Cassandra (originally developed in-house and later opensourced Hadoop & Cassandra
Twitter 4/28/12
In the News
4,850% $14milli
Percent increase in NoSQL jobs since November 2009 as reported by SimplyHired.com
on
Amount raised in Series C Funding last week by CouchBase for further investment in to NoSQL solutions(Aug2011)
8TB
Amount of data generated everyday in Twitter, and needs to be supported by their NoSQL solution (figure from 2010)
4/28/12
MongoDB
An Open Source Document oriented database Dynamic schemas, storing data in JSON style documents that allows for addition of data on the fly Supports indexing of documents Auto-sharding supported, providing full horizontal scaling
4/28/12
Core Spring and the Spring MVC framework are enterprise standard Java libraries for building professional web sites
As more and more web sites need to be able to handle huge amounts of data, there will be an increased need to be able to build professional sites incorporating NoSQL solutions 4/28/12
4/28/12
Getting Started
This overview does not cover details of setting up a Spring MVC app, and will assume a working knowledge of the framework Only the configuration to connect an existing Spring MVC app will be covered in detail
Before proceeding, install MongoDB full details are available: 4/28/12 http://www.mongodb.org/display/DOCS/Q
Domain Objects
Similar to using JPA(Hibernate, etc) the first step is to model the underlying Domain objects you want to use to model the applications underlying meta-data. Using Spring Data we can model our domain using POJOs and annotations.
4/28/12
Domain Objects
@Document annotation on the root Document object @Id on any member variable to be used as an identifier for the object (must be String/int/long) The @Id field must either be named id, or the annotation used in combination with a MappingConverter
4/28/12
Domain Objects
Unlike traditional JPA models, Document based models will often have a root object, and will often @Document public class Resume contain nested objects, which do not @Id need to be annotated private String id;
In our Resume example, only our top public class ResumePage level Resume object needs to be annotated and we will only be PageSection public class persisting/retrieving at this level 4/28/12
Core CRUD functionality can be achieved out-of-the-box by extending the MongoRepository class
This provides save(), findAll(), findOne() and delete() methods that can be used on our @Document object (in our case Resume.java) 4/28/12
Our Repository interface can easily be extended to include custom methods, and the MongoTemplate class provides an API to create custom queries:
mongoTemplate.find(COLLECTION_NAME, new Query(Criteria.where(name).is(searchTerm )), Resume.class);
4/28/12
Service Layer
The majority of the MongoDB implementation is abstracted to the Data Access layer through the MongoRepository There is no real change in the Service or Controller Layers to the normal Spring MVC approach
In the Service class we can just autowire the Repository Interface in to the 4/28/12 class just like we would normally
Configuration
<!-- Mongo Configuration --> <mongo:repositories basepackage="com.tmm.nosql.mongodb.repo" /> <bean id="mongoTemplate" class="org.springframework.data.document.mongodb.Mong oTemplate"> <constructor-arg ref="mongoDbFactory" /> </bean> <!-- local config --> <mongo:db-factory id="mongoDbFactory" dbname="resume_db host="localhost" port="27017"/> 3. Define the Mongo DB Factory with the host name, port, dbname (can also provide username and password if needed) 4/28/12 1. Define package location for the Repository
Deploying to CloudFoundry
Sign up for your free CloudFoundry account (currently in Beta, so it may take time for your account details) at http://www.cloudfoundry.com/
Install the latest STS Eclipse build and CloudFoundry extensions (full details here: http://blog.springsource.com/2011/04/13 4/28/12
A change to your application config is needed to support access to CloudFoundry MongoDB service
Notice the only change is the mongoDbFactory bean is being instantiated with no constructor 4/28/12
Configuring CloudFoundry
In the Servers tab, doubleclick the deployed application to open the Applications tab
In the Services section of the Applications tab, select the Add Service button, select MongoDB and name the service appropriately Once the MongoDB service has been created, click and drag it in to the Application Services tab 4/28/12
Thoughts
NoSQL is growing in popularity but is not intended to be a replacement for RDBMS NoSQL should be considered as another tool in the arsenal RDBMS is not going anywhere!
Database technology choice should be driven by the requirements of the problem Document oriented have specific applications 4/28/12
Appendix
Full source code for the application is available from http://automateddeveloper.blogspot.com Feel free to drop me an email or come by to the blog and leave a comment if you have any questions or feedback
4/28/12