Ruby で Windows Azure Storage テーブルを操作するには

December 10, 2011

Github にある johnnyhalife/waz-storage · GitHub ライブラリを使うと簡単に操作できます。Windows Azure Storage REST API のラッパーになってます。

Rubygems でインストール
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
$ sudo gem install waz-storage
Fetching: mime-types-1.17.2.gem (100%)
Fetching: rest-client-1.6.7.gem (100%)
Fetching: ruby-hmac-0.4.0.gem (100%)
Fetching: waz-storage-1.1.1.gem (100%)
Successfully installed mime-types-1.17.2
Successfully installed rest-client-1.6.7
Successfully installed ruby-hmac-0.4.0
Successfully installed waz-storage-1.1.1
4 gems installed
Installing ri documentation for mime-types-1.17.2...
Installing ri documentation for rest-client-1.6.7...
Installing ri documentation for ruby-hmac-0.4.0...
Installing ri documentation for waz-storage-1.1.1...
Installing RDoc documentation for mime-types-1.17.2...
Installing RDoc documentation for rest-client-1.6.7...
Installing RDoc documentation for ruby-hmac-0.4.0...
Installing RDoc documentation for waz-storage-1.1.1...
認証

waz-storage ライブラリの認証方法は Base のクラスメソッド establish_connection! にアカウント名、アクセスキー、SSLを使うかどうかをハッシュで渡します。

1
2
3
4
5
6
require 'waz-storage'
require 'waz-tables'
wazopt = { :account_name =\> 'accounttilfin',
:access_key =\> 'XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX==',
:use_ssl =\> true }
WAZ::Storage::Base.establish_connection!(wazopt)

テーブルサービスの操作

テーブルの全てのエンティティを取得

エンティティを取り出します。サービスインスタンスにテーブル名を指定して query メソッドを呼ぶだけです。

service = WAZ::Tables::Table.service_instance
entities = @service.query(“<テーブル>”)

これでハッシュの配列でエンティティ群が取得できます。
ただ一度に取れるのは、1000件までです。continuation_token に続きのPartitionKeyとRowKeyが入っています。これを query メソッドのオプションに渡します。

1
2
3
if entities.continuation_token\["NextPartitionKey"\].nil?
next_entities = service.query("<テーブル>", { :continuation_token =\> entities.continuation_token })
end
パーティション内のエンティティを取得

query オプションに expression を指定できるのでここでフィルタで定義します。

1
2
3
partition_key = "<PartitionKey>"
entities = service.query("<テーブル>",
{ :expression =\> "PartitionKey eq #{partition_key}" })

テーブル デザイナー用のフィルター文字列の作成

エンティティの取得

特定の Row だけ取得します。

1
entity = service.get_entity("<テーブル>", "<PartitionKey>", "<RowKey>")
エンティティの追加・更新

追加は insert_enity, 置き換え更新は update_enity, マージ更新は merge_enity になります。

1
2
3
4
5
entity = { :name =\> "なまえ", :age =\> 25 }
entity\[:partition_key\] = "<PartitionKey>"
entity\[:row_key\] = "<RowKey>"
entity\[:Timestamp\] = Time.now.utc
service.insert_entity("<テーブル>", entity)

upsert をやりたい場合は、REST API の 2011-08-18 バージョンから追加するもしくはマージ更新という仕様に変わっています。そこでライブラリに下記のように mix-in を使うことで upsert_entity を実装できます。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
module WAZTablesTableExtends
def upsert_entity(table_name, entity)
raise WAZ::Tables::InvalidTableName, table_name unless WAZ::Storage::ValidationRules.valid\_table\_name?(table_name)
response = execute(:merge, "#{table_name}(PartitionKey='#{entity\[:partition_key\]}',RowKey='#{entity\[:row_key\]}')",
{},
default_headers.merge({'x-ms-version' =\> '2011-08-18'}),
generate\_payload(table\_name, entity))
return parse_response(response)
end
end

class WAZ::Tables::Service
include WAZTablesTableExtends
end
エンティティの削除

削除は delete_entity を呼び出します。

service.delete_entity(“<テーブル>”, “<PartitionKey>”, “<RowKey>”)

Ruby Azure

tilfin freelance software engineer