使用C#操作Azure Table Storage

AzureTable Storage 是一种用于存储大量结构化数据的服务,这些数据没有复杂的关系,并且是无模式的。存储的数据是持久的,高度可扩展的,并且可以快速检索。本文将演示如何使用C#创建一个新的Azure存储表,对表进行CRUD操作(读取、插入、更新和删除数据),以及如何删除创建的表。

表存储数据存储在跨多个存储节点的分区中。每个存储的数据实体都有一个与之关联的行键、分区键和时间戳。每个分区由一个分区键标识。在分区中,每个数据实体都与一个行键关联。分区键和行键的组合唯一地标识了一个数据实体。与数据实体相关联的时间戳跟踪了数据实体最后一次被修改的时间。行键和分区键可以由开发人员修改。时间戳由服务器管理,开发人员无法修改。

使用代码

为了演示本文中的表存储操作,使用Visual Studio 2015创建了一个控制台应用程序。通过Nuget包管理器在控制台应用程序中安装了WindowsAzureStorage SDK。

创建了一个名为Customer的数据实体类。这个实体数据将被保存到表存储中。Customer类是从Microsoft.WindowsAzure.Storage.Table命名空间中的TableEntity类派生的。表存储中的每个实体数据都必须与一个分区键和行键关联。AssignPartitionKey方法将客户类型分配为分区键,AssignRowKey方法将客户ID分配为行键。

public class Customer : TableEntity { private int customerID; private string customerName; private string customerDetails; private string customerType; public void AssignRowKey() { this.RowKey = customerID.ToString(); } public void AssignPartitionKey() { this.PartitionKey = customerType; } public int CustomerID { get { return customerID; } set { customerID = value; } } public string CustomerName { get { return customerName; } set { customerName = value; } } public string CustomerDetails { get { return customerDetails; } set { customerDetails = value; } } public string CustomerType { get { return customerType; } set { customerType = value; } } }

表存储的连接字符串包含存储帐户的名称和访问密钥,这些信息可以从Azure门户中获取。

<add key="StorageConnectionString" value="DefaultEndpointsProtocol=https;AccountName=[存储帐户名称];AccountKey=[存储帐户访问密钥]">

CloudStorageAccount.Parse方法解析连接字符串并返回一个云存储帐户对象。可以使用CloudTableClient类访问表存储中的表和实体。GetTableReference方法接受表名作为参数并返回对云表的引用。CreateIfNotExists方法在表不存在时创建一个新表。

CloudStorageAccount cloudStorageAccount = CloudStorageAccount.Parse(ConfigurationManager.AppSettings["StorageConnectionString"]); CloudTableClient tableClient = cloudStorageAccount.CreateCloudTableClient(); Console.WriteLine("Enter Table Name to create"); string tableName = Console.ReadLine(); CloudTable cloudTable = tableClient.GetTableReference(tableName); CreateNewTable(cloudTable); public static void CreateNewTable(CloudTable table) { if (!table.CreateIfNotExists()) { Console.WriteLine("Table {0} already exists", table.Name); return; } Console.WriteLine("Table {0} created", table.Name); }

TableOperation.Insert方法接受customer实体作为输入并返回需要对表执行的TableOperation对象。RetrieveRecord方法搜索并获取正在插入的客户实体数据。如果相同的客户数据实体已经存在,应该不执行插入操作。它是分区,通过调用customer实体类中的AssignPartitionKey和AssignRowKey方法来分配行键和分区键给数据记录。

public static void InsertRecordToTable(CloudTable table) { Console.WriteLine("Enter customer type"); string customerType = Console.ReadLine(); Console.WriteLine("Enter customer ID"); string customerID = Console.ReadLine(); Console.WriteLine("Enter customer name"); string customerName = Console.ReadLine(); Console.WriteLine("Enter customer details"); string customerDetails = Console.ReadLine(); Customer customerEntity = new Customer(); customerEntity.CustomerType = customerType; customerEntity.CustomerID = Int32.Parse(customerID); customerEntity.CustomerDetails = customerDetails; customerEntity.CustomerName = customerName; customerEntity.AssignPartitionKey(); customerEntity.AssignRowKey(); Customer custEntity = RetrieveRecord(table, customerType, customerID); if (custEntity == null) { TableOperation tableOperation = TableOperation.Insert(customerEntity); table.Execute(tableOperation); Console.WriteLine("Record inserted"); } else { Console.WriteLine("Record exists"); } }

RetrieveRecord方法查询表并返回与正在查询的行键和分区键匹配的客户实体。

public static Customer RetrieveRecord(CloudTable table, string partitionKey, string rowKey) { TableOperation tableOperation = TableOperation.Retrieve(partitionKey, rowKey); TableResult tableResult = table.Execute(tableOperation); return tableResult.Result as Customer; }

实体记录通过分区键和行键的组合唯一地标识。在更新记录之前,使用RetrieveRecord方法通过传递分区键和行键作为参数在表中搜索它。如果实体存在,则只执行更新操作。TableOperation.Replace方法执行更新操作。

public static void UpdateRecordInTable(CloudTable table) { Console.WriteLine("Enter customer type"); string customerType = Console.ReadLine(); Console.WriteLine("Enter customer ID"); string customerID = Console.ReadLine(); Console.WriteLine("Enter customer name"); string customerName = Console.ReadLine(); Console.WriteLine("Enter customer details"); string customerDetails = Console.ReadLine(); Customer customerEntity = RetrieveRecord(table, customerType, customerID); if (customerEntity != null) { customerEntity.CustomerDetails = customerDetails; customerEntity.CustomerName = customerName; TableOperation tableOperation = TableOperation.Replace(customerEntity); table.Execute(tableOperation); Console.WriteLine("Record updated"); } else { Console.WriteLine("Record does not exists"); } }

创建并执行TableQuery对象。

public static void DisplayTableRecords(CloudTable table) { TableQuery tableQuery = new TableQuery(); foreach (Customer customerEntity in table.ExecuteQuery(tableQuery)) { Console.WriteLine("Customer ID : {0}", customerEntity.CustomerID); Console.WriteLine("Customer Type : {0}", customerEntity.CustomerType); Console.WriteLine("Customer Name : {0}", customerEntity.CustomerName); Console.WriteLine("Customer Details : {0}", customerEntity.CustomerDetails); Console.WriteLine("******************************"); } }

TableOperation.Delete创建要执行的表删除操作对象。要删除的记录通过行键和分区键标识。如果记录存在,则仅删除它。

public static void DeleteRecordinTable(CloudTable table) { Console.WriteLine("Enter customer type"); string customerType = Console.ReadLine(); Console.WriteLine("Enter customer ID"); string customerID = Console.ReadLine(); Customer customerEntity = RetrieveRecord(table, customerType, customerID); if (customerEntity != null) { TableOperation tableOperation = TableOperation.Delete(customerEntity); table.Execute(tableOperation); Console.WriteLine("Record deleted"); } else { Console.WriteLine("Record does not exists"); } } public static void DropTable(CloudTable table) { if (!table.DeleteIfExists()) { Console.WriteLine("Table does not exists"); } }
沪ICP备2024098111号-1
上海秋旦网络科技中心:上海市奉贤区金大公路8218号1幢 联系电话:17898875485