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

你可能感兴趣的文章
nio 中channel和buffer的基本使用
查看>>
NIO三大组件基础知识
查看>>
NIO与零拷贝和AIO
查看>>
NIO同步网络编程
查看>>
NIO基于UDP协议的网络编程
查看>>
NIO笔记---上
查看>>
NIO蔚来 面试——IP地址你了解多少?
查看>>
NISP一级,NISP二级报考说明,零基础入门到精通,收藏这篇就够了
查看>>
NISP国家信息安全水平考试,收藏这一篇就够了
查看>>
NIS服务器的配置过程
查看>>
Nitrux 3.8 发布!性能全面提升,带来非凡体验
查看>>
NiuShop开源商城系统 SQL注入漏洞复现
查看>>
NI笔试——大数加法
查看>>
NLog 自定义字段 写入 oracle
查看>>
NLog类库使用探索——详解配置
查看>>
NLP 基于kashgari和BERT实现中文命名实体识别(NER)
查看>>
NLP 模型中的偏差和公平性检测
查看>>
Vue3.0 性能提升主要是通过哪几方面体现的?
查看>>
NLP 项目:维基百科文章爬虫和分类【01】 - 语料库阅读器
查看>>
NLP_什么是统计语言模型_条件概率的链式法则_n元统计语言模型_马尔科夫链_数据稀疏(出现了词库中没有的词)_统计语言模型的平滑策略---人工智能工作笔记0035
查看>>