Windows Azure Storage (6) Windows Azure Storage之Table
《Windows Azure Platform 系列文章目錄》
?
最近想了想,還是有必要把Windows Azure Table?Storage 給說清楚。
?
1.概念
Windows Azure Table是存儲在云端的非關系型數據表,主要存儲結構化數據,簡單理解上來是類似SQL Server的一張單表,包含了列名和行數據,但是無法執行關系型運算(e.g. inner join)。在某些場景,比如只紀錄系統運行日志、用戶操作日志等場景下,比較適合使用Table?Storage。
使用Table Storage,除了設置賬戶信息(Account Name和Access Key)以外,還需要設置TableName。TableName就是存儲的數據表。(比如我可以使用Product來存儲產品信息數據,使用Client來存儲用戶信息數據,這個概念和SQL Server非常類似)。
Table Service 主要是存儲結構化數據的,所有的數據都是一個個 Entity,多個 Entity 在一起作為一個 Table。而每一個 Entity 中最多包含 255 個 Property。每個 Property 以鍵值對(Key-Value Pair)的方式保存,Key 是字符串類型而 Value 可以是任意的.NET 標準類型,比如字符串、整型、日期等。但是,Storage Service 要求每一個 Entity 必須包含下面三個 Property:Partition Key、Row Key和 Timestamp。
?
-PartitionKey設置了Table中數據的分區規則。比如下圖中
前2行數據具有相同的Partition Key,名為Examples Doc。那前2行數據在物理存儲上是在同一個存儲節點上(比如同一個磁盤上)。
后2行數據具有相同的Partion key(FAQ Doc)。那后面3行的數據在物理存儲上也是在同一個存儲節點上的。(可能與上面的Example Doc存儲在同一個節點上)
?
這樣的架構設計好處在于:
1.當存儲在Table中的數據訪問量少的時候,Windows Azure會把Example Doc的數據和FAQ Doc的數據放在同一個存儲上。
2.當訪問FAQ Doc的用戶逐漸增多的時候,Windows Azure會把FAQ Doc的數據單獨遷移到某一存儲上,加快訪問速度。
?
-RowKey 是識別Table 行數據的唯一標識符。
對于相同的Partition Key的行數據來說,他們的RowKey必須是唯一的。
一般情況下,我們可以使用GUID來設置RowKey。
?
Partition Key 和 Row Key 可以作為 Entity 的聯合主鍵。
?
-TimeStamp
DateTime類型,這個屬性是由系統維護的,用戶無法修改。表示該行數據的最后操作時間。
?
2.我們使用管理員身份運行VS2013。新建Cloud Project,并且重命名為AzureStorageTable
3.添加ASP.NET Web Role
4.展開AzureStorageTable,展開Roles,右鍵WebRole1。在Settings添加StorageConnectionString,并設置相應的Value
5.在WebRole1 Project中,修改Global.asax.cs代碼如下:
using Microsoft.WindowsAzure; using Microsoft.WindowsAzure.Storage; using Microsoft.WindowsAzure.Storage.Table; using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.Routing; using System.Web.Security; using WebRole1;namespace WebRole1 {public class Global : HttpApplication{void Application_Start(object sender, EventArgs e){// Code that runs on application startup AuthConfig.RegisterOpenAuth();RouteConfig.RegisterRoutes(RouteTable.Routes);//Initialize Table FirstCloudStorageAccount storageAccount = CloudStorageAccount.Parse(CloudConfigurationManager.GetSetting("StorageConnectionString"));CloudTableClient tableClient = storageAccount.CreateCloudTableClient();CloudTable table = tableClient.GetTableReference("Message");table.CreateIfNotExists();}void Application_End(object sender, EventArgs e){// Code that runs on application shutdown }void Application_Error(object sender, EventArgs e){// Code that runs when an unhandled error occurs }} }6.修改Default.aspx,增加TextBox和Button。修改后的界面如下:
7.在Default.aspx.cs里,增加引用如下:
using Microsoft.WindowsAzure; using Microsoft.WindowsAzure.Storage; using Microsoft.WindowsAzure.Storage.Table;8.增加Button_Click事件
protected void btnAddMessage_Click(object sender, EventArgs e){// Retrieve the storage account from the connection string.CloudStorageAccount storageAccount = CloudStorageAccount.Parse(CloudConfigurationManager.GetSetting("StorageConnectionString"));// Create the table client.CloudTableClient tableClient = storageAccount.CreateCloudTableClient();// Create the CloudTable object that represents the "people" table.CloudTable table = tableClient.GetTableReference("Message");// Create a new Message entity.MessageEntity message1 = new MessageEntity();message1.Detail = txbMessage.Text;// Create the TableOperation that inserts the customer entity.TableOperation insertOperation = TableOperation.Insert(message1);// Execute the insert operation. table.Execute(insertOperation);}9. MessageEntity的定義如下:
using System; using System.Collections.Generic; using System.Linq; using System.Web; using Microsoft.WindowsAzure.Storage.Table;namespace WebRole1 {public class MessageEntity : TableEntity{public MessageEntity(){PartitionKey = "Message";RowKey = Guid.NewGuid().ToString();}public string Detail { get; set; }} }?
10.運行VS2013調試。在輸入框中輸入相應的內容,并點擊按鈕。如下圖:
10.點擊按鈕完畢后,我們回到VS2013,點擊菜單欄的VIEW->Server Explorer
11.在Server Explorer中,展開Windows Azure,Storage。選擇相應的Storage Table,就可以查看到我們插入成功的數據。
?
轉載于:https://www.cnblogs.com/threestone/p/3406259.html
總結
以上是生活随笔為你收集整理的Windows Azure Storage (6) Windows Azure Storage之Table的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Sublime Text3前端必备插件
- 下一篇: Weblogic二种修改端口的方法(转)