Azure 基础:自定义 Table storage 查询条件

前端之家收集整理的这篇文章主要介绍了Azure 基础:自定义 Table storage 查询条件前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

一文的基础上介绍如何自定义 Azure Table storage 的查询过滤条件。如果您还不太清楚 Azure Table storage 的基本用法,请先移步

MyLogEntity( pkey,.PartitionKey =.RowKey = }

功能方法,因此我们需要自己实现它(还好 TableQuery 的实现支持我们去扩展它)!本文将通过实现 StartsWith 过滤条件说明如何自定义 Azure Table storage 的查询过滤条件。

查询。基本用法是使用查询条件构建一个 TableQuery 类的实例,然后把这个实例作为参数传递给 CloudTable 的ExecuteQuery 方法

TableQuery query = TableQuery,QueryComparisons.Equal, queryResult = logTable.ExecuteQuery(query);

方法 CombineFilters 构建自定义查询条件。比如我们要查询 PartitionKey 等于 "201607" 并且 RowKey 等于"161148372454"的记录:

,,));

函数的返回结果为: "(PartitionKey eq '201607') and (RowKey eq '161148372454')"。函数做参数,或者设置给 query.FilterString 属性,就可以完成过滤功能了。方法可爱的地方在于我们可以不断的用它来合并查询条件,直到满意为止!

“abc” == “abc” << “abca” << “abcz” < “abd”

方法构建 StartsWith 过滤条件:

startsWithCondition =,QueryComparisons.GreaterThanOrEqual,

方法的返回值是一个字符串。运行上面的代码我们会得到字符串:

"(RowKey ge ) and (RowKey lt )"

方法:

startStr = endIndex = startStr.Length - = Char afterLastChar = ()(lastChar + endStr = startStr.Substring(,endIndex) + startsWithCondition =

方法组合了不同的过滤条件。遗憾的是 TableQuery.CombineFilters 方法只有两个参数的重载,我们不能添加更多的 TableOperators 操作。

调用 TableQuery.CombineFilters 方法去组合上一个结果和新的条件。比如我们要把 Startswith 过滤条件和 PartitionKey 过滤条件组合起来就可以这么干:

filterCondition =,

代码,生成的结果为:

(PartitionKey eq ) and ((RowKey ge ) and (RowKey lt ))

方法的主要工作就是把过滤条件组织成查询引擎能够识别的字符串。因而我们可以通过不断的叠加生成很复杂的过滤条件。

代码:

MyLogEntity( pkey,.PartitionKey =.RowKey =</span><span style="color: #0000ff"&gt;public</span> DateTime 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;; }

}<span style="color: #0000ff">public <span style="color: #0000ff">class StartsWithByRowKey : IQuery<CloudTable,List><span style="color: #000000">
{
<span style="color: #0000ff">private <span style="color: #0000ff">readonly <span style="color: #0000ff">string<span style="color: #000000"> partitionKey;
<span style="color: #0000ff">private <span style="color: #0000ff">readonly <span style="color: #0000ff">string<span style="color: #000000"> startsWithString;
<span style="color: #0000ff">internal StartsWithByRowKey(<span style="color: #0000ff">string<span style="color: #000000"> partitionKey,<span style="color: #0000ff">string<span style="color: #000000"> startsWithString)
{
<span style="color: #0000ff">this.partitionKey =<span style="color: #000000"> partitionKey;
<span style="color: #0000ff">this.startsWithString =<span style="color: #000000"> startsWithString;
}

</span><span style="color: #0000ff"&gt;public</span> List<MyLogEntity><span style="color: #000000"&gt; Execute(CloudTable coludTable)
{
    </span><span style="color: #0000ff"&gt;var</span> query = <span style="color: #0000ff"&gt;new</span> TableQuery<MyLogEntity><span style="color: #000000"&gt;();

    </span><span style="color: #0000ff"&gt;int</span> endIndex = startsWithString.Length - <span style="color: #800080"&gt;1</span><span style="color: #000000"&gt;;
    Char lastChar </span>=<span style="color: #000000"&gt; startsWithString[endIndex];
    Char afterLastChar </span>= (<span style="color: #0000ff"&gt;char</span>)(lastChar + <span style="color: #800080"&gt;1</span><span style="color: #000000"&gt;);
    </span><span style="color: #0000ff"&gt;string</span> endStr = startsWithString.Substring(<span style="color: #800080"&gt;0</span>,endIndex) +<span style="color: #000000"&gt; afterLastChar;

    </span><span style="color: #0000ff"&gt;string</span> startsWithCondition =<span style="color: #000000"&gt; TableQuery.CombineFilters(
         TableQuery.GenerateFilterCondition(</span><span style="color: #800000"&gt;"</span><span style="color: #800000"&gt;RowKey</span><span style="color: #800000"&gt;"</span><span style="color: #000000"&gt;,startsWithString),endStr)
    );

    </span><span style="color: #0000ff"&gt;string</span> filterCondition =<span style="color: #000000"&gt; TableQuery.CombineFilters(
         TableQuery.GenerateFilterCondition(</span><span style="color: #800000"&gt;"</span><span style="color: #800000"&gt;PartitionKey</span><span style="color: #800000"&gt;"</span><span style="color: #000000"&gt;,partitionKey),startsWithCondition
    );

    </span><span style="color: #0000ff"&gt;var</span> entities =<span style="color: #000000"&gt; coludTable.ExecuteQuery(query.Where(filterCondition));
    </span><span style="color: #0000ff"&gt;return</span><span style="color: #000000"&gt; entities.ToList();
}

}

<span style="color: #0000ff">public <span style="color: #0000ff">interface IQuery<<span style="color: #0000ff">in TModel,<span style="color: #0000ff">out TResult><span style="color: #000000">
{
TResult Execute(TModel model);
}

查询 PartitionKey 为"201607",RowKey 以"16"开头的记录可以这么写:

StartsWithByRowKey myStartsWithQuery = StartsWithByRowKey(, result = myStartsWithQuery.Execute(logTable);

代码简洁了很多,读起来也更清晰了(您还可以动手给 PartitionKey 也添加同样的功能)!

方法来实现自定义查询的过滤条件。对于有类似需求的朋友,希望能起到抛砖引玉的作用。

猜你在找的Azure相关文章