Azure   发布时间:2019-11-03  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了Azure 基础:Table storage大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。

Azure Storage 是微软 Azure 云提供的云端存储解决方案,当前支持的存储类型有 Blob、Queue、File 和 Table。其中的 Table 就是本文的主角 Azure Table storage。

title="Azure 基础:Table storage" alt="Azure 基础:Table storage" src="https://cn.js-code.com/res/2019/02-09/09/3a0ee3296ffd852a7752bcce0080da24.png" >

Azure Table storage 是一个在云端存储结构化 NoSQL 数据的服务。它不仅存取速度快,而且效费比高。MSDN 上的说法是:成本显著低于传统 SQL!笔者最近在项目中用 Table storage 实现了一个日志表,在此和大家分享一下 Table storage 的基本用法。

Azure storage account

就概念上来讲,Table storage 只是 Azure 提供的存储服务的一种。其他的存储服务还有 Blob、Queue、File 等。对这些存储服务的访问控制都是通过 storage account 来进行的。所以要想使用 Table storage 需要先创建你的 storage account。具体创建过程不是本文重点,请参 MSDN。但你需要去了解一下 Access keys,它就是你访问 storage account 的用户名和密码

title="Azure 基础:Table storage" alt="Azure 基础:Table storage" src="https://cn.js-code.com/res/2019/02-09/09/fb0cd8e4dcdd1d21d34bc05f926ef73c.png" >

创建 Table storage 的对象

在使用 Azure Table storage 的相关对象前,我们需要安装对应的包。其实很简单,只需在 Visual studio 的 Package Manager Console 中输入:

Install-Package WindowsAzure.Storage

Visual studio 会自动安装 WindowsAzure.Storage 包及其依赖的所有包,安装完成后的 packages.config 文件看起来像这个样子:

title="Azure 基础:Table storage" alt="Azure 基础:Table storage" src="https://cn.js-code.com/res/2019/02-09/09/7cc0586cc31c2e7e0cb3b9814f0a0bc7.png" >

安装完相关的包以后,我们就可以使用其中的类型了。

CloudStorageAccount 类表示一个 Azure storage account,我们得先创建它的实例才能访问属于它的资源。

CloudStorageAccount storageAccount = CloudStorageAccount.Parse(https;AccountName=xxx;AccountKey=yyy);

<span style="color: #008000">//<span style="color: #008000"> CloudTableClient 类是 Windows Azure Table service 客户端的逻辑表示。我们使用它来配置和执行对 Table storage 的操作。
CloudTableClient cloudTableClient = storageAccount.CreateCloudTableClient();

CloudTable 类表示一张数据表。

CloudTable logTable = cloudTableClient.GetTableReference(); logTable.CreateIfNotExists();

这样在后面的操作中就可以确保 MyLogTable 表是存在的。

有了 logTable 对象我们就可以向表中插入数据了。但是等等,好像少了点什么。我们开篇第一句中就说明了,Table storage 存储的是结构化的数据,所以我们还要先定义存储的数据的类型。

定义日志类

在定义我们自己的数据类型时,有一个强制性的要求,必须继承自 TableEntity 类型:

{ { } MyLogEntity(String pkey,String{ .PartitionKey =.RowKey =</span><span style="color: #0000ff"&gt;public</span> datetiR_686_11845@e LogDate { <span style="color: #0000ff"&gt;get</span>; <span style="color: #0000ff"&gt;set</span><span style="color: #000000"&gt;; } </span><span style="color: #0000ff"&gt;public</span> <span style="color: #0000ff"&gt;String</span> Logmessage { <span style="color: #0000ff"&gt;get</span>; <span style="color: #0000ff"&gt;set</span><span style="color: #000000"&gt;; } </span><span style="color: #0000ff"&gt;public</span> <span style="color: #0000ff"&gt;String</span> ErrorType { <span style="color: #0000ff"&gt;get</span>; <span style="color: #0000ff"&gt;set</span><span style="color: #000000"&gt;; }

}

在我们的设计中,PartitionKey 用来存放产生日志的年份和月份(例如201607),RowKey 用来存放产生日志的天和时分秒毫秒(例如160934248492)。日志数据主要是 LogDate,Logmessage 和 ErrorType。

把数据插入到 Table storage

终于可以向表中插入数据了,试一下先:

datetiR_686_11845@e now =datetiR_686_11845@e.Now; String partitionKey = now.ToString(ymM); String rowKey = now.ToString(); MyLogEntity logEntity = ); logEntity.LogDate =message = message= Leoperation 类表示对一个表进行的操作,可以插入一行或多行数据,删除数据,更新数据等 TabLeoperation insertOperation =Leoperation.Insert(logEntity); logTable.Execute(insertOperation);

看起来还不错,我们用 Visual studio 自带的 Cloud Explorer 查看一下 MyLogTable 中的内容:

title="Azure 基础:Table storage" alt="Azure 基础:Table storage" src="https://cn.js-code.com/res/2019/02-09/09/18921408308d30411cb707a1134246fd.png" >

OK,数据已经成功插入到 MyLogTable 表中。接下来我们看看如何批量的插入数据。

TableBatchOperation batchOperation = ); ( i = ; i < ; i++{ datetiR_686_11845@e now =datetiR_686_11845@e.Now; String partitionKey = now.ToString(ymM); String rowKey = now.ToString(); MyLogEntity logEntity = ); logEntity.LogDate =message = message +String(); logEntity.ErrorType = (i%) == ? : ); Thread.Sleep(); } logTable.ExecuteBatch(batchOperation);

这次我们把 TabLeoperation 类换成了 TableBatchOperation 类,然后一次插入十条数据。去检查一下结果,OK 十条数据全部插入成功!下面让我们把循环中的 10 改成 200 试试:

title="Azure 基础:Table storage" alt="Azure 基础:Table storage" src="https://cn.js-code.com/res/2019/02-09/09/84197ad55de8bbf1d6f83c0d0ed00c76.png" >

怎么收到一个 InvalidoperationException 呢?看看红框中的内容,原来批量操作是有一些限制的:1.    每个批量操作的数据上限是 100 条记录。2.    每个批量操作中的数据都必须保持相同的 Partition Key请大家在使用批量操作时务必注意这些限制条件!

查询操作

对于日志数据的操作,最重要的就是查询。我们通过几个具体的用例来介绍 Table storage 的查询操作。

查询所有的记录@H_811_262@

这是最简单的查询方法,一般是想要导出全部数据时才会这么干:

TableQuery query = TableQuery); (MyLogEntity entity { Console.WriteLine({0}\t{1}\t{2}\t{3}message,entity.ErrorTypE); }

查询某年的某个月的记录@H_811_262@

要查询某个月的所有记录也是比较容易的,因为我们设计的 PartitionKey 就代表了某个月份:

TableQuery query = TableQuery,QueryComparisons.Equal,607); (MyLogEntity entity { }

请注意 TableQuery.GenerateFilterCondition 方法,我们创建了一个过滤条件:PartitionKey 等于 "201607"。这个查询会把所有 PartitionKey 为 "201607" 的记录都找到!

查询某一条记录@H_811_262@

如果我们已经知道了一条记录的 PartitionKey 和 RowKey,就可以通过这两个条件直接查询到这条记录的详情:

TableQuery query = TableQuery,607Leoperators.And,TableQuery.GenerateFilterCondition(,); (MyLogEntity entity { }

这次我们使用了组合条件。然这里只使用了条件运算操作 TabLeoperators.And 和 QueryComparisons.Equal,你完全可尝试其它的条件类型。唯一要注意的是:对于 PartitionKey 和 RowKey,QueryComparisons 的操作对象都是字符串。

我们还需要更多的查询条件,比如查询某一天产生的所有日志。在 MyLogTable 表中,这需要查询以 "xx" 字符串开头的 RowKey。我会单独在一篇文章中和大家分享相关内容,因为它并不像看起来的那么简单。接下来我们介绍如何更新和删除日志表中的数据,当然这么做并不恰当,我们这里只是借用日志表介绍更新和删除操作而已。

更新记录

TabLeoperation retrieveOperation = TabLeoperation.Retrieve(607,); TableResult retrievedResult =); MyLogEntity updateEntity =<span style="color: #0000ff">if (updateEntity != <span style="color: #0000ff">null<span style="color: #000000">)
{
updateEntity.Logmessage
= <span style="color: #800000">"
<span style="color: #800000">new log message
<span style="color: #800000">"<span style="color: #000000">;
TabLeoperation updateOperation =<span style="color: #000000"> TabLeoperation.replace(updateEntity);
logTable.Execute(updateOperation);
}

这次我们先用 TabLeoperation.Retrieve 方法获得一条数据的详情,然后更新它的 Logmessage 属性,最后使用 TabLeoperation.replace 方法把新的内容更新的到 Table storage 中。

删除记录

实际上删除一条记录和更新一条记录一样麻烦,不同点是把 TabLeoperation.replace 方法换成 TabLeoperation.delete 方法:

TabLeoperation retrieveOperation = TabLeoperation.Retrieve(607,); TableResult retrievedResult =); MyLogEntity deleteEntity = (deleteEntity != { TabLeoperation deleteOperation =Leoperation.delete(deleteEntity); logTable.Execute(deleteOperation); }

删除表

删除表和创建表一样简单(可比删除一条记录容易多了):

logTable.deleteIfExists();

总结

本文通过对一个日志表的操作介绍了 Azure Table storage 的一个典型应用场景和基本的使用方法。从操作的代码上看和传统的 sql 表操作差别还是挺大的。希望对朋友们了解 Azure Table storage 能有所帮助。

大佬总结

以上是大佬教程为你收集整理的Azure 基础:Table storage全部内容,希望文章能够帮你解决Azure 基础:Table storage所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。
标签: