Introduction To Google App Engine

66
はじめての Google App Engine Kosei Moriyama (cou929) Nov, 23, 2009 Wednesday, May 5, 2010

description

Google App Engine の概要についてまとめたものです. 私的勉強会で使用.

Transcript of Introduction To Google App Engine

Page 1: Introduction To Google App Engine

はじめてのGoogle App Engine

Kosei Moriyama (cou929)Nov, 23, 2009

Wednesday, May 5, 2010

Page 2: Introduction To Google App Engine

Agenda

• Google App Engine とは

• Getting Started

• その他の機能

Wednesday, May 5, 2010

Page 3: Introduction To Google App Engine

Google App Engineとは

Wednesday, May 5, 2010

Page 4: Introduction To Google App Engine

楽に無料で

Webアプリケーションが作れる

Wednesday, May 5, 2010

Page 5: Introduction To Google App Engine

従来• ハードウエアの準備・設定

• 自宅サーバ

• レンタル

• ミドルウエアの準備・設定

• サーバ

• データベース

• アプリケーションの開発

• 運用コスト・スケーリング・メンテナンス・騒音・トラブル…

Wednesday, May 5, 2010

Page 6: Introduction To Google App Engine

App Engine• ハードウエアの準備・設定

• 自宅サーバ

• レンタル

• ミドルウエアの準備・設定

• サーバ

• データベース

• アプリケーションの開発

• 運用コスト・スケーリング・メンテナンス・騒音・トラブル…

抽象化

自動化無料化

Wednesday, May 5, 2010

Page 7: Introduction To Google App Engine

できること• Pythonランタイム環境

• Javaランタイム環境

• JVM上で実装されたインタプリタ・コンパイラを使えば,他の言語も使用可

• e.g., JRuby

• サンドボックス

• データストア

• App Engine サービス

• Googleアカウントでのユーザ認証, URL Fetch, Mail, memcache, 画像

• Cron

Wednesday, May 5, 2010

Page 8: Introduction To Google App Engine

開発の流れ• App Engine SDK でローカル開発

• webサーバ付属

• app engineの全てのサービスをエミュレート可能

• Python SDK / Java SDK

• google のサーバへアップロード

Wednesday, May 5, 2010

Page 9: Introduction To Google App Engine

Getting Started

Wednesday, May 5, 2010

Page 10: Introduction To Google App Engine

簡易ゲストブック• スタートガイド

• http://code.google.com/intl/ja/appengine/docs/python/gettingstarted/

Wednesday, May 5, 2010

Page 11: Introduction To Google App Engine

• 開発環境

• Hello World!

• Webapp フレームワーク

• Googleアカウントでのユーザー認証

• フォーム処理

• データストア

• テンプレート

• 静的ファイル

• デプロイ

Wednesday, May 5, 2010

Page 12: Introduction To Google App Engine

• 開発環境

• Hello World!

• Webapp フレームワーク

• Googleアカウントでのユーザー認証

• フォーム処理

• データストア

• テンプレート

• 静的ファイル

• デプロイ

Wednesday, May 5, 2010

Page 13: Introduction To Google App Engine

開発環境• Pythonのインストール

• http://www.python.org/download/

• 2.5以降

• 3.xは動かないので注意

• Python SDK のインストール

• http://code.google.com/intl/ja/appengine/downloads.html

• Pathを通す

Wednesday, May 5, 2010

Page 14: Introduction To Google App Engine

開発環境

• dev_appserver.py

• 開発用サーバ

• appcfg.py

• app engineにアプリケーションをアップロードするスクリプト

Wednesday, May 5, 2010

Page 15: Introduction To Google App Engine

• 開発環境

• Hello World!

• Webapp フレームワーク

• Googleアカウントでのユーザー認証

• フォーム処理

• データストア

• テンプレート

• 静的ファイル

• デプロイ

Wednesday, May 5, 2010

Page 16: Introduction To Google App Engine

Hello World!

• アプリケーションの構成

• 設定ファイル app.yaml

• pythonスクリプト main.py

Wednesday, May 5, 2010

Page 17: Introduction To Google App Engine

app.yaml

• アプリケーションの情報

• id, version, runtime ...

• ハンドラ

• url と スクリプトをマッピング

Wednesday, May 5, 2010

Page 18: Introduction To Google App Engine

main.py

• print してるだけ

Wednesday, May 5, 2010

Page 19: Introduction To Google App Engine

App Engine webサーバ

• ファイルを書き換えると自動リスタート

Wednesday, May 5, 2010

Page 20: Introduction To Google App Engine

App Engine webサーバ

• app engine のwebサーバとスクリプトはCGI標準に従って通信

app engineweb server

app.yaml

foo.py bar.py baz.py

CGIrequest

response CGI

routingclient

Wednesday, May 5, 2010

Page 21: Introduction To Google App Engine

• 開発環境

• Hello World!

• Webapp フレームワーク

• Googleアカウントでのユーザー認証

• フォーム処理

• データストア

• テンプレート

• 静的ファイル

• デプロイ

Wednesday, May 5, 2010

Page 22: Introduction To Google App Engine

Webapp フレームワーク• CGIを手で扱うのは面倒

‣フレームワーク, ライブラリ

• Pythonのみで書かれ,CGIを使用するものならなんでも使用可

• Django, Cherry.py, Pylons, web.py

Wednesday, May 5, 2010

Page 23: Introduction To Google App Engine

Webapp

• デフォルトでインストール済

• シンプル

• app engine 独自

Wednesday, May 5, 2010

Page 24: Introduction To Google App Engine

3つのパーツ• RequestHandler クラス (複数)

• リクエストを処理, レスポンスを作成

• WSGIApplication インスタンス

• 着信したリクエストを、URL と関連付けられたハンドラへルーティング

• main ルーチン

• CGI アダプタを使用して WSGIApplication を実行

Wednesday, May 5, 2010

Page 25: Introduction To Google App Engine

main.py• webapp, run_wsgi_app モジュールをインポート

• MainPage クラス

• webapp.RequestHandlerを継承

• def get(self)

• GET リクエストがあると呼ばれる

• response.headers()

• response.out.write()

• webapp.WSGIApplication()

• run_wsgi_app()

Wednesday, May 5, 2010

Page 26: Introduction To Google App Engine

• 開発環境

• Hello World!

• Webapp フレームワーク

• Googleアカウントでのユーザー認証

• フォーム処理

• データストア

• テンプレート

• 静的ファイル

• デプロイ

Wednesday, May 5, 2010

Page 27: Introduction To Google App Engine

Google アカウントでのユーザ認証

• users を import

• user.get_current_user()

• ログインしてたらuserオブジェクト

• してなかったらnone

• self.redirect(dest_uri)

• dest_uriへリダイレクト

• users.create_login_url(dest_uri)

• ログイン画面を生成

• ログインが完了したら, dest_uriへリダイレクト

Wednesday, May 5, 2010

Page 28: Introduction To Google App Engine

• 開発環境

• Hello World!

• Webapp フレームワーク

• Googleアカウントでのユーザー認証

• フォーム処理

• データストア

• テンプレート

• 静的ファイル

• デプロイ

Wednesday, May 5, 2010

Page 29: Introduction To Google App Engine

フォーム処理

• def post(self)

• POST リクエストがあると呼ばれる

• request.get(‘name’)

• “name” フォームデータを取得

Wednesday, May 5, 2010

Page 30: Introduction To Google App Engine

• 開発環境

• Hello World!

• Webapp フレームワーク

• Googleアカウントでのユーザー認証

• フォーム処理

• データストア

• テンプレート

• 静的ファイル

• デプロイ

Wednesday, May 5, 2010

Page 31: Introduction To Google App Engine

データストア

• すべてのデータはデータストアに格納

• 自動でスケール

• 分散型

• not RDBMS

Wednesday, May 5, 2010

Page 32: Introduction To Google App Engine

データストア

Data Storeapplication

Entityput()

List of EntityGQL

Wednesday, May 5, 2010

Page 33: Introduction To Google App Engine

Entity• Entity

• db.Model を継承したクラス

• 複数のPropertyとkeyを持つ

• Property

• db.fooProperty() のインスタンス

• Key

• 各entityを一意に識別

• Data store が勝手に振ってくれる

Wednesday, May 5, 2010

Page 34: Introduction To Google App Engine

モデルの定義

class Pet(db.Model): name = db.StringProperty(required=True) type = db.StringProperty(required=True, choices=set(["cat", "dog", "bird"])) birthdate = db.DateProperty() weight_in_pounds = db.IntegerProperty() spayed_or_neutered = db.BooleanProperty() owner = db.UserProperty(required=True)

Wednesday, May 5, 2010

Page 35: Introduction To Google App Engine

データの格納

• インスタンスを作成して, put()メソッドを呼び出す

• コンストラクタも使用可greeting = Greeting(author = users.get_current_user(), content = self.request.get('content'))

greeting.put()

Wednesday, May 5, 2010

Page 36: Introduction To Google App Engine

GQLでデータ取得• db.GqlQuery("SELECT * FROM Greeting ORDER BY date DESC LIMIT 10")

• greetingsはGreetingインスタンスのリスト

• ループは for greeting in greetings: と簡単

• いろいろな方法

• db.GqlQuery("SELECT * FROM Greeting ORDER BY date DESC LIMIT 10")

• Greeting.gql("ORDER BY date DESC LIMIT 10")

• "SELECT * FROM Greeting"は省略可(メソッドが補ってくれる)

Wednesday, May 5, 2010

Page 37: Introduction To Google App Engine

メソッド呼び出しのようなインターフェース

• Greeting.all()

greetings.filter("author =", users.get_current_user())

greetings.order("-date")

• つなげて書くこともできる• Greeting.all().filter("author =", users.get_current_user()).order("-date")

Wednesday, May 5, 2010

Page 38: Introduction To Google App Engine

• 開発環境

• Hello World!

• Webapp フレームワーク

• Googleアカウントでのユーザー認証

• フォーム処理

• データストア

• テンプレート

• 静的ファイル

• デプロイ

Wednesday, May 5, 2010

Page 39: Introduction To Google App Engine

テンプレート

• EZT, Cheetah, ClearSilver, Quixote, Django が有名

• Django のテンプレートはデフォルト

Wednesday, May 5, 2010

Page 40: Introduction To Google App Engine

• template 用 html を準備

• template を import

• template へは Dictionary を渡す

• template.render(path, values)

Wednesday, May 5, 2010

Page 41: Introduction To Google App Engine

Django template

• 変数は {{ variable }}

• 制御系は {% foo %}

• {% for * %}

• {% if * %}

• {% include * %}

Wednesday, May 5, 2010

Page 42: Introduction To Google App Engine

template の継承

• Django template 独自の概念

• class の template と似ている

• {% extends foo.html %}

• {% block bar %}

Wednesday, May 5, 2010

Page 43: Introduction To Google App Engine

• 開発環境

• Hello World!

• Webapp フレームワーク

• Googleアカウントでのユーザー認証

• フォーム処理

• データストア

• テンプレート

• 静的ファイル

• デプロイ

Wednesday, May 5, 2010

Page 44: Introduction To Google App Engine

静的ファイル• index.html に, クライアントからアクセスする事はできない

• さっきのはプログラムからだったのでok

• クライアントのブラウザからアクセスする必要のある静的ファイル

• 画像, css, JavaScript, 映像, Flash

Wednesday, May 5, 2010

Page 45: Introduction To Google App Engine

app.yaml で設定- url: /stylesheets

static_dir: stylesheets

• クライアントブラウザから /stylesheets/*

にアクセスがあると, stylesheets ディレクトリにパスの残りをマッピングする

Wednesday, May 5, 2010

Page 46: Introduction To Google App Engine

• 開発環境

• Hello World!

• Webapp フレームワーク

• Googleアカウントでのユーザー認証

• フォーム処理

• データストア

• テンプレート

• 静的ファイル

• デプロイ

Wednesday, May 5, 2010

Page 47: Introduction To Google App Engine

デプロイ• アプリケーションの登録

• http://appengine.google.com/

• app.yamlの編集

• application: の値を登録したアプリケーションIDに変更

• アップロード

• appcfg.py update helloworld/

• プロンプトでメールアドレスとパスワードを入力

• http://application-id.appspot.com

Wednesday, May 5, 2010

Page 48: Introduction To Google App Engine

その他の機能

Wednesday, May 5, 2010

Page 49: Introduction To Google App Engine

• memcache

• URL Fetch

• Mail

• 画像

• Cron (スケジューリングタスク)

• サードパーティライブラリ

Wednesday, May 5, 2010

Page 50: Introduction To Google App Engine

• memcache

• URL Fetch

• Mail

• 画像

• Cron (スケジューリングタスク)

• サードパーティライブラリ

Wednesday, May 5, 2010

Page 51: Introduction To Google App Engine

memcach

• データをキャッシュできる

• memchached 互換

• key-value store

Wednesday, May 5, 2010

Page 52: Introduction To Google App Engine

• memcache

• URL Fetch

• Mail

• 画像

• Cron (スケジューリングタスク)

• サードパーティライブラリ

Wednesday, May 5, 2010

Page 53: Introduction To Google App Engine

URL Fetch

• HTTP/HTTPS リクエストで, インターネット経由で他のドメインにあるリソースにアクセス/他のホストと通信

• Googleのインフラを使っているので, 効率・スケーラビリティ高

Wednesday, May 5, 2010

Page 54: Introduction To Google App Engine

使いかた

• urllib, urllib2, httplib をそのまま使う

• 裏ではgoogleのインフラ上で走る

• app engine の api

Wednesday, May 5, 2010

Page 55: Introduction To Google App Engine

• memcache

• URL Fetch

• Mail

• 画像

• Cron (スケジューリングタスク)

• サードパーティライブラリ

Wednesday, May 5, 2010

Page 56: Introduction To Google App Engine

Mail

• メールの送信

Wednesday, May 5, 2010

Page 57: Introduction To Google App Engine

• memcache

• URL Fetch

• Mail

• 画像

• Cron (スケジューリングタスク)

• サードパーティライブラリ

Wednesday, May 5, 2010

Page 58: Introduction To Google App Engine

画像• 基本的な画像処理

• サイズ変更、回転、左右反転、トリミング

• 複数の画像を 1 つの画像にまとめる

• データ形式の変換

• エンハンス

• 情報の取得 (フォーマット,幅,高さ,カラー値のヒストグラム)

Wednesday, May 5, 2010

Page 59: Introduction To Google App Engine

• memcache

• URL Fetch

• Mail

• 画像

• Cron (スケジューリングタスク)

• サードパーティライブラリ

Wednesday, May 5, 2010

Page 60: Introduction To Google App Engine

Cron

• ある時刻・一定時間でタスクを実行

• cron.yaml

Wednesday, May 5, 2010

Page 61: Introduction To Google App Engine

cron.yamlcron:

- description: daily summary job

url: /tasks/summary

schedule: every 24 hours

- description: monday morning mailout

url: /mail/weekly

schedule: every monday of month 09:00

timezone: Australia/NSW

Wednesday, May 5, 2010

Page 62: Introduction To Google App Engine

スケジュールのフォーマット

• every 5 minutes

• every 12 hours

• 2nd,third mon,wed,thu of march 17:00

• every monday of month 09:00

• 1st monday of sep,oct,nov 17:00

Wednesday, May 5, 2010

Page 63: Introduction To Google App Engine

app.yaml のハンドラに認証を追加

handlers:

- url: /report/weekly

script: reports.py

login: admin

Wednesday, May 5, 2010

Page 64: Introduction To Google App Engine

• memcache

• URL Fetch

• Mail

• 画像

• Cron (スケジューリングタスク)

• サードパーティライブラリ

Wednesday, May 5, 2010

Page 65: Introduction To Google App Engine

サードパーティライブラリ• pure python ならば何でもok

• ルートディレクトリに設置

• デフォルトでバンドル

• Antlr 3

• Django 0.9.6

• PyCrypto

• WebOb

• YAML

• zipimport

Wednesday, May 5, 2010

Page 66: Introduction To Google App Engine

ありがとうございました

Wednesday, May 5, 2010