Bringing spatial love to your python application

35
OPENSHIFT Workshop PRESENTED BY Shekhar Gulati Bringing Spatial Love to your Python Application

description

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

Transcript of Bringing spatial love to your python application

Page 1: Bringing spatial love to your python application

OPENSHIFTWorkshop

PRESENTEDBY

ShekharGulati

Bringing Spatial Love to your Python Application

Page 2: Bringing spatial love to your python application

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

Page 3: Bringing spatial love to your python application

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

Page 4: Bringing spatial love to your python application

Write an application and host it on internet

http://localjobsdemo-shekhargulati.rhcloud.com/

GOAL

Page 5: Bringing spatial love to your python application

CODE DU JOUR

https://github.com/shekhargulati/localjobs-python2.7

Page 6: Bringing spatial love to your python application

● You know Python● You know Git● You like to write code rather than managing

servers.

ASSUMPTIONS

Page 7: Bringing spatial love to your python application

The ToolsFlask , MongoDB , and OpenShift

Page 8: Bringing spatial love to your python application

FLASK

Page 9: Bringing spatial love to your python application

● 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

Page 10: Bringing spatial love to your python application

● 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?

Page 11: Bringing spatial love to your python application

$ 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

Page 12: Bringing spatial love to your python application

● Look under templates for templates (default jinja2)

● Look under static for assets (CSS , JS , etc.)

FLASK DEFAULTS

Page 13: Bringing spatial love to your python application

RESTFUL REQUEST DISPATCHING

Page 14: Bringing spatial love to your python application

MONGODB

Page 15: Bringing spatial love to your python application

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

Page 16: Bringing spatial love to your python application

Database → Database

Table → Collection

Row → JSON Document

Index → Index

Join → Embedding

STEP 4 : MONGODB TERMINOLOGY

Page 17: Bringing spatial love to your python application

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

Page 18: Bringing spatial love to your python application

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

Page 19: Bringing spatial love to your python application

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

Page 20: Bringing spatial love to your python application
Page 21: Bringing spatial love to your python application

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!”

Page 22: Bringing spatial love to your python application

origin

Public Cloud

Service

On-premise

or Private Cloud

Software

Open Source Project

FLAVORS OF OPENSHIFT

Page 23: Bringing spatial love to your python application

23

OUR STACK

Page 24: Bringing spatial love to your python application

24

https://openshift.redhat.com/app/account/newPromo code is PYCONAPAC

CREATING OPENSHIFT ACCOUNT

Page 25: Bringing spatial love to your python application

● Free! No time limit

● 3 gears (like servers) - each 512 Mb RAM, 1 Gb disk

● Auto-scaling

● Simple pricing

BUT WAIT – THERE's MORE

Page 26: Bringing spatial love to your python application

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

Page 27: Bringing spatial love to your python application

rhc app create localjobs python-2.7

CREATE APPLICATION

Page 28: Bringing spatial love to your python application

rhc cartridge add mongodb-2.2 --app localjobs

ADD CARTRIDGE

Page 29: Bringing spatial love to your python application

git remote add upstream -m master https://github.com/shekhargulati/localjobs-python2.7.git

git pull -s recursive -X theirs upstream master

PULL SOURCECODE

Page 30: Bringing spatial love to your python application

● 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

Page 31: Bringing spatial love to your python application

$ 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

Page 32: Bringing spatial love to your python application

git push

DEPLOY APP

Page 33: Bringing spatial love to your python application

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

Page 34: Bringing spatial love to your python application

QUESTIONS?

Page 35: Bringing spatial love to your python application

DONE!