Slide 1

Slide 1 text

1 Building Location Aware Applications using MongoDB and OpenShift Shekhar Gulati @shekhargulati OpenShift Evangelist, Red Hat

Slide 2

Slide 2 text

2 About Me ~ Shekhar Gulati ➢ OpenShift Developer Evangelist at Red Hat ➢ Developer, Speaker, Blogger ➢ Twitter Handle : shekhargulati

Slide 3

Slide 3 text

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

Slide 4

Slide 4 text

4 What is PaaS?

Slide 5

Slide 5 text

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!

Slide 6

Slide 6 text

6

Slide 7

Slide 7 text

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*

Slide 8

Slide 8 text

8 Why choose PaaS? Lowers Cost Improves Developer Efficiency Scales

Slide 9

Slide 9 text

9 OpenShift is PaaS by Red Hat Multi-language, Auto-Scaling, Self-service, Elastic, Cloud Application Platform

Slide 10

Slide 10 text

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.

Slide 11

Slide 11 text

11 OpenShift Flavors

Slide 12

Slide 12 text

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.

Slide 13

Slide 13 text

13 Developers Choose How To Work with OpenShift Developer IDE Integrations Web Browser Console Command Line Tooling REST APIs

Slide 14

Slide 14 text

14 Developers Choose Languages, Frameworks and Middleware

Slide 15

Slide 15 text

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

Slide 16

Slide 16 text

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

Slide 17

Slide 17 text

17 Let's get our hands dirty

Slide 18

Slide 18 text

18 Create OpenShift Account https://openshift.redhat.com/app/account/new Promo code is MONGODB.HACKATHON

Slide 19

Slide 19 text

19 Creating Namespace ➢ Login to OpenShift web console ➢ Click on “My Account” ➢ Namespace – mongomadness – Unique per user

Slide 20

Slide 20 text

20 Creating Tomcat 7 Application from Web Console

Slide 21

Slide 21 text

21 https://localjobs-mongomadness.rhcloud.com/

Slide 22

Slide 22 text

22 Installing Client Tools Install Ruby 1.8.7 or greater Install Git Install rhc OpenShift gem Refer to documentation

Slide 23

Slide 23 text

23 Setup your OpenShift Environment rhc setup -l -> ask for namespace -> ask to upload ssh keys

Slide 24

Slide 24 text

24 Let's see it in action

Slide 25

Slide 25 text

25 Building Location Aware Job Search App

Slide 26

Slide 26 text

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.

Slide 27

Slide 27 text

27 Technology Choices  MongoDB 2.2  OpenShift  Java 6 – Spring Framework – Spring MongoDB  GIT  SSH

Slide 28

Slide 28 text

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

Slide 29

Slide 29 text

29 Rich Document

Slide 30

Slide 30 text

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))

Slide 31

Slide 31 text

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.

Slide 32

Slide 32 text

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( {: "2d"} , { bits: } )

Slide 33

Slide 33 text

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 } )

Slide 34

Slide 34 text

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

Slide 35

Slide 35 text

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)

Slide 36

Slide 36 text

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/

Slide 37

Slide 37 text

37 Look at the generated code

Slide 38

Slide 38 text

38 Application Deployments Options OpenShift supports two types of deployment options 1) Source code deployment 2) Binary deployment – war,ear

Slide 39

Slide 39 text

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

Slide 40

Slide 40 text

40 Adding MongoDB and RockMongo Cartridge rhc cartridge add mongodb-2.2 -a localjobs rhc cartridge add rockmongo -a localjobs

Slide 41

Slide 41 text

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

Slide 42

Slide 42 text

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

Slide 43

Slide 43 text

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

Slide 44

Slide 44 text

44 Let's look at jobs-data.json

Slide 45

Slide 45 text

45 Importing Data into MongoDB rhc app show localjobs scp jobs-data.json :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"})

Slide 46

Slide 46 text

46 Show some geospatial queries

Slide 47

Slide 47 text

47 Code Walkthrough

Slide 48

Slide 48 text

48 Deploy the code to OpenShift git push

Slide 49

Slide 49 text

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

Slide 50

Slide 50 text

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