Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Polyglot Persistence Spring Applications on Ope...

Polyglot Persistence Spring Applications on OpenShift

Red Hat OpenShift is a multi-language, multi-framework, multi-cartridge Platform-as-a-Service (PaaS) solution. This rich platform gives the developer several choices in terms of languages (e.g., Java, PHP, Python, Ruby, etc.), frameworks (e.g., Spring, JavaEE, etc.), and datastores (e.g., MySQL, PostgreSQL, and MongoDB). Support for multiple datastores enables you to choose the right tool for the specific problem and embraces Polyglot persistence. In this session, Shekhar Gulati will cover:
What Polyglot Persistence is and why it is important today
What OpenShift is and how you can interact with the OpenShift rhc command line tool to:
Create an application
Enable Jenkins support
Add MongoDB and MySQL cartridges
Walk through a Spring MongoDB MySQL web application
Push the code to cloud
Bring your laptops as we'll create and push applications to the cloud.

Shekhar Gulati

November 01, 2012
Tweet

More Decks by Shekhar Gulati

Other Decks in Programming

Transcript

  1. Shekhar Gulati 3 Now we have choices ... lots of

    them http://nosql-database.org/ list 122+ databases
  2. Shekhar Gulati 4 Why should I care when relational databases

    have met my needs? • Widely used and understood • Tested in real environments • Efficient use of storage space if data normalized properly • Great tools support • ACID semantics • Incredibly flexible and powerful query language • Great framework support
  3. Shekhar Gulati 5 Relational Databases have limitations • Complex object

    graphs does not map very well with flat tables • Difficult to evolve Schema with time • Data constraints and JOINs can be expensive at runtime • Difficult and EXPENSIVE to scale horizontally
  4. Shekhar Gulati 7 NoSQL Database to the rescue • Schema-less

    • Rich documents • Eventual consistency • Fast writes • Easy to scale horizontally to add processing power and storage
  5. Shekhar Gulati 8 Polyglot Persistence Using multiple data storage technologies,

    chosen based upon the way data is being used by individual applications or components of single application. Martin Fowler http://martinfowler.com/articles/nosql-intro.pdf
  6. Shekhar Gulati 12 User Stories • As a User, I

    should be able to find all the jobs. • As a User, I should be able to find all the jobs near to my location. • As a User, I should be able to find all MongoDB (or any skill) jobs near to my location. • As a User, I should be able to find all the MongoDB (or any other skill) jobs near to my location with distance.
  7. Shekhar Gulati 13 Technology Choices • OpenShift • MongoDB •

    PostgreSQL • Java • Spring Framework • Spring Data MongoDB • Spring Social • Spring Data JPA • Git
  8. Shekhar Gulati 14 What is MongoDB? • Document Oriented database

    • Schema-less • Each document is heterogeneous, and may have completely unique structure compared to other documents. • Fast and horizontally scalable • Rich query language
  9. Shekhar Gulati 15 MongoDB Terminology Database → Database Table →

    Collection Row → Document Index → Index
  10. Shekhar Gulati 16 Why MongoDB? • Easy to get running

    • Open Source • Active community • Rich documents • Geospatial indexing. • Writes are very fast. You can customize it using WriteConcern.
  11. Shekhar Gulati 18 Geospatial Indexing Basics • What is it

    for? • Find all the MongoDB jobs near me • Find all the MongoDB jobs within London • Supports only two dimensional indexes. • You can only have one geospatial index per collection. • The spatial functionality MongoDB currently has is: • Near • Containment http://www.mongodb.org/display/DOCS/Geospatial+ Indexing
  12. Shekhar Gulati 19 How to make it work? • Put

    your coordinates into an array { loc : [ 50 , 30 ] } //SUGGESTED OPTION { loc : { x : 50 , y : 30 } } { loc : { foo : 50 , y : 30 } } { loc : { lon : 40.739037, lat: 73.992964 } } • Make a 2d index db.places.ensureIndex( { loc : "2d" } )
  13. Shekhar Gulati 20 What is OpenShift? Red Hat’s free platform

    as a service for applications in the cloud.
  14. Shekhar Gulati 22 Why OpenShift? • Supports MongoDB and PostgreSQL.

    Also supports MySQL. • Multi-language support. Supports Java, Node.js, Perl, Python, PHP and Ruby. • No need to learn anything new. • Scalable. • FREE!
  15. Shekhar Gulati 23 Developers Choose How To Work with OpenShift

    Developer IDE Integrations Web Browser Console Command Line Tooling REST APIs
  16. Shekhar Gulati 25 What do I get? • OpenShift is

    free-as-in-beer & free-as-in-freedom • You get three free gears, each with 512MB memory and 1GB of disk space. • Need more resources, just ask! • The catch is we are in developer preview right now
  17. Shekhar Gulati 29 Adding MongoDB and PostgreSQL support rhc app

    cartridge add -a localjobs -c mongodb-2.0 rhc app cartridge add -a localjobs -c postgresql-8.4
  18. Shekhar Gulati 30 Play with MongoDB running in the cloud

    • ssh into instance • Type mongo on the shell • Create a sample db • Insert some documents in the collection • Run some queries
  19. Shekhar Gulati 31 Pulling code from github git rm -rf

    src pom.xml git commit -am “removed default files” git remote add localjobs -m master git://github.com/shekhargulati/localjobs-developer-day.git git pull -s recursive -X theirs localjobs master
  20. Shekhar Gulati 32 Importing data into MongoDB rhc app show

    -a localjobs -l <openshift_login_email> scp -r jobs-data.json <instance_ssh_access>:localjobs/data ssh <instance_ssh_access> mongoimport -d localjobs -c jobs --file jobs-data.json -u $OPENSHIFT_NOSQL_DB_USERNAME -p $OPENSHIFT_NOSQL_DB_PASSWORD -h $OPENSHIFT_NOSQL_DB_HOST -port $OPENSHIFT_NOSQL_DB_PORT db.jobs.ensureIndex({"location":"2d"})
  21. Shekhar Gulati 35 Conclusion • OpenShift is very easy to

    use and you can build polyglot persistence applications on it. • MongoDB makes it very easy to build location aware applications. • All the Spring latest projects work without any problem on OpenShift. • Application code is on github https://github.com/shekhargulati/localjobs-developer-day • Did I mention – Free? • What are you waiting for? Try it out.