Azure Storage 系列(五)通过Azure.Cosmos.Table 类库在.Net 上使用 Table Storage
一,引言
上一篇文章我们在.NET 项目中添加了 “WindowsAzure.Storage” 的 NuGet 包进行操作Table 数据,但是使用的 “WindowsAzure.Storage” NeGet 以及没微遗弃了,所以我们今天继续讲 Azure Table Storage,使用新的 Nuget 包--- “Microsoft.Azure.Cosmos.Table” 来操作 Table 数据。
nuget 链接:https://www.nuget.org/packages/Microsoft.Azure.Cosmos.Table
我们可以看到 当前neget 包 允许使用 Microsoft Azure CosmosDB 表 API 以及 Azure 表存储。
--------------------我是分割线--------------------
Azure Blob Storage 存储系列:
2,Azure Storage 系列(二) .NET Core Web 项目中操作 Blob 存储
3,Azure Storage 系列(三)Blob 参数设置说明
4,Azure Storage 系列(四)在.Net 上使用Table Storage
5,Azure Storage 系列(五)通过Azure.Cosmos.Table 类库在.Net 上使用 Table Storage
6,Azure Storage 系列(六)使用Azure Queue Storage
二,正文
1,安装 “Microsoft.Azure.Cosmos.Table” 的 NuGet
使用程序包管理控制台进行安装
Install-Package Microsoft.Azure.Cosmos.Table -Version 1.0.8
2,创建ITableServiceV2 接口,和 TableServiceV2 实现类,控制器方法等
因为使用的 “Microsoft.Azure.Cosmos.Table” 的Nuget 和 “WindowsAzure.Storage” Nuget 中对 Table Storage 的操作中使用的方法,以及类都是一样的,只是命名空间不一样,所以代码上差别不太大。所以大家可以看一下具体代码和已遗弃的方法进行对比。
完整代码:
1 public interface ITableServiceV2 2 { 3 /// <summary> 4 /// AddEntity(abandoned) 5 /// </summary> 6 /// <param name="user">user</param> 7 /// <returns></returns> 8 Task AddEntity(UserInfoV2 user); 9 10 /// <summary> 11 /// BatchAddEntities(abandoned) 12 /// </summary> 13 /// <param name="users">users</param> 14 /// <returns></returns> 15 Task BatchAddEntities(List<UserInfoV2> users); 16 17 /// <summary> 18 /// QueryUsers(abandoned) 19 /// </summary> 20 /// <param name="filter">filter</param> 21 /// <returns></returns> 22 IEnumerable<UserInfoV2> QueryUsers(string filter); 23 24 /// <summary> 25 /// UpdateEntity(abandoned) 26 /// </summary> 27 /// <param name="user">user</param> 28 /// <returns></returns> 29 Task UpdateEntity(UserInfoV2 user); 30 31 /// <summary> 32 /// DeleteEntity(abandoned) 33 /// </summary> 34 /// <param name="user">user</param> 35 /// <returns></returns> 36 Task DeleteEntity(UserInfoV2 user); 37 38 /// <summary> 39 /// DeleteTable(abandoned) 40 /// </summary> 41 /// <param name="tableName">tableName</param> 42 /// <returns></returns> 43 Task DeleteTable(string tableName); 44 }
1 public class TableServiceV2 : ITableServiceV2 2 { 3 private readonly CloudStorageAccount _cloudStorageClient; 4 public TableServiceV2(CloudStorageAccount cloudStorageClient) 5 { 6 _cloudStorageClient = cloudStorageClient; 7 } 8 9 #region 01,添加表数据+async Task AddEntity(UserInfo user) 10 /// <summary> 11 /// 添加表数据 12 /// </summary> 13 /// <param name="user">用户数据</param> 14 /// <returns></returns> 15 public async Task AddEntity(UserInfoV2 user) 16 { 17 var cloudTableClient = _cloudStorageClient.CreateCloudTableClient(); 18 var cloudTable = cloudTableClient.GetTableReference("USERINFOV2"); 19 await cloudTable.CreateIfNotExistsAsync(); 20 21 var tableOperation = TableOperation.Insert(user); 22 await cloudTable.ExecuteAsync(tableOperation); 23 } 24 #endregion 25 26 public async Task BatchAddEntities(List<UserInfoV2> users) 27 { 28 var cloudTableClient = _cloudStorageClient.CreateCloudTableClient(); 29 var cloudTable = cloudTableClient.GetTableReference("USERINFOV2"); 30 await cloudTable.CreateIfNotExistsAsync(); 31 32 var tableBatchOperation = new TableBatchOperation(); 33 foreach (UserInfoV2 item in users) 34 { 35 tableBatchOperation.Insert(item); 36 } 37 38 await cloudTable.ExecuteBatchAsync(tableBatchOperation); 39 } 40 41 public async Task DeleteEntity(UserInfoV2 user) 42 { 43 var cloudTableClient = _cloudStorageClient.CreateCloudTableClient(); 44 var cloudTable = cloudTableClient.GetTableReference("USERINFOV2"); 45 46 var queryOperation = TableOperation.Retrieve<UserInfoV2>(user.PartitionKey, user.RowKey); 47 48 var tableResult = await cloudTable.ExecuteAsync(queryOperation); 49 if (tableResult.Result is UserInfoV2 userInfo) 50 { 51 var deleteOperation = TableOperation.Delete(userInfo); 52 await cloudTable.ExecuteAsync(deleteOperation); 53 } 54 } 55 56 public async Task DeleteTable(string tableName) 57 { 58 var cloudTableClient = _cloudStorageClient.CreateCloudTableClient(); 59 var cloudTable = cloudTableClient.GetTableReference(tableName); 60 await cloudTable.DeleteIfExistsAsync(); 61 } 62 63 public IEnumerable<UserInfoV2> QueryUsers(string filter) 64 { 65 var cloudTableClient = _cloudStorageClient.CreateCloudTableClient(); 66 var cloudTable = cloudTableClient.GetTableReference("USERINFOV2"); 67 68 TableQuery<UserInfoV2> query = new TableQuery<UserInfoV2>().Where(filter); 69 70 var users = cloudTable.ExecuteQuery(query); 71 foreach (var item in users) 72 { 73 yield return item; 74 } 75 } 76 77 public async Task UpdateEntity(UserInfoV2 user) 78 { 79 var cloudTableClient = _cloudStorageClient.CreateCloudTableClient(); 80 var cloudTable = cloudTableClient.GetTableReference("USERINFOV2"); 81 82 var queryOperation = TableOperation.Retrieve<UserInfoV2>(user.PartitionKey, user.RowKey); 83 84 var tableResult = await cloudTable.ExecuteAsync(queryOperation); 85 if (tableResult.Result is UserInfoV2 userInfo) 86 { 87 user.ETag = userInfo.ETag; 88 var deleteOperation = TableOperation.Replace(user); 89 await cloudTable.ExecuteAsync(deleteOperation); 90 } 91 } 92 }
1 [Route("TableV2")] 2 public class TableExplorerV2Controller : Controller 3 { 4 private readonly ITableServiceV2 _tableService; 5 6 public TableExplorerV2Controller(ITableServiceV2 tableService) 7 { 8 this._tableService = tableService; 9 } 10 11 [HttpPost("AddUser")] 12 public async Task<ActionResult> AddEntity([FromBody] UserInfoV2 user) 13 { 14 await _tableService.AddEntity(new UserInfoV2("huge", "610124199012221000") { Email = "huge@qq.com", TelNum = "13000000000" }); 15 return Ok(); 16 } 17 18 [HttpPost("AddBatchUser")] 19 public async Task<ActionResult> AddEntities([FromBody] List<UserInfoV2> users) 20 { 21 List<UserInfoV2> userList = new List<UserInfoV2>(); 22 userList.Add(new UserInfoV2("wangwei", "610124199012221001") { Email = "wangwei@qq.com", TelNum = "13000000001" }); 23 userList.Add(new UserInfoV2("wangwei", "610124199012221002") { Email = "wangwei@qq.com", TelNum = "13000000002" }); 24 userList.Add(new UserInfoV2("wangwei", "610124199012221003") { Email = "wangwei@qq.com", TelNum = "13000000003" }); 25 await _tableService.BatchAddEntities(userList); 26 return Ok(); 27 } 28 29 [HttpGet("Users")] 30 public ActionResult QueryUsers() 31 { 32 var filter = TableQuery.CombineFilters(TableQuery.GenerateFilterCondition("PartitionKey", QueryComparisons.Equal, "wangwei"), TableOperators.And, TableQuery.GenerateFilterCondition("RowKey", QueryComparisons.Equal, "610124199012221001")); 33 34 return Ok(_tableService.QueryUsers(filter)); 35 } 36 37 [HttpPut("UpdateUser")] 38 public async Task<ActionResult> UpdateUser([FromBody] UserInfoV2 user) 39 { 40 await _tableService.UpdateEntity(new UserInfoV2("huge", "610124199012221000") { Email = "huge@163.com", TelNum = "15000000000" }); 41 return Ok(); 42 } 43 44 [HttpDelete("DeleteEntity")] 45 public async Task<ActionResult> DeleteEntity([FromBody] UserInfoV2 user) 46 { 47 await _tableService.DeleteEntity(new UserInfoV2("wangwei", "610124199012221003")); 48 return Ok(); 49 } 50 51 [HttpDelete("{tableName}")] 52 public async Task<ActionResult> DeleteTable(string tableName) 53 { 54 await _tableService.DeleteTable(tableName); 55 return Ok(); 56 } 57 }
3,添加对 TableServiceV2 ,CloudStorageAccount 的注入
services.AddSingleton(x => new Microsoft.Azure.Cosmos.Table.CloudStorageAccount(new Microsoft.Azure.Cosmos.Table.StorageCredentials("cnbateblogaccount", "FU01h022mn1JjONp+ta0DAXOO7ThK3diYhd4xrm0Hpg891n9nycsTLGZXXXXnJpGvTIZvO5VCVFhGOfV0wndOOQ=="), true)); services.AddSingleton<ITableServiceV2, TableServiceV2>();
4,测试使用新的 Nuget 包中的方法是否能正常操作 Table 表数据
添加用户数据,我们在 postman 中调用添加用户表数据接口
我们可以看到,在Azure Portal 上已经创建出一个叫 “USERINFOV2” 的表信息(注意,大家不要疑惑,可以看看上面添加用户的Service方法,我这里有两行代码)
var cloudTable = cloudTableClient.GetTableReference("USERINFOV2"); await cloudTable.CreateIfNotExistsAsync();
接下来我们在Cloud Explorer 查看 “USERINFOV2” Table 的表信息
查询用户数据,我这里还是使用两个查询条件联合进行查询,分别是 “PartitionKey” 和 “RowKey” 作为查询的 Key,通过 “Partition” 等于 “wangwei” 和 “RowKey” 等于 “610124199012221001”
注意(我提前已经将调用了批量添加用户的接口,将 PartitionKey 等于 "wangwu" 的三条数据添加到 Table 中了)
OK,剩余的更新,删除就不再演示了,大家可以自行进行校验,今天的分享内容到此结束。
三,结尾
github:https://github.com/yunqian44/Azure.Storage.git
作者:Allen
版权:转载请在文章明显位置注明作者及出处。如发现错误,欢迎批评指正。