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

MongoDB Bangalore Hackathon

MongoDB Bangalore Hackathon

MongoDB Bangalore Hackathon

Shekhar Gulati

March 02, 2013
Tweet

More Decks by Shekhar Gulati

Other Decks in Technology

Transcript

  1. 1 Building Location Aware Applications using MongoDB and OpenShift Shekhar

    Gulati @shekhargulati OpenShift Evangelist, Red Hat
  2. 2 About Me ~ Shekhar Gulati ➢ OpenShift Developer Evangelist

    at Red Hat ➢ Developer, Speaker, Blogger ➢ Twitter Handle : shekhargulati
  3. 3 Cloud Service Models STORAGE (RHS) HARDWARE (x86, Power, S/390)

    VIRTUALIZATION (RHEV) OPERATING SYSTEM (RHEL) APPLICATION PLATFORM (JBOSS, PHP, RUBY, ETC) APPLICATION Automated and Managed by the Public or Private Cloud Offering Managed and Controlled by Customer (IT, Dev, or User) IaaS PaaS SaaS Increased Control Increased Automation
  4. 5 PaaS = Platform as a Service A Cloud Application

    Platform Code Deploy Enjoy Save Time and Money Code your app Push-button Deploy, and your App is running in the Cloud!
  5. 6

  6. 7 Streamlining App Dev with PaaS With PaaS How to

    Build an App: 1. Have Idea 2. Get Budget 3. Code 4. Test 5. Launch 6. Automatically Scale How to Build an App: 1. Have Idea 2. Get Budget 3. Submit VM Request request 4. Wait 5. Deploy framework/appserver 6. Deploy testing tools 7. Test testing tools 8. Code 9. Configure Prod VMs 10. Push to Prod 11. Launch 12. Request More Prod VMs to meet demand 13. Wait 14. Deploy app to new VMs 15. Etc. Today How to Build an App: 1. Have Idea 2. Get Budget 3. Submit hardware acquisition request 4. Wait 5. Get Hardware 6. Rack and Stack Hardware 7. Install Operating System 8. Install Operating System Patches/Fix-Packs 9. Create user Accounts 10. Deploy framework/appserver 11. Deploy testing tools 12. Test testing tools 13. Code 14. Configure Prod servers (and buy them if needed) 15. Push to Prod 16. Launch 17. Order more servers to meet demand 18. Wait… 19. Deploy new servers 20. Etc. Yesterday “The use of Platform-as-a-Service technologies will enable IT organizations to become more agile and more responsive to the business needs.” –Gartner*
  7. 10 Introducing OpenShift  A free auto-scaling, cloud-based application platform

    from Red Hat.  Multi Language, Multi Frameworks, Multi Cartridges.  No proprietary software.  No Lock-in.  Open source – OpenShift Origin.
  8. 12 Why OpenShift? • Strength. OpenShift is built on proven

    Red Hat technologies. • Freedom. In OpenShift, work the way you want. • Choice of Interface: Web Console, Command-line, or IDE • Choice of Middleware: Java(EE6), Ruby, Node.js, PHP, Python, and Perl • Choice of Cloud: Public, Private, or Hybrid Cloud • Choice of Elasticity: Automatic application scaling when needed • Openness. OpenShift’s open source software stack ensures application portability and No Lock-In.
  9. 13 Developers Choose How To Work with OpenShift Developer IDE

    Integrations Web Browser Console Command Line Tooling REST APIs
  10. 15 What else do I get and what is the

    catch?  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
  11. 16 Some terminology for today 1. Application – your web

    code and any data store. Has to be on 1 or more gears 2. Gear – is like a server. It can have only 1 language for the web programming. 3. Cartridge – it adds a language, a data store, or other functionality 4. Git – used for version control and managing code between server and your development machine 5. Ssh – command line tool to connect to your gear
  12. 19 Creating Namespace ➢ Login to OpenShift web console ➢

    Click on “My Account” ➢ Namespace – mongomadness – Unique per user
  13. 22 Installing Client Tools Install Ruby 1.8.7 or greater Install

    Git Install rhc OpenShift gem Refer to documentation
  14. 23 Setup your OpenShift Environment rhc setup -l <openshift_login> ->

    ask for namespace -> ask to upload ssh keys
  15. 26 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.
  16. 27 Technology Choices  MongoDB 2.2  OpenShift  Java

    6 – Spring Framework – Spring MongoDB  GIT  SSH
  17. 28 Why MongoDB ?  Easy to get running 

    Open Source  Active community  Rich documents  Geospatial indexing  Writes are very fast. You can customize it using WriteConcern
  18. 30 Geospatial Indexing Basics  What is it for? 

    Find all the MongoDB jobs near me – Proximity Queries  Find all the MongoDB jobs within Bangalore – Bounded Queries  Find all the MongoDB job at this location – Exact Queries • Supports only two dimensional indexes.  You can only have one geospatial index per collection.  By default, 2d geospatial indexes assume longitude and latitude have boundaries of -180 inclusive and 180 non-inclusive (i.e. [-180, 180))
  19. 31 How to make it work 1) Put your coordinates

    into an array { loc : [ 50 , 30 ] } //SUGGESTED OPTION { loc : { x : 50 , y : 30 } } { loc : { foo : 50 , y : 30 } } 1) { loc : { lon : 40.739037, lat: 73.992964 } } 2) Make a 2d index db.places.ensureIndex( { loc : "2d" } ) 3) If you use latitude and longitude as your coordinate system, always store longitude first. MongoDB’s 2d spherical index operators only recognize [ longitude, latitude] ordering.
  20. 32 GeoHashes • When you create the index, MongoDB converts

    location data to binary geohash values and calculates these values using the location data and the index’s location range • Geohashes have a precision that is determined by the number of bits in the hash. Default value 26 • More bits allow the index to provide results with greater precision • You can configure number of bits at index creation db.collection.ensureIndex( {<location field>: "2d"} , { bits: <bit precision> } )
  21. 33 Compound GeoSpatial Indexes • MongoDB supports compound geospatial indexes.

    • the field with location data is the first field db.jobs.ensureIndex( { location: "2d", skills: 1 } )
  22. 34 Let's take a look at GIT Distributed Version control

    A local repository – on your laptop A remote repository – on some other machine, usually a server
  23. 35 You need to understand at least 3 commands in

    Git 1. Git add . (means add all new files as being tracked in the local repository) 2. Git commit –am “your message” (means commit all my changes to the local repository with this message) 3. Git push (means push from your local repository to the repository on your OpenShift gear)
  24. 36 Cloning OpenShift Application ➢ View application details rhc app

    show -a localjobs ➢ Clone the git repository git clone ssh://5131704b500446f181000022@localjobs- mongomadness.rhcloud.com/~/git/localjobs.git/
  25. 38 Application Deployments Options OpenShift supports two types of deployment

    options 1) Source code deployment 2) Binary deployment – war,ear
  26. 39 Other Application Commands App Details – rhc app show

    -a localjobs Delete App – rhc app delete -a localjobs App Status – rhc app show --state -a localjobs Start App – rhc app start -a localjobs Stop App – rhc app stop -a localjobs Restart App – rhc app restart -a localjobs Clean App – rhc app tidy -a localjobs
  27. 40 Adding MongoDB and RockMongo Cartridge rhc cartridge add mongodb-2.2

    -a localjobs rhc cartridge add rockmongo -a localjobs
  28. 41 Other Cartridge Commands List all Cartridges : rhc cartridge

    list Cartridge Details : rhc cartridge show -a app -c mongodb-2.2 Remove Cartridge : rhc cartridge remove -a app -c mongodb-2.2 Start Cartridge : rhc cartridge start -a app -c mongodb-2.2 Stop Cartridge : rhc cartridge stop -a app -c mongodb-2.2 Restart Cartridge : rhc cartridge restart -a app -c mongodb-2.2 Cartridge Status : rhc cartridge status -a app -c mongodb-2.2
  29. 42 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
  30. 43 Pulling the code from GitHub git rm -rf src

    pom.xml git commit -am “removed default files” git remote add upstream -m master git://github.com/shekhargulati/localjobs.git git pull -s recursive -X theirs upstream master
  31. 45 Importing Data into MongoDB rhc app show localjobs scp

    jobs-data.json <instance_ssh_access>:app-root/data rhc app ssh -a localjobs mongoimport -d localjobs -c jobs --file jobs-data.json -u $OPENSHIFT_MONGODB_DB_USERNAME -p $OPENSHIFT_MONGODB_DB_PASSWORD -h $OPENSHIFT_MONGODB_DB_HOST -port $OPENSHIFT_MONGODB_DB_PORT db.jobs.ensureIndex({"location":"2d"})
  32. 49 1. OpenShift makes life great for you 2. The

    tools are easy to use 3. MongoDB makes writing location aware apps very easy 4. Did I mention – Free 5. Source code https://github.com/shekhargulati/localjobs Conclusion
  33. 50 Resources Sign up with promo code MONGO.HACKATHON at https://openshift.redhat.com/app/account/new

    OpenShift Blogs https://openshift.redhat.com/community/blogs Follow @OpenShift on twitter https://twitter.com/openshift Get your app in OpenShift Application Gallery https://openshift.redhat.com/community/developer-spotlight