Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
147 views
in Technique[技术] by (71.8m points)

c# - How do I use Azure TableClient 2.0 BeginExecuteQuerySegmented

I'm trying to get all entries in my table asynchronously but am unable to figure out how to work with the continuation token. I suspect I need to take my anonymous method and convert it to a delegate, then recursively call it with the continuation token.

How do I take the following code and perform an Async call and fetch all entries in the new API?

 Task<string[]> GetAllTableEntries(CloudTable tbl, string[] urls, string name, CancellationToken token)
    {
        TableRequestOptions reqOptions = new TableRequestOptions() { };
        OperationContext ctx = new OperationContext() { ClientRequestID = "" };
        object state = null;

        // Register Cancelation Token
        ICancellableAsyncResult result = null;

        TableQuery qry = new TableQuery();
        TableContinuationToken tok = null;

        result = tbl.BeginExecuteQuerySegmented(qry, tok, reqOptions, ctx, (o) =>
        {

            var response = (o.AsyncState as CloudTable).EndExecuteQuerySegmented(o);

            Console.WriteLine("Found " + response.Results.Count + " records");

            // The following code was used in the previous version of the SDK
            //
            //26:                      // add first segment of data
            //27:                      pageData.CompletedList.AddRange(
            //28:                          from wu in response.Results
            //29:                          select new CompletedWorkUnit(wu));
            //30:   
            //31:                      // continue fetching segments to complete page
            //32:                      while (response.HasMoreResults)
            //33:                      {
            //34:                          response = response.GetNext();
            //35:                          pageData.CompletedList.AddRange(
            //36:                              from wu in response.Results
            //37:                              select new CompletedWorkUnit(wu));
            //38:                      }
            //39:   
            //40:                      // set continuation token for next page request
            //41:                      pageData.ContinuationToken = response.ContinuationToken;
            //42:                      evt.Set();

        }, state);

        // Add cancellation token according to guidance from Table Client 2.0 Breaking Changes blog entry
        token.Register((o) => result.Cancel(), state);
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Please try this:

    static void ExecuteQuery()
    {
        TableContinuationToken token = null;
        TableRequestOptions reqOptions = new TableRequestOptions() { };
        OperationContext ctx = new OperationContext() { ClientRequestID = "" };
        long totalEntitiesRetrieved = 0;
        while (true)
        {
            CloudTable table = cloudTableClient.GetTableReference("MyTable");
            TableQuery<TempEntity> query = (new TableQuery<TempEntity>()).Take(100);
            System.Threading.ManualResetEvent evt = new System.Threading.ManualResetEvent(false);
            var result = table.BeginExecuteQuerySegmented<TempEntity>(query, token, reqOptions, ctx, (o) =>
            {
                var response = (o.AsyncState as CloudTable).EndExecuteQuerySegmented<TempEntity>(o);
                token = response.ContinuationToken;
                int recordsRetrieved = response.Count();
                totalEntitiesRetrieved += recordsRetrieved;
                Console.WriteLine("Records retrieved in this attempt = " + recordsRetrieved + " | Total records retrieved = " + totalEntitiesRetrieved);
                evt.Set();
            }, table);
            evt.WaitOne();
            if (token == null)
            {
                break;
            }
        }
    }

One thing I noticed is that if I execute a query which returns a dynamic table entity, I'm getting an error related to DateTimeOffset. That's why I ended up creating a temporary entity.

Hope this helps.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...