上一篇文章咱們在.NET 項目中添加了 「WindowsAzure.Storage」 的 NuGet 包進行操做Table 數據,可是使用的 「WindowsAzure.Storage」 NeGet 以及沒微遺棄了,因此咱們今天繼續講 Azure Table Storage,使用新的 Nuget 包--- 「Microsoft.Azure.Cosmos.Table」 來操做 Table 數據。html
nuget 連接:https://www.nuget.org/packages/Microsoft.Azure.Cosmos.Tablegit
咱們能夠看到 當前neget 包 容許使用 Microsoft Azure CosmosDB 表 API 以及 Azure 表存儲。github
--------------------我是分割線--------------------bash
Azure Blob Storage 存儲系列:async
2,Azure Storage 系列(二) .NET Core Web 項目中操做 Blob 存儲post
3,Azure Storage 系列(三)Blob 參數設置說明測試
4,Azure Storage 系列(四)在.Net 上使用Table Storagethis
5,Azure Storage 系列(五)經過Azure.Cosmos.Table 類庫在.Net 上使用 Table Storage spa
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 }
services.AddSingleton(x => new Microsoft.Azure.Cosmos.Table.CloudStorageAccount(new Microsoft.Azure.Cosmos.Table.StorageCredentials("cnbateblogaccount", "FU01h022mn1JjONp+ta0DAXOO7ThK3diYhd4xrm0Hpg891n9nycsTLGZXXXXnJpGvTIZvO5VCVFhGOfV0wndOOQ=="), true)); services.AddSingleton<ITableServiceV2, TableServiceV2>();
添加用戶數據,咱們在 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
版權:轉載請在文章明顯位置註明做者及出處。如發現錯誤,歡迎批評指正。