pythonでAWS. SimpleDBを触ってみる(1). - lolloo-htnの日記
今日は, SimpleDBを使ってみることにした. SimpleDBは, 要はクラウドの中にスプレッドシートを持って参照や更新ができるようなイメージ. 公式ドキュメントのココのページの図が特徴を良くあらわしている.
今日は, SimpleDBを使ってみることにした. SimpleDBは, 要はクラウドの中にスプレッドシートを持って参照や更新ができるようなイメージ. 公式ドキュメントのココのページの図が特徴を良くあらわしている.
(dev)hdknr@domU-12-31-39-00-D9-A1:~/.ve/dev/src$ hg clone
https://hdknr@bitbucket.org/david/django-storages/
destination directory: django-storages
requesting all changes
adding changesets
adding manifests
adding file changes
added 43 changesets with 105 changes to 51 files
updating working directory
32 files updated, 0 files merged, 0 files removed, 0 files unresolved
(dev)hdknr@domU-12-31-39-00-D9-A1:~/.ve/dev/src$ cd django-storages/
(dev)hdknr@domU-12-31-39-00-D9-A1:~/.ve/dev/src/django-storages$ ls -al
total 68
drwxr-xr-x 6 hdknr users 4096 2009-10-11 05:20 .
drwxr-xr-x 3 hdknr users 4096 2009-10-11 05:20 ..
-rw-r--r-- 1 hdknr users 562 2009-10-11 05:20 AUTHORS
drwxr-xr-x 2 hdknr users 4096 2009-10-11 05:20 backends
drwxr-xr-x 2 hdknr users 4096 2009-10-11 05:20 docs
drwxr-xr-x 4 hdknr users 4096 2009-10-11 05:20 examples
drwxr-xr-x 3 hdknr users 4096 2009-10-11 05:20 .hg
-rw-r--r-- 1 hdknr users 103 2009-10-11 05:20 .hgignore
-rw-r--r-- 1 hdknr users 1539 2009-10-11 05:20 LICENSE
-rw-r--r-- 1 hdknr users 315 2009-10-11 05:20 README
-rw-r--r-- 1 hdknr users 21229 2009-10-11 05:20 S3.py
-rw-r--r-- 1 hdknr users 988 2009-10-11 05:20 setup.py
Data Storage in Amazon SimpleDB vs. Data Storage in Amazon S3
Unlike Amazon S3, Amazon SimpleDB is not storing raw data. Rather, it takes your data as input and expands it to create indices across multiple dimensions, which enables you to quickly query that data. Additionally, Amazon S3 and Amazon SimpleDB use different types of physical storage. Amazon S3 uses dense storage drives that are optimized for storing larger objects inexpensively. Amazon SimpleDB stores smaller bits of data and uses less dense drives that are optimized for data access speed.
In order to optimize your costs across AWS services, large objects or files should be stored in Amazon S3, while smaller data elements or file pointers (possibly to Amazon S3 objects) are best saved in Amazon SimpleDB. Because of the close integration between services and the free data transfer within the AWS environment, developers can easily take advantage of both the speed and querying capabilities of Amazon SimpleDB as well as the low cost of storing data in Amazon S3, by integrating both services into their applications.
For the Beta release, a single Amazon SimpleDB domain may grow to 10 GB and you are initially allocated a maximum of 100 domains; however, over time these allocations may be raised. Please complete this form if you require additional domains.
でかいデータはS3、ポインタ情報の様な小さいデータはSimpleDB。
(jail)hdknr@mailjail:~/.ve/jail/src$ svn checkout http://simpledb-dev.googlecode.com/svn/trunk/ simpledb-dev
A simpledb-dev/simpledb-dev
A simpledb-dev/simpledb-dev/src
A simpledb-dev/simpledb-dev/src/simpledb_dev.py
A simpledb-dev/simpledb-dev/src/portalocker.py
A simpledb-dev/simpledb-dev/src/templates
A simpledb-dev/simpledb-dev/src/templates/Query.xml
A simpledb-dev/simpledb-dev/src/templates/GetAttributes.xml
A simpledb-dev/simpledb-dev/src/templates/ListDomains.xml
A simpledb-dev/simpledb-dev/src/templates/QueryWithAttributes.xml
A simpledb-dev/simpledb-dev/src/templates/DeleteAttributes.xml
A simpledb-dev/simpledb-dev/src/templates/error.xml
A simpledb-dev/simpledb-dev/src/templates/DeleteDomain.xml
A simpledb-dev/simpledb-dev/src/templates/CreateDomain.xml
A simpledb-dev/simpledb-dev/src/templates/PutAttributes.xml
今回紹介するオープンソース・ソフトウェアはSimpleDB/dev、Python製のSimpleDBクローンだ。
SimpleDB/devはAmazon Webサービスの一つ、SimpleDBをローカルでも動作させられるものだ。SimpleDBはスキーマ情報を持たないデータベースで、簡単にデータの登録および取得ができる。
SimpleDB/devはデフォルトでポート番号8080で立ち上がる。サービスが立ち上がったら、開発用アドレスとしてlocalhostを設定しておき、開発を行えば良い。SimpleDB/devはSimpleDBの置き換えを目指すものではないので、開発用として考えよう。
仕様としては2007年11月07日版REST APIの機能をサポートしている。アクションは全てをサポートしており、HTTPレスポンスも同じものになるように作られている。なお、逆にない機能としてはSOAP APIへの対応、認証、タイムスタンプ形式のチェック、HTTPSとなっている。
同じような機能を持ったライブラリは他にも存在する。だがAPIとの接続形式は変わらないので実装言語に依らず、自由に選択ができるのが魅力だ。Rubyの開発でも、PHPの開発でもクライアントライブラリさえあれば容易に使えるだろう。SimpleDBを使った開発を行われる方は要チェックだ。
SimpleDBでは, 作業はdomain(シートに相当)上で行うため, これらを作成したり,削除したり,既に作ったdomainに接続する必要がある. これらの操作を実現するbotoの関数を触ってみた. 解説は特にいらないほどシンプルなものである.
あと, かなりのバッドノウハウだが, SimpleDBは文字列しか扱えないため, 数値のattributeの並び替えで問題が発生する(100より2の方が大きいと判断される, 辞書順だから). なので数値は固定桁,ゼロパディングな感じにする必要がある. (2ではなく00000002のようにする). 文字列しか扱えないのはSimpleDBの仕様とのことだがなんとかならないのだろうか.
パフォーマンス
SimpleDBのパフォーマンスは、MySQLのようなデータベースにどの程度匹敵するのだろうか?SimpleDBの大きな問題は、それぞれのリクエストがHTTPを用いて送信されるという点だ。これは、Amazonが強固なインフラを持っていたとしても、パフォーマンスの面では問題となりうる。
この問題を解決する一つの方法は、ウェブアプリケーションとSimpleDBの間にキャッシュを設け、小さなリクエストを多数送るのではなくバッチでリクエストを発行することだろう。
しかし、そもそもSimpleDBはリレーショナルデータベースに取って代わるために開発されたものではない。拡張性に富んだサービスで、一部のサービスやアプリケーションレイヤーを外部に移動させる機会を提供しているのである。ぜひ、Amazonのディスカッションフォーラムを読んで、SimpleDBを用いて実際にどんなことが行われているのかを調べて欲し