博客
关于我
自定义 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/

你可能感兴趣的文章
numpy转PIL 报错TypeError: Cannot handle this data type
查看>>
Numpy闯关100题,我闯了95关,你呢?
查看>>
nump模块
查看>>
Nutch + solr 这个配合不错哦
查看>>
NuttX 构建系统
查看>>
NutUI:京东风格的轻量级 Vue 组件库
查看>>
NutzCodeInsight 2.0.7 发布,为 nutz-sqltpl 提供友好的 ide 支持
查看>>
NutzWk 5.1.5 发布,Java 微服务分布式开发框架
查看>>