一,引言
上一篇文章我們在.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 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
二,正文
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
版權:轉載請在文章明顯位置註明作者及出處。如發現錯誤,歡迎批評指正。