博客
关于我
自定义 Azure Table storage 查询过滤条件
阅读量:457 次
发布时间:2019-03-06

本文共 4221 字,大约阅读时间需要 14 分钟。

如何在 Azure Table Storage 中自定义查询过滤条件

作为一名开发人员,您可能已经对 Azure Table Storage 有一定的了解。如果您希望在特定条件下筛选数据,例如筛选某一天产生的所有日志,您可能会发现现有的过滤功能不够灵活。以下将详细介绍如何实现自定义过滤条件,特别是如何实现 StartsWith 过滤条件。

TableQuery 类概述

TableQuery 是本文的核心类,它用于构建和执行 Azure Table Storage 的查询。基本用法是通过 TableQuery 类创建一个查询实例,然后将该实例传递给 CloudTableExecuteQuery 方法:

var query = new TableQuery
() .Where((entity) => entity.PartitionKey == "201607");var queryResult = logTable.ExecuteQuery(query);

此外,我们还可以使用 TableQuery.CombineFilters 方法来构建更复杂的查询条件。例如,如果要同时筛选 PartitionKey 等于 "201607" 并且 RowKey 等于 "161148372454",可以这样做:

var filter = TableQuery.CombineFilters(    TableQuery.GenerateFilterCondition("PartitionKey", QueryComparisons.Equal, "201607"),    TableOperators.And,    TableQuery.GenerateFilterCondition("RowKey", QueryComparisons.Equal, "161148372454"));

运行上述代码,您会得到一个过滤字符串:"(PartitionKey eq '201607') and (RowKey eq '161148372454')"。

StartsWith 过滤条件的实现

现有的 Azure Table Storage 接口中没有直接支持 StartsWith 过滤条件的方法,因此我们需要通过自定义方式实现它。StartsWith 过滤条件的核心思想是筛选 RowKey 属性以某个特定子串开头。例如,如果要筛选 RowKey 以 "16" 开头,可以使用下面的代码:

var startStr = "16";int endIndex = startStr.Length - 1;Char lastChar = startStr[endIndex];Char afterLastChar = (char)(lastChar + 1);string endStr = startStr.Substring(0, endIndex) + afterLastChar;string startsWithCondition = TableQuery.CombineFilters(    TableQuery.GenerateFilterCondition("RowKey", QueryComparisons.GreaterThanOrEqual, startStr),    TableOperators.And,    TableQuery.GenerateFilterCondition("RowKey", QueryComparisons.LessThan, endStr));

运行上述代码,您会得到一个过滤字符串:"(RowKey ge '16') and (RowKey lt '17')"。

组合多个过滤条件

为了实现更复杂的过滤条件,我们可以使用 TableQuery.CombineFilters 方法不断地组合过滤条件。例如,如果要同时筛选 PartitionKey 为 "201607" 并且 RowKey 以 "16" 开头,可以这样做:

var filter = TableQuery.CombineFilters(    TableQuery.GenerateFilterCondition("PartitionKey", QueryComparisons.Equal, "201607"),    TableOperators.And,    "(RowKey ge '16') and (RowKey lt '17')");

运行上述代码,您会得到一个过滤字符串:"(PartitionKey eq '201607') and ((RowKey ge '16') and (RowKey lt '17'))"。

StartsWithByRowKey 类的实现

为了简化 StartsWith 过滤条件的使用,我们可以创建一个自定义类 StartsWithByRowKey,如下所示:

public class MyLogEntity : TableEntity{    public MyLogEntity() { }    public MyLogEntity(string pkey, string rkey)    {        this.PartitionKey = pkey;        this.RowKey = rkey;    }    public DateTime LogDate { get; set; }    public string LogMessage { get; set; }    public string ErrorType { get; set; }}public class StartsWithByRowKey : IQuery
>{ private readonly string partitionKey; private readonly string startsWithString; public StartsWithByRowKey(string partitionKey, string startsWithString) { this.partitionKey = partitionKey; this.startsWithString = startsWithString; } public List
Execute(CloudTable cloudTable) { var query = new TableQuery
(); int endIndex = startsWithString.Length - 1; Char lastChar = startsWithString[endIndex]; Char afterLastChar = (char)(lastChar + 1); string endStr = startsWithString.Substring(0, endIndex) + afterLastChar; string startsWithCondition = TableQuery.CombineFilters( TableQuery.GenerateFilterCondition("RowKey", QueryComparisons.GreaterThanOrEqual, startsWithString), TableOperators.And, TableQuery.GenerateFilterCondition("RowKey", QueryComparisons.LessThan, endStr) ); string filterCondition = TableQuery.CombineFilters( TableQuery.GenerateFilterCondition("PartitionKey", QueryComparisons.Equal, partitionKey), TableOperators.And, startsWithCondition ); var entities = cloudTable.ExecuteQuery(query.Where(filterCondition)); return entities.ToList(); }}public interface IQuery
{ TResult Execute(TModel model);}

应用 StartsWith 过滤条件

在实际应用中,如果您希望筛选 PartitionKey 为 "201607" 且 RowKey 以 "16" 开头,可以使用 StartsWithByRowKey 类,如下所示:

var myStartsWithQuery = new StartsWithByRowKey("201607", "16");var result = myStartsWithQuery.Execute(logTable);

通过上述代码,您可以轻松地筛选出满足特定条件的日志记录。

小结

本文介绍了如何在 Azure Table Storage 中自定义查询过滤条件,特别是如何实现 StartsWith 过滤条件。通过 TableQuery 类和 TableQuery.CombineFilters 方法,我们可以构建复杂的过滤条件,并通过自定义类 StartsWithByRowKey 来简化使用过程。希望以上内容能为您提供帮助!

转载地址:http://ddyfz.baihongyu.com/

你可能感兴趣的文章
ntko web firefox跨浏览器插件_深度比较:2019年6个最好的跨浏览器测试工具
查看>>
ntko文件存取错误_苹果推送 macOS 10.15.4:iCloud 云盘文件夹共享终于来了
查看>>
ntpdate 通过外网同步时间
查看>>
NTPD使用/etc/ntp.conf配置时钟同步详解
查看>>
NTP及Chrony时间同步服务设置
查看>>
NTP配置
查看>>
NUC1077 Humble Numbers【数学计算+打表】
查看>>
NuGet Gallery 开源项目快速入门指南
查看>>
NuGet(微软.NET开发平台的软件包管理工具)在VisualStudio中的安装的使用
查看>>
nuget.org 无法加载源 https://api.nuget.org/v3/index.json 的服务索引
查看>>
Nuget~管理自己的包包
查看>>
NuGet学习笔记001---了解使用NuGet给net快速获取引用
查看>>
nullnullHuge Pages
查看>>
NullPointerException Cannot invoke setSkipOutputConversion(boolean) because functionToInvoke is null
查看>>
null可以转换成任意非基本类型(int/short/long/float/boolean/byte/double/char以外)
查看>>
Numix Core 开源项目教程
查看>>
NumPy 或 Pandas:将数组类型保持为整数,同时具有 NaN 值
查看>>
numpy 或 scipy 有哪些可能的计算可以返回 NaN?
查看>>
numpy 数组 dtype 在 Windows 10 64 位机器中默认为 int32
查看>>
numpy 数组与矩阵的乘法理解
查看>>