Windows Azure入门教学系列 (六):使用Table Storage

本文是Windows Azure入门教学的第六篇文章。

本文将会介绍如何使用Table Storage。Table Storage提供给我们一个云端的表格结构。我们可以把他想象为XML文件或者是一个轻量级的数据库(当然,不是通过SQL 语句进行数据的操作)。

使用Table Storage的方法依然是调用REST API。有关Table Storage REST API的详细信息,请参见Table服务API:

为了方便.NET开发人员,我们在SDK中提供了Microsoft.WindowsAzure.StorageClient类来帮助发送REST请求。

在开始本教学之前,请确保你从Windows Azure 平台下载下载并安装了最新的Windows Azure开发工具。本教学使用Visual Studio 2010作为开发工具。

步骤一:创建解决方案和项目

由于我们要在本地模拟环境下测试Table Storage,首先,请确保Storage Emulator已经启动。我们可以找到管理器的进程手动启动或者让Visual Studio 2010帮助我们启动他。

右击工具栏中Windows Azure模拟器的图标,选择”Show Storage Emulator UI”。弹出如下图所示的窗口:

我们要关注的是Service management中Table所在的一行。要确保Status为Running。

确认完毕后启动Visual Studio 2010,并且新建一个Console项目。

步骤二:添加程序集引用

请在项目属性页里确认项目的Target framework的值是.NET Framework 4或.NET Framework 3.5。然后添加对C:Program FilesWindows Azure SDKv1.3refMicrosoft.WindowsAzure.StorageClient.dll的引用。该路径为SDK默认安装路径,如果你不能在这个路径中找到Microsoft.WindowsAzure.StorageClient.dll请从SDK安装路径中寻找。

接下来添加对System.Data.Services.Client程序集的引用。该程序集安装在GAC中。你能够在Add Reference窗口的.NET标签下找到他。

步骤三:添加代码

首先在项目中的Program.cs中引用命名空间:

 


1

2

3

using Microsoft.WindowsAzure;

 

using Microsoft.WindowsAzure.StorageClient;

然后在Program.cs中添加如下代码:


1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

115

116

117

118

119

120

121

122

123

124

125

126

127

128

129

130

131

132

133

134

135

136

137

138

139

140

141

142

143

144

145

146

147

148

149

150

151

152

153

154

155

156

157

158

159

160

161

162

163

class Program

 

{

 

    static void Main(string[] args)

 

    {

 

        var storageAccount = CloudStorageAccount.DevelopmentStorageAccount;

 

        var tableStorage = storageAccount.CreateCloudTableClient();

 

  

 

        // 检查名为CustomerInfo的表格是否被创建,如果没有,创建它

 

        tableStorage.CreateTableIfNotExist("CustomerInfo");

 

  

 

        // 创建表格服务上下文

 

        var context = new CustomerInfoContext(storageAccount.TableEndpoint.AbsoluteUri, storageAccount.Credentials);

 

  

 

        // 插入两条客户信息数据,客户ID分别设置为0和1

 

        CustomerInfo ci1 = new CustomerInfo() { CustomerAge = 25, CustomerID = "0", CustomerName = "Mike" };

 

        context.AddObject("CustomerInfo", ci1);

 

  

 

        CustomerInfo ci2 = new CustomerInfo() { CustomerAge = 32, CustomerID = "1", CustomerName = "Peter" };

 

        context.AddObject("CustomerInfo", ci2);

 

  

 

        context.SaveChanges();

 

  

 

  

 

        // 查找CustomerID为1的客户数据并显示

 

  

 

        Console.WriteLine("Retrieve information of a customer whose ID is 1");

 

  

 

        var query = context.CreateQuery<CustomerInfo>("CustomerInfo").Where(c => c.CustomerID == "1").ToList();

 

        var returnedcustomerinfo = query.FirstOrDefault();

 

  

 

        Console.WriteLine(string.Format("Customer info retrieved: ID:{0},Name:{1},Age:{2}",

 

            returnedcustomerinfo.CustomerID, returnedcustomerinfo.CustomerName, returnedcustomerinfo.CustomerAge));

 

  

 

  

 

        // 更新CustomerID为1的客户数据中的年龄

 

        returnedcustomerinfo.CustomerAge = 33;

 

        context.UpdateObject(returnedcustomerinfo);

 

        Console.WriteLine("**Customer Info updated**");

 

  

 

        // 重新查询,测试更新效果

 

  

 

        Console.WriteLine("Retrieve information of a customer whose ID is 1");

 

  

 

        var query2 = context.CreateQuery<CustomerInfo>("CustomerInfo").Where(c => c.CustomerID == "1").ToList();

 

        var returnedcustomerinfo2 = query.FirstOrDefault();

 

        Console.WriteLine(string.Format("Customer info retrieved: ID:{0},Name:{1},Age:{2}",

 

                returnedcustomerinfo2.CustomerID, returnedcustomerinfo2.CustomerName, returnedcustomerinfo2.CustomerAge));

 

  

 

  

 

        // 删除插入的两条客户数据

 

        context.DeleteObject(ci1);

 

        context.DeleteObject(ci2);

 

        context.SaveChanges();

 

  

 

        Console.WriteLine("The records has been deleted");

 

        Console.ReadLine();

 

    }

 

  

 

}

 

public class CustomerInfo : TableServiceEntity

 

{

 

    public string CustomerID

 

    {

 

        get { return this.RowKey; }

 

        set { this.RowKey = value; }

 

    }

 

    public string CustomerName { get; set; }

 

    public int CustomerAge { get; set; }

 

  

 

    public CustomerInfo()

 

    {

 

        this.PartitionKey = "mypartitionkey";

 

    }

 

}

 

  

 

public class CustomerInfoContext : TableServiceContext

 

{

 

    public CustomerInfoContext(string baseAddress, StorageCredentials credentials) :

 

        base(baseAddress, credentials)

 

    {

 

    }

 

}

步骤四:观察并分析代码

步骤三中的代码中,首先我们通过CloudStorageAccount.DevelopmentStorageAccount来说明我们使用的本地的Development Storage自带账户而不是真正的云端存储服务账户。(如果要用真实账户可以使用

//DefaultEndpointsProtocol=https可以改成DefaultEndpointsProtocol=http表示用HTTP而不是HTTPS

CloudStorageAccount.Parse("DefaultEndpointsProtocol=https;AccountName=[用户名];AccountKey=[密码]");

来实例化对象)然后通过该账户类来实例化一个Table客户端类。这两步是使用SDK中StorageClient程序集来调用Table Storage服务的必要步骤。

然后我们需要关注System.Data.Services.Client程序集。该程序集是WCF Data Services的客户端程序集,能够帮助我们很方便地创建OData客户端程序。(Table Storage提供的REST API遵循OData规范,因此我们的客户端需要遵循OData规范向Table Storage服务发送消息)

我们需要创建上下文来调用服务,我们可以直接使用TableServiceContext。但是通常我们会通过写一个继承自TableServiceContext的类来实现,这样我们可以在该类中添加一些属性或者方法来更加方便地调用。上面的代码中我们继承了TableServiceContext但是并没有添加额外的代码。在实际应用中我们可以加上一些代码来更加方便地调用。比如我们可以在CustomerInfoContext 类中加入下面的属性:


1

2

3

4

5

6

7

8

9

10

11

12

13

public IQueryable<CustomerInfo> CustomerInfo

 

{

 

    get

 

    {

 

        return CreateQuery<CustomerInfo>("CustomerInfo");

 

    }

 

}

这样我们就可以通过调用context.CustomerInfo来更加代替context.CreateQuery<CustomerInfo>("CustomerInfo")。

继承自TableServiceEntity类的CustomerInfo 类定义了每条数据的模式。需要注意的是,与一般的关系型数据库不同,Table Storage并不要求一个表中的所有数据都遵循同一模式。举例来说,在一个表中,可以存储一条有三个字段的记录和一条只有两个字段的记录。这也是我们为什么说可以把Table Storage想象为XML文件的原因。当然在通常情况下我们都会需要在一个表中存储同一模式的数据。这时候我们就可以使用System.Data.Services.Client程序集来为Table Storage创建客户端程序。当我们需要在一个表中存储不同模式的数据时我们可以手动构建和发送REST请求。

还有需要注意的地方是PartitionKey和RowKey。这两项共同组合成表的主键。详细信息,请参见理解Table服务数据模型

代码逻辑包括了对Table Storage的插入,更新,删除和读取。请参考注释部分。

步骤五:运行程序

如果一切正常,你将会看到Console程序输出如下信息:         

时间: 2024-09-18 17:32:31

Windows Azure入门教学系列 (六):使用Table Storage的相关文章

Windows Azure入门教学系列 (七):使用REST API访问Storage Service

本文是Windows Azure入门教学的第七篇文章. 本文将会介绍如何使用REST API来直接访问Storage Service. 在前三篇教学中,我们已经学习了使用Windows Azure SDK所提供的StorageClient来使用Blob Storage, Queue Storage以及Table Storage的基本方法.我们在前几篇教学中也提及最终StorageClient也是通过发送REST请求来与服务器端通信的. 在这篇教学中,我们会以Blob Storage为例,说明如何

Windows Azure入门教学系列 (一): 创建第一个WebRole程序

原文 Windows Azure入门教学系列 (一): 创建第一个WebRole程序 在第一篇教学中,我们将学习如何在Visual Studio 2008 SP1中创建一个WebRole程序(C#语言).在开始学习之前,请确保你的机器上已经安装了: Windows Azure Tools for Microsoft Visual Studio 1.1 (February 2010) Windows Azure Software Development Kit (November 2009) 步骤

Windows Azure入门教学系列 (九):Windows Azure 诊断功能

本文是Windows Azure入门教学的第九篇文章. 本文将会介绍如何使用Windows Azure 诊断功能.跟部署在本地服务器上的程序不同,当我们的程序发布到云端之后,我们不能使用通常的调试方法,例如Remote Debugging等等来对我们的程序进行调试.那么当程序运行出现问题时我们如何能够得知问题的根源以便修正Bug呢?如果我要检测程序的性能,又有什么好的方法呢? 要做到上述的需求,我们需要利用Windows Azure提供的诊断功能,该功能能够记录诊断日志,保存到Windows A

Windows Azure入门教学系列 (二):部署第一个Web Role程序

本文是Windows Azure入门教学的第二篇文章. 在第一篇教学中,我们已经创建了第一个Web Role程序.在这篇教学中,我们将学习如何把该Web Role程序部署到云端. 注意:您需要购买Windows Azure Platform 服务才能继续本教学.您可以通过点击https://mocp.microsoftonline.com/Site/Support.aspx页面上的   联系我们的支持团队来获得购买服务的具体步骤或询问其他有关购买,收费的问题. 在开始本教学之前,请确保你从Win

Windows Azure入门教学系列 (五):使用Queue Storage

本文是Windows Azure入门教学的第五篇文章. 本文将会介绍如何使用Queue Storage.Queue Storage提供给我们一个云端的队列.我们可以用Queue Storage来进行进程间的相互通信(包括运行在不同机器上的进程之间的通信). 一个使用Queue Storage经典的场景是,在一个Web应用程序中,用户通过表单递交给服务器数据,服务器收到数据后将进行处理,而这一处理将花费很多时间.这种情况下,服务器端通过Queue Storage可以把用户递交的信息存储在队列中,后

Windows Azure入门教学系列 (四):使用Blob Storage

本文将会介绍如何使用Blob Storage.Blob Storage可以看做是云端的文件系统.与桌面操作系统上不同,我们是通过REST API来进行对文件的操作.有关REST API的详细信息,请参见Blob 服务 API. 为了方便.NET开发人员,我们在SDK中提供了Microsoft.WindowsAzure.StorageClient类来帮助发送REST请求. 在开始本教学之前,请确保你从Windows Azure 平台下载下载并安装了最新的Windows Azure开发工具.本教学使

Windows Azure入门教学系列 (八):使用Windows Azure Drive

我们知道,由于云端的特殊性,通常情况下,对文件系统的读写建议使用Blob Storage来代替.这就产生了一个问题:对于一个已经写好的本地应用程序,其中使用了NTFS API对本地文件系统读写的代码是否需要进行完全重写以便迁移到Windows Azure平台上呢?答案是否定的.Windows Azure平台提供了Drive的功能. 在1.1版本的SDK中提供了CloudDrive类,能够将本地NTFS文件系统Mount到Blob Storage上.我们只要添加一小段代码来表明我们希望Mount

Windows Azure入门教学系列 (一): 创建第一个WebRole程序,互联网营销

在第一篇教学中,我们将学习如何在Visual Studio 2008 SP1中创建一个WebRole程序(C#语言).在开始学习之前,请确保你的机器上已经安装了: Windows Azure Tools for Microsoft Visual Studio 1.1 (February 2010) Windows Azure Software Development Kit (November 2009) 步骤一:创建解决方案和项目 以管理员权限打开Visual Studio 2008 SP1,

Windows Azure AppFabric 入门教学系列 (六):Message Buffer之初体验

本文是Windows Azure AppFabric入门教学的第六篇文章.本文会介绍一下Service Bus中一个非常有用的特性-MessageBuffer.该特性类似于Queue Storage,作为一种小型的,临时的信息缓冲区,能够应用于多种场景.本例中,我们会简单的模拟一下订单系统:下单,查看订单,处理订单. 前置条件 为了使后续的教程能够顺利进行,请确保如下软件或组件已被安装: ·       Microsoft .NET Framework 3.5 SP1 ·       Micro