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 KeyRow KeyTimestamp

  -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 First
CloudStorageAccount 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,就可以查看到我们插入成功的数据。

Windows Azure Storage (6) Windows Azure Storage之Table的更多相关文章

  1. Windows Azure Storage (20) 使用Azure File实现共享文件夹

    <Windows Azure Platform 系列文章目录> Update 2016-4-14.在Azure VM配置FTP和IIS,请参考: http://blogs.iis.net/ ...

  2. [Windows Azure] .NET Multi-Tier Application Using Storage Tables, Queues, and Blobs - 1 of 5

    .NET Multi-Tier Application Using Storage Tables, Queues, and Blobs - 1 of 5 This tutorial series sh ...

  3. [转]windows azure How to use Blob storage from .NET

    本文转自:http://azure.microsoft.com/en-us/documentation/articles/storage-dotnet-how-to-use-blobs/?rnd=1 ...

  4. Azure Queue Storage 基本用法 -- Azure Storage 之 Queue

    Azure Storage 是微软 Azure 云提供的云端存储解决方案,当前支持的存储类型有 Blob.Queue.File 和 Table. 笔者在<Azure File Storage 基 ...

  5. Azure File Storage 基本用法 -- Azure Storage 之 File

    Azure Storage 是微软 Azure 云提供的云端存储解决方案,当前支持的存储类型有 Blob.Queue.File 和 Table. 笔者在<Azure Blob Storage 基 ...

  6. Azure Blob Storage 基本用法 -- Azure Storage 之 Blob

    Azure Storage 是微软 Azure 云提供的云端存储解决方案,当前支持的存储类型有 Blob.Queue.File 和 Table. 笔者在<Azure Table storage ...

  7. Azure Table storage 基本用法 -- Azure Storage 之 Table

    Azure Storage 是微软 Azure 云提供的云端存储解决方案,当前支持的存储类型有 Blob.Queue.File 和 Table,其中的 Table 就是本文的主角 Azure Tabl ...

  8. Windows Azure Web Site (18) Azure Web App设置MIME

    <Windows Azure Platform 系列文章目录> 在笔者之前的文章中,介绍了我们在使用Azure Web App,可以把静态资源保存到Azure Storage中: Wind ...

  9. 【初码干货】【Azure系列】1、再次感受Azure,体验Windows Server 2016并部署BlogEngine.NET

    上个月末,在某人的建议下,重新注册了一个1元试用账户(包含1个月期限的1500元订阅),并充值了1000元转为了正式账户,相当于1000元得到了2500的订阅,于是又一次开启了Azure之旅. 在这不 ...

随机推荐

  1. 【ajax 提交表单】多种方式的注意事项

    在业务中,可能因为表单内容过于庞大,字段过于繁杂,如果人为去拼接的话 ,需要耗费大量的时间和精力,与此同时,代码看上去也是冗余不堪. 所以,提交表单的时候如果能整个表单数据整体提交,那是非常开心的事情 ...

  2. Cookie 用法 小记

    //保存cookie Cookie cookieName = new Cookie("name", realUser.getName());                 Coo ...

  3. 一个神奇的POS -扫描 现场销售 开单打印票据 安卓物联网POS机 手持开单终端机 省时省力 高效准确!!

    5寸高清彩屏,高端大气上档次,小巧轻便,独特的包胶防护,坚固耐用,外形精细,美观!与软件灵活对接,解决企业手工盘点,手工输单,库存管理等困难,提高准确率,提高工作效率!! 应用领域:适用于仓库.超市. ...

  4. 让不支持h5新标签的浏览器支持新标签

    把这段js加到页面的头部就可以了,创建想让浏览器支持的标签即可 //条件判断是否支持 h5 if(window.applicationCache){ alert("支持h5") } ...

  5. python中的函数

    Python 函数 函数是组织好的,可重复使用的,用来实现单一,或相关联功能的代码段. 函数能提高应用的模块性,和代码的重复利用率.你已经知道Python提供了许多内建函数,比如print().但你也 ...

  6. unison+inotify-tools触发式双向自动同步

    双向实时数据同步部署 首先添加服务器ssh信任,即免秘钥登陆 Web1:192.168.10.36 Web2:192.168.10.37 分别在web1和web2上执行以下命令 mkdir ~/.ss ...

  7. (RMQ版)LCA注意要点

    inline int lca(int x,int y){ if(x>y) swap(x,y); ]][x]]<h[rmq[log[y-x+]][y-near[y-x+]+]])? rmq[ ...

  8. 【第一课】WEBIX 入门自学-介绍WEBIX

    Webix是跨浏览器.跨平台的JavaScript框架,使用JavaScript.CSS,HTML5技术构建交互式web应用程序.库中提供几十个完全可定制的组件,提供了JQuery集成和可以处理任何服 ...

  9. 2-Sat问题

    二分+2-Sat 判断是否可行 输出字典序最小的解 输出字典序可行解 其实这些都是小问题,最重要的是建图,请看论文. 特殊的建边方式,如果a b是一对,a必须选,那么就是b->a建边. HDU ...

  10. 关于Web服务器的认识

    马上就要毕业了,也要开始找工作了,大学写了这么多代码了,却没有好好总结一下常用的概念很是遗憾额,就通过这篇博客记录一下我最常用的一些知识好了. 说到Web服务器,有很多文章都介绍的很好,之前看到一篇非 ...