Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Introduction to Google App Engine

5,520 views

Published on

Slides of a talk I gave on the 22nd of May for the first event of the Catania Google Technology Users Group.

The talk is about Google App Engine, and it's a very basic introduction to this platform. In the last part I present a toy example based on Google App Engine.

Published in: Technology

Introduction to Google App Engine

  1. 1. Introduction to Google App Engine Andrea Spadaccini - @lupino3 May 22nd, 2010 - Catania – Italy [email_address]
  2. 2. Agenda Overview Developing applications with Google App Engine Toy example Andrea Spadaccini - @lupino3 – Introduzione a Google App Engine Andrea Spadaccini - @lupino3 – Introduzione a Google App Engine
  3. 3. Overview
  4. 4. What is GAE? Google App Engine (GAE) is a service, offered by Google, that allows developers to build applications that can run on Google's infrastructure. It is a form of Cloud Computing. Andrea Spadaccini - @lupino3 – Introduzione a Google App Engine Andrea Spadaccini - @lupino3 – Introduzione a Google App Engine
  5. 5. Cloud computing Abstraction of details like hardware configuration, physical location of data, operating system. Internet-based computing. Cloud : metaphor inspired by the drawing used to represent the Internet (and, earlier, the PSTN) in network diagrams Andrea Spadaccini - @lupino3 – Introduzione a Google App Engine Andrea Spadaccini - @lupino3 – Introduzione a Google App Engine
  6. 6. Cloud computing Andrea Spadaccini - @lupino3 – Introduzione a Google App Engine Andrea Spadaccini - @lupino3 – Introduzione a Google App Engine Traditional client/server paradigm Client N Client 1 Server Internet
  7. 7. Cloud computing Andrea Spadaccini - @lupino3 – Introduzione a Google App Engine Andrea Spadaccini - @lupino3 – Introduzione a Google App Engine Cloud Computing Client N Client 1 Internet Server Server Server
  8. 8. Cloud layers Andrea Spadaccini - @lupino3 – Introduzione a Google App Engine Andrea Spadaccini - @lupino3 – Introduzione a Google App Engine Software as a Service Platform as a Service Infrastructure as a Service HW / Net IaaS Paas SaaS
  9. 9. Cloud layers Andrea Spadaccini - @lupino3 – Introduzione a Google App Engine Andrea Spadaccini - @lupino3 – Introduzione a Google App Engine Software as a Service Platform as a Service Infrastructure as a Service HW / Net IaaS Paas SaaS
  10. 10. GAE Features Run dynamic web applications (request / response) Serve static files Data storage (BigTable) Google infrastructure -> scalability, efficiency Additional features (mail, XMPP, Google Accounts, cron, WebServices, memcache, image manip.) Andrea Spadaccini - @lupino3 – Introduzione a Google App Engine Andrea Spadaccini - @lupino3 – Introduzione a Google App Engine
  11. 11. App Engine datastore The App Engine Datastore is a distributed efficient storage designed for web applications. Entity : data object to be stored. Contains attributes and is identified by a key . Queries must be run against indexes . It's not a relational DB. There are no joins (or aggregate queries like count(*)). The schema is enforced by the application, not by the database. Andrea Spadaccini - @lupino3 – Introduzione a Google App Engine Andrea Spadaccini - @lupino3 – Introduzione a Google App Engine
  12. 12. Developing applications with Google App Engine
  13. 13. Getting started <ul>Register an account at http://www.appspot.com You will be able to create up to 10 GAE apps (each with its http:// app-id .appspot.com URL) Download the GAE SDK (Linux, MacOSX, Win) </ul>Andrea Spadaccini - @lupino3 – Introduzione a Google App Engine Andrea Spadaccini - @lupino3 – Introduzione a Google App Engine … which SDK?
  14. 14. Which language? Andrea Spadaccini - @lupino3 – Introduzione a Google App Engine Andrea Spadaccini - @lupino3 – Introduzione a Google App Engine Frame taken from Matrix (1999)
  15. 15. Which language? Andrea Spadaccini - @lupino3 – Introduzione a Google App Engine Andrea Spadaccini - @lupino3 – Introduzione a Google App Engine
  16. 16. Which language? Andrea Spadaccini - @lupino3 – Introduzione a Google App Engine Andrea Spadaccini - @lupino3 – Introduzione a Google App Engine
  17. 17. Hello world! # helloworld.py print “Content-Type: text/html” print print “Hello world” # app.yaml application : gtugct-helloworld version : 1 runtime : python api-version : 1 handlers : - uri : /.* script : helloworld.py Andrea Spadaccini - @lupino3 – Introduzione a Google App Engine Andrea Spadaccini - @lupino3 – Introduzione a Google App Engine Taken from App Engine Intro – see References
  18. 18. The GAE dev cycle 10 Develop - <your favourite tools here> (hint: ViM) 20 Test - dev_appserver.py 30 Deploy - appcfg.py 40 Repeat - GOTO 10 Andrea Spadaccini - @lupino3 – Introduzione a Google App Engine Andrea Spadaccini - @lupino3 – Introduzione a Google App Engine With the Python SDK
  19. 19. Paradigm and features Request / response (max 30 seconds!) URL ↔ script mapping Web Frameworks: GAE comes with webapp , a Web Framework developed by Google. You can use Django (but not its ORM!). Web-based administration console Andrea Spadaccini - @lupino3 – Introduzione a Google App Engine Andrea Spadaccini - @lupino3 – Introduzione a Google App Engine
  20. 20. Toy Example
  21. 21. Answr It! Answr.it by Vanni Rizzo, Pierluigi D'Antrassi, Andrea Spadaccini - http://www.answr.it Andrea Spadaccini - @lupino3 – Introduzione a Google App Engine
  22. 22. Data modeling (I) # models.py from google.appengine.ext import db class Answr(db.Model): text = db.StringProperty() rand = db.FloatProperty() def to_json(self): return '{&quot;text&quot; : &quot;%s&quot;}' % self.text @staticmethod def get_random(): def add_answr(answr_text, rand = None): Andrea Spadaccini - @lupino3 – Introduzione a Google App Engine Andrea Spadaccini - @lupino3 – Introduzione a Google App Engine
  23. 23. Data modeling (II) # models.py (continued) class ApplicationData(db.Model): name = db.StringProperty() value = db.IntegerProperty() @staticmethod def getAnswrCounter(): def incrementAnswrCounter(): ... Andrea Spadaccini - @lupino3 – Introduzione a Google App Engine Andrea Spadaccini - @lupino3 – Introduzione a Google App Engine
  24. 24. Main page code from google.appengine.ext import webapp from google.appengine.ext.webapp.util import run_wsgi_app from models import Answr class AnswrApp(webapp.RequestHandler): def get(self): self.response.headers['Content-Type'] = 'application/json' random_answr = Answr.get_random() self.response.out.write(random_answr.to_json()) application = webapp.WSGIApplication([('/answr', AnswrApp)], debug = True) if __name__ == '__main__': run_wsgi_app(application) Andrea Spadaccini - @lupino3 – Introduzione a Google App Engine Andrea Spadaccini - @lupino3 – Introduzione a Google App Engine
  25. 25. app.yaml application: answr-it version: 10 runtime: python api_version: 1 handlers: - url: /answr script: main.py - url: / static_files: static/index.html upload: static/index.html - url: /css (same for /img e /js) static_dir: static/css Andrea Spadaccini - @lupino3 – Introduzione a Google App Engine Andrea Spadaccini - @lupino3 – Introduzione a Google App Engine
  26. 26. Users API from models import Answr from google.appengine.api import users [.. omissis ..] class PopulatePage(webapp.RequestHandler): def get(self): user = users.get_current_user() if not user: print &quot;<a href = '%s'>Log in with your Google Account</a>&quot; % users.create_login_url(self.request.path) elif users.is_current_user_admin(): print &quot;Populating the DB...&quot; answrs = [ .. omissis .. ] for answr_text in answrs: Answr.add_answr(answr_text) Answr.add_answr(&quot;Something strange is going on..&quot;, 1.0) else : logging.warning('Unauthorized access to /populate') Andrea Spadaccini - @lupino3 – Introduzione a Google App Engine Andrea Spadaccini - @lupino3 – Introduzione a Google App Engine
  27. 27. Live demo http://answr-it.appspot.com .. or .. http://www.answr.it Andrea Spadaccini - @lupino3 – Introduzione a Google App Engine Andrea Spadaccini - @lupino3 – Introduzione a Google App Engine
  28. 28. Twitter bot (cron + WS) # twitterbot.py import tweepy from models import ApplicationData, Answr # 1. Login auth = tweepy.BasicAuthHandler(&quot;answrit&quot;, &quot;the_password&quot;) api = tweepy.API(auth) logging.info('Login done!') # 2. Follow back who follows me followers = api.followers() friends = api.friends() to_follow = [x for x in followers if x not in friends] for user in to_follow: try : user.follow() except tweepy.TweepError, e: logging.warning(e) Andrea Spadaccini - @lupino3 – Introduzione a Google App Engine Andrea Spadaccini - @lupino3 – Introduzione a Google App Engine
  29. 29. Twitter bot (cron + WS) # twitterbot.py (cont.) # 3. Read last mentions last_id = ApplicationData.getLastAnswredTweetId() if not last_id: mentions = api.mentions(count = 200) else : mentions = api.mentions(count = 200, since_id = last_id) # 4. Random answr for status in mentions: answr = Answr.get_random() api.update_status(&quot;@%s %s&quot; % (status.author.screen_name, answr.text), status.id) if status.id > last_id: last_id = status.id # 5. Save the last ID if last_id: ApplicationData.setLastAnswredTweetId(last_id) Andrea Spadaccini - @lupino3 – Introduzione a Google App Engine Andrea Spadaccini - @lupino3 – Introduzione a Google App Engine
  30. 30. Twitter bot (cron + WS) # cron.yaml cron: - description: answr to people url: /twitterbot schedule: every 1 mins # (part of) app.yaml - url: /twitterbot login: admin script: twitterbot.py Andrea Spadaccini - @lupino3 – Introduzione a Google App Engine Andrea Spadaccini - @lupino3 – Introduzione a Google App Engine DEMO! (if Murphy allows it..) Tweet something to @answrit (like: @answrit: are you ok?)
  31. 31. Chat bot (XMPP) # (part of) xmpp.py from google.appengine.api import xmpp from google.appengine.ext import webapp from google.appengine.ext.webapp.util import run_wsgi_app from models import Answr class XMPPHandler(webapp.RequestHandler): def post(self): message = xmpp.Message(self.request.POST) message.reply(Answr.get_random().text) application = webapp.WSGIApplication([('/_ah/xmpp/message/chat/', XMPPHandler)], debug=True) Andrea Spadaccini - @lupino3 – Introduzione a Google App Engine Andrea Spadaccini - @lupino3 – Introduzione a Google App Engine
  32. 32. Chat bot (XMPP) # (significant part of) app.yaml - url: /_ah/xmpp/message/chat/ script: xmpp.py inbound_services: - xmpp_message Andrea Spadaccini - @lupino3 – Introduzione a Google App Engine Andrea Spadaccini - @lupino3 – Introduzione a Google App Engine DEMO! Add to your XMPP buddy list [email_address] (GTalk is fine), and ask him questions!
  33. 33. References http://en.wikipedia.org - Articles on Cloud Computing, Affero GPL and others Google Inc – aa.vv. - “Bigtable: a distributed storage system for structured data” - OSDI 2006 Dan Sanderson - “Programming Google App Engine” - O' Reilly / Google Press – 2009 Google App Engine home page - http://www.appspot.com Simon Willison - “I've (probably) been using Google App Engine for a week longer than you have” - Slides from Barcamp London 4 – 2008” (referred in this document as App Engine Intro ) Andrea Spadaccini - @lupino3 – Introduzione a Google App Engine Andrea Spadaccini - @lupino3 – Introduzione a Google App Engine
  34. 34. More stuff
  35. 35. No vendor lock-in! Open source implementation of GAE: AppScale http://appscale.cs.ucsb.edu/ Runs on: <ul><li>Amazon EC2
  36. 36. XEN (?)
  37. 37. KVM </li></ul>Andrea Spadaccini - @lupino3 – Introduzione a Google App Engine Andrea Spadaccini - @lupino3 – Introduzione a Google App Engine
  38. 38. Cloud Freedom - AGPL Software licenses like the General Public License state rights of those who get the program, not of those who use the program via a network The GNU Affero General Public License (AGPL) require the availability of source code when the licensed code is deployed as a network service. http://www.gnu.org/licenses/agpl-3.0.html Andrea Spadaccini - @lupino3 – Introduzione a Google App Engine Andrea Spadaccini - @lupino3 – Introduzione a Google App Engine
  39. 39. Python vs. Java Image taken from stacktrace.it: http://stacktrace.it/2009/05/google-app-engine/ Andrea Spadaccini - @lupino3 – Introduzione a Google App Engine

×