Bringing spatial love to your python application

  • 1,660 views
Uploaded on

Bringing spatial love to your python application with MongoDB and deploy it to OpenShift

Bringing spatial love to your python application with MongoDB and deploy it to OpenShift

More in: Technology
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Be the first to comment
No Downloads

Views

Total Views
1,660
On Slideshare
0
From Embeds
0
Number of Embeds
1

Actions

Shares
Downloads
12
Comments
0
Likes
5

Embeds 0

No embeds

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
    No notes for slide

Transcript

  • 1. OPENSHIFT Workshop PRESENTED BY Shekhar Gulati Bringing Spatial Love to your Python Application
  • 2. WHO AM I • Shekhar Gulati – Works at Red Hat • Principal OpenShift Developer Evangelist • Java / Python/ JavaScript / NoSQL / Cloud Guy • Twitter Handle : shekhargulati • Github https://github.com/shekhargulati • Slides http://www.slideshare.net/shekhargulati
  • 3. AGENDA ➔ Learn a bit about Flask ➔ Learn some MongoDB basics ➔ Build a location aware Job search application ➔ Go live with a Flask MongoDB powered app in ~ 50 mins
  • 4. Write an application and host it on internet http://localjobsdemo-shekhargulati.rhcloud.com/ GOAL
  • 5. CODE DU JOUR https://github.com/shekhargulati/localjobs-python2.7
  • 6. ● You know Python ● You know Git ● You like to write code rather than managing servers. ASSUMPTIONS
  • 7. The Tools Flask , MongoDB , and OpenShift
  • 8. FLASK
  • 9. ● Flask is a microframework for web application development in Python ● Microframework refers to simplicity and small size of framework ● It is inspired by Sinatra(Ruby framework to create web apps with minimum fuss) ● Built on Werkzeug , Jinja2 , WSGI ● Good sensible defaults FLASK
  • 10. ● Easy to learn – dead simple ● Simple but extensible ● Excellent documentation spread over 280 pages with lots of examples ● Very active community ● It has minimal footprint ● Does not make choice for you ● Unit testing support WHY FLASK?
  • 11. $ mkdir flask-demo $ cd flask-demo $ virtualenv venv --python=python2.7 $ . venv/bin/activate $ pip install flask $ touch app.py $ python app.py FLASK : GETTING STARTED
  • 12. ● Look under templates for templates (default jinja2) ● Look under static for assets (CSS , JS , etc.) FLASK DEFAULTS
  • 13. RESTFUL REQUEST DISPATCHING
  • 14. MONGODB
  • 15. 15  Open Source NoSQL document datastore – JSON style documents  Schema-less – Each document is heterogeneous, and may have completely unique structure compared to other documents  Fast and horizontally scalable  Rich query language  Rich documents  Easy to get running  Geospatial indexing WHAT IS MONGODB
  • 16. Database → Database Table → Collection Row → JSON Document Index → Index Join → Embedding STEP 4 : MONGODB TERMINOLOGY
  • 17. 17  What is it for?  Find all the MongoDB jobs near me – Proximity Queries  Find all the MongoDB jobs within Tokyo – 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)) GEOSPATIAL INDEXING BASICS
  • 18. 18 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.jobs.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. HOW TO MAKE IT WORK
  • 19. 19 // Find all the jobs with skill as mongodb db.jobs.find({"skills":"mongodb"}) // Find all the jobs with python as skill and near to given location db.jobs.find({"lngLat":{$near : [139.69 , 35.68]}, "skills":"python"}) // Find all the python or mongodb jobs near to given location db.jobs.find({"lngLat":{$near : [139.69 , 35.68]}, "skills":{$in : ["mongodb","python"]}}) SOME QUERIES
  • 20. Platform as a Service delivers • Application run-time environment in the cloud • Configures & manages both the cloud & stack for your application “The cloud is now useful!”
  • 21. origin Public Cloud Service On- premise or Private Cloud Software Open Source Project FLAVORS OF OPENSHIFT
  • 22. 23 OUR STACK
  • 23. 24 https://openshift.redhat.com/app/account/new Promo code is PYCONAPAC CREATING OPENSHIFT ACCOUNT
  • 24. ● Free! No time limit ● 3 gears (like servers) - each 512 Mb RAM, 1 Gb disk ● Auto-scaling ● Simple pricing BUT WAIT – THERE's MORE
  • 25. 1) Create Python 2.7 OpenShift Application 2) Add MongoDB database 3) Pull source code from github 4) Import data into MongoDB 5) Push the changes to Application DEMO
  • 26. rhc app create localjobs python-2.7 CREATE APPLICATION
  • 27. rhc cartridge add mongodb-2.2 --app localjobs ADD CARTRIDGE
  • 28. git remote add upstream -m master https://github.com/shekhargulati/localjobs-python2.7.git git pull -s recursive -X theirs upstream master PULL SOURCECODE
  • 29. ● Step 1 : Application Setup , User Registration , User login – git checkout 005ce14038f489b16249a28be84c035cad99b2b8 ● Step 2 : Create New Job – git checkout e96e5d19f28ae8aaa33c1cce283daef98c4ff124 ● Step 3 : Search with Near – git checkout 06a388f003fbfec51c6cee1763bae3cf96b58185 ● Step 4 : Search with GeoNear – git checkout 653e830dfecf5e7df31016be14e89d5daadb84ee ● Step 5 : OpenShift Application – git checkout master CODE WALKTHROUGH
  • 30. $ rhc show-app $ scp jobs-data.json <ssh url>:app-root/data $ rhc ssh $ cd app-root/data $ 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 IMPORT DATA INTO MONGODB
  • 31. git push DEPLOY APP
  • 32. rhc create-app localjobs python-2.7 mongodb-2.2 --enable-jenkins -s --from-code https://github.com/shekhargulati/localjobs-python2.7.git LOT MORE GOODIES AutoScaling + Jenkins
  • 33. QUESTIONS?
  • 34. DONE!