gpt4 book ai didi

c# - Azure 表存储查询从错误分区返回数据?

转载 作者:太空宇宙 更新时间:2023-11-03 14:02:25 25 4
gpt4 key购买 nike

我正在使用 azure 表存储,并且我正在尝试快速迭代该表。

我一定做错了,但我怎么也看不出为什么;

当我只指定一个分区时,我最终会得到多个分区的结果。IE。如果我仅使用“pkey1”来限制查询,则“pkey1”会返回 1000 个结果,“pkey2”会返回 325 个结果

完全搞不懂这是怎么发生的..

这是我正在使用的代码:

private CloudTableClient _client;
private string _tableName;
private class QueryState
{
public CloudTableQuery<T> Ctq;
public Action<IEnumerable<T>> Populator;
public ManualResetEvent Mre;
public string Pkey;

public QueryState(CloudTableQuery<T> ctq, Action<IEnumerable<T>> populator, ManualResetEvent mre, string pkey)
{
Populator = populator;
Ctq = ctq;
Mre = mre;
Pkey = pkey;
}
}

public void ParallelQueryWithClause(Action<IEnumerable<T>> populator, string[] partitionKeys)
{
List<ManualResetEvent> mre = new List<ManualResetEvent>();
foreach (string pKey in partitionKeys)
{
//_retry.Go(tsc =>
// {
TableServiceContext tsc = _client.GetDataServiceContext();
ManualResetEvent m = new ManualResetEvent(false);
mre.Add(m);
CloudTableQuery<T> query = tsc.CreateQuery<T>(_tableName).Where(e => e.PartitionKey == pKey).AsTableServiceQuery<T>();
Action<IAsyncResult> act = null;
act = result =>
{
int retries = 0;
while (retries++ < 5)
{
try
{
QueryState qsInternal = result.AsyncState as QueryState;
CloudTableQuery<T> ctq = qsInternal.Ctq;
ResultSegment<T> seg = ctq.EndExecuteSegmented(result);
if (seg.Results.Count() > 0)
populator(seg.Results);
if (seg.ContinuationToken != null)
{
ctq.BeginExecuteSegmented(seg.ContinuationToken, iasync => act(iasync), qsInternal);
}
else
{
m.Set();
}
break;
}
catch(Exception ex)
{
Logger.LogError(ex);
}
}
};
query.BeginExecuteSegmented(iasync => act(iasync), new QueryState(query, populator, m, pKey));
//});
}
ManualResetEvent.WaitAll(mre.ToArray());
}

以及示例调用代码:

AzureTableStorage<ProductEntity> _ats = new AzureTableStorage<ProductEntity>("Products");
string[] partitions = new string[] { "pkey1" };

Dictionary<string, int> cntr = new Dictionary<string, int>();
_ats.ParallelQueryWithClause(p =>
{
lock (cntr)
{
foreach (ProductEntity pe in p)
{
if (cntr.ContainsKey(pe.PartitionKey))
cntr[pe.PartitionKey]++;
else
cntr.Add(pe.PartitionKey, 1);
}
}
}, partitions);

希望这是有道理的,并且有人可以提供帮助!

最佳答案

您可能会遇到关闭值被修改的情况。 http://marlongrech.wordpress.com/2010/06/02/closures-in-c-can-be-evil/这将使用 null 的 pKey 运行查询,实际上不会通过 pKey 进行过滤,从而返回表中的所有值。

尝试更换

foreach (string pKey in partitionKeys)

foreach (string pKeyTmp in partitionKeys)
{
string pKey = pKeyTmp;

关于c# - Azure 表存储查询从错误分区返回数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10368643/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com