简体   繁体   English

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

[英]Azure table storage query returning data from wrong partition?

I'm using azure table storage, and I'm trying to iterate over the table quickish. 我正在使用azure表存储,并且试图快速遍历表。

I must be doing it wrong, but I can't for the life of me see why; 我一定做错了,但是我不能一辈子明白为什么。

When I specify just a single partition I end up getting back results for MULTIPLE partitions. 当我只指定一个分区时,我最终将获得多个分区的结果。 Ie If I constrain the query by just using "pkey1", I get 1000 results back for "pkey1", and then 325 for "pkey2" 即,如果仅使用“ pkey1”来约束查询,则“ pkey1”将返回1000个结果,而“ pkey2”将返回325个结果

Completely confused as to how this can happen.. 关于如何发生完全困惑。

This is the code I'm using: 这是我正在使用的代码:

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());
    }

And sample calling code: 和示例调用代码:

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);

Hopefully this makes sense, and someone can help! 希望这是有道理的,并且有人可以提供帮助!

You may be running into the situation where the closure value is being modified. 您可能会遇到关闭值被修改的情况。 http://marlongrech.wordpress.com/2010/06/02/closures-in-c-can-be-evil/ This would run the query with a pKey of null, effectively not filtering by the pKey and hence returning all the values in the table. http://marlongrech.wordpress.com/2010/06/02/closures-in-c-can-be-evil/这将使用pKey为null来运行查询,实际上不会被pKey过滤,因此返回所有表中的值。

Try replacing 尝试更换

foreach (string pKey in partitionKeys)

with

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM