Windows Azure Storage
Windows Azure Storageには、Table、Blob、Queueという3種類のストレージがあります。Blobストレージは、構造化されていない大きなデータの塊を保存するためのものです。Queueストレージは、Web RoleとWorker Roleの間の一時的なメッセージ送受に使用するためのものです。Tableストレージは、構造化データを拡張可能な方法で保存するためのものです。以下の表に、これらのストレージの簡単な説明を示します。
Tableストレージは、Azure SQLと同じではなく、リレーショナルデータベースモデルともまったく関係ありません。Tableストレージは、リレーショナルではありません。
| Storage Service | Primary Use |
| Table | Structured data |
| Blob | Unstructured data |
| Queue | Passing messages between cloud service roles |
Azure Storageには、RESTインターフェースを介してアクセスします。従って、HTTPリクエストを発行できるならば、どこからでもアクセスできます。つまり、Tableストレージには、ADO.NET Data Servicesを用いてアクセスすることもできます。ありがたいことに、Azure Storageとのインターフェース用のマネージドコードラッパーが、Azure SDKの一部としてMicrosoftにより既に構築されています。July CTPの時点では、このプロジェクトは「StorageClient」という名前で、Azure SDKをインストールすると、C:\Program Files\Windows Azure SDK\v1.0\Samples\StorageClientに(全ソースが)格納されます。このプロジェクトを自分のCloud Serviceソリューションに直接追加するか、または、アセンブリをコンパイルしてそれをソリューション内で参照できます。StorageClientインターフェースは、本稿の例全体を通して、TableおよびQueueストレージを利用するために使用します。
Windows Azure Tableストレージを使用する
開発環境のTableストレージは、Windows Azure Storageサービスのものとは少し異なります。相違点の1つは、開発環境のTableストレージでは、スキーマが固定でなければならないという点です。完全なAzure StorageサービスのTableストレージには、テーブル設定は必要なく、同一のテーブル内の多数のエンティティに、任意の数や種類のプロパティを追加できます。開発ストレージとWindows Azure Storageのすべての相違点については、http://msdn.microsoft.com/en-us/library/dd320275.aspxを参照ください。
テーブルのデータにアクセスするためのテーブルスキーマとデータサービスコンテキストを作成するには、2つの別個のクラスを作成します。1つ目のクラスの名前を「Calculation.cs」、2つ目のクラスを「CalculationDataServiceContext.cs」とします。これら2つのクラスを作成したプロジェクトにおいて、System.Data.Services.Clientへの参照を追加します。2つのクラスに、それぞれ以下のコードを追加します(詳細についてはコード内のコメントを参照ください)。
public class Calculation : Microsoft.Samples.ServiceHosting.StorageClient.TableStorageEntity
{
// Inheriting from TableStroageEntiry predefines the 3 necessary
// properties for storing any entity in Table storage; PartitionKey,
// RowKey and TimeStamp
public Calculation()
{
// Hardcoded for this example but can be anything; best practice
// is to assign the PartitionKey based on a value that groups
// your data to optimize search performance.
PartitionKey = "TEST";
// Unique identifier with each partition
RowKey = DateTime.Now.Ticks.ToString();
}
public int ValueOne { get; set; }
public int ValueTwo { get; set; }
public int CalcValue { get; set; }
}
CalculationDataServiceContext.cs
public class CalculationDataServiceContext : TableStorageDataServiceContext
{
public CalculationDataServiceContext(StorageAccountInfo accountInfo)
: base(accountInfo)
{ }
// Properties that return IQueryable<T> are used by VS to create a
// table in development storage named after the property. The
// schema is defined by the public properites in the model class.
public IQueryable<CALCULATION> Calculations
{
get
{
return this.CreateQuery<CALCULATION>("Calculations");
}
}
// Method to take two input values, input them into the table storage
// and returns the RowKey so the value can be later retrieved.
public string AddCalculation(int valueOne, int valueTwo)
{
Calculation calc = new Calculation();
calc.ValueOne = valueOne;
calc.ValueTwo = valueTwo;
this.AddObject("Calculations", calc);
this.SaveChanges();
return calc.RowKey;
}
}
2つのクラスを正しく作成したら、次に、Visual Studioによってテストストレージテーブルを作成します。Solution Explorerで、MyCloudServiceプロジェクトを右クリックし、[Create Test Storage Tables]を選択します。処理が完了すると、Visual Studioはメッセージを表示します。ここで、アプリケーションをデバッグし、[Development Storage]管理ウィンドウを開くと、Tableサービスが起動していることが確認できます。

次に、ファイル「Default.aspx.cs」にコードを追加して、新しいCalculationオブジェクトを作成し、Tableストレージに保存できるようにします。そのためには、Page_Loadメソッドと[start calculation]ボタンのクリックイベントハンドラにコードを追加します。
using Microsoft.Samples.ServiceHosting.StorageClient;
using System.Data.Services.Client;
// ...
protected void Page_Load(object sender, EventArgs e)
{
try
{
StorageAccountInfo accountInfo =
StorageAccountInfo.GetAccountInfoFromConfiguration(
"TableStorageEndpoint");
// Not required in development since the table is created
// manually but is needed in the cloud to create the tables
// the first time.
TableStorage.CreateTablesFromModel(
typeof(CalculationDataServiceContext),
accountInfo);
}
catch (DataServiceRequestException ex)
{
// Unable to connect to table storage. In development
// verify the service is running. In the cloud check that
// the config values are correct.
// Handle error
}
}
protected void cmdStartCalculation_Click(object sender, EventArgs e)
{
StorageAccountInfo accountTableInfo =
StorageAccountInfo.GetDefaultTableStorageAccountFromConfiguration();
CalculationDataServiceContext calculationDSContext =
new CalculationDataServiceContext(accountTableInfo);
lblCalculationKey.Text =
calculationDSContext.AddCalculation(
int.Parse(txtValueOne.Text),
int.Parse(txtValueTwo.Text));
lblCalculationKey.Visible = true;
}
ここでアプリケーションを実行すると、2つの数値を入力し、それらをTableストレージに保存し、後で結果を取得するためのRowKeyを返すことができます。次のステップでは、2つの入力値を取得してそれらを加算するために、Queueストレージを介してWorker Roleにメッセージを送信します。
