GAE/PyでLow Level API Keyオブジェクトを触ってみる
Slim3本を読んで「PythonでもデータストアのLow Level APIを触ってみたいなー」と思った。うちのMacはメモリが少ないのでEclipseを起動したくないのです。
オープンソース徹底活用Slim3onGoogleAppEngineforJava
- 作者: ひがやすを,小川信一
- 出版社/メーカー: 秀和システム
- 発売日: 2010/07/30
- メディア: 単行本
- 購入: 12人 クリック: 462回
- この商品を含むブログ (36件) を見る
Slim3本に沿って、まずはKeyオブジェクトから触ってみる。KeyというのはGoogle App Engineのエンティティを一意にする識別子。リレーショナルデータベースでいうところのプライマリーキーのようなもの。
BigTable全体の中で一意になる必要があるため、キーの中に
- アプリケーションID
- 親キー
- kind(テーブル名みたいなもの)
- ID or キー名
の情報を持っている。
Keyの動作確認コード
def test_idkey(self): key = Key.from_path('mykind', 1) print `key` def test_namedkey(self): key = Key.from_path('mykind', 'name') print `key`
Keyクラスの「from_path」関数でKeyを生成する。パラメータには「kindとid」または「kindとキー名」を渡す。idは数値でキー名は文字列。
出力
datastore_types.Key.from_path(u'mykind', 1, _app=u'testbed-test') datastore_types.Key.from_path(u'mykind', u'name', _app=u'testbed-test')
親子関係をもつキー
キーを作成する時に親キーを指定することができる。これでキーの親子関係ができる。
テスト
def test_childkey(self): parent = Key.from_path('parentkind', 1) child = Key.from_path('childkind', 1, parent=parent) grandchild = Key.from_path('grandchildkind', 1, parent=child) print `parent` print `child` print `grandchild`
出力
datastore_types.Key.from_path(u'parentkind', 1, _app=u'testbed-test') datastore_types.Key.from_path(u'parentkind', 1, u'childkind', 1, _app=u'testbed-test') datastore_types.Key.from_path(u'parentkind', 1, u'childkind', 1, u'grandchildkind', 1, _app=u'testbed-test')