代码之家  ›  专栏  ›  技术社区  ›  meds

azure cosmos db,排后选择?

  •  2
  • meds  · 技术社区  · 7 年前

    我试图在x行之后选择一些行,例如:

    SELECT * from collection WHERE ROWNUM >= 235 and ROWNUM <= 250
    

    不幸的是,看起来rownum没有在azure cosmos db中解析。

    还有别的办法吗?我已经研究过使用continuation令牌,但是如果用户跳到第50页是没有帮助的,我需要继续使用continuation令牌进行查询才能到达第50页吗?

    我试过使用页面大小选项,但这在一次可以返回多少内容方面有一些限制。

    1 回复  |  直到 7 年前
        1
  •  1
  •   Jay Gong    7 年前

    例如,我在azure中有1000000条记录。我想查询行 500000至500010。我无法从rownum>=500000和rownum<=500010的集合中选择*,所以如何实现这一点?

    如果没有任何过滤器,到目前为止,您无法通过cosmos db中的query sql direcly检索特定范围内的项。所以,你需要使用分页来定位你想要的物品。我知道, pagination 基于 continuation token 只是到目前为止。

    请参阅以下功能:

    using JayGongDocumentDB.pojo;
    using Microsoft.Azure.Documents.Client;
    using Microsoft.Azure.Documents.Linq;
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Threading.Tasks;
    
    namespace JayGongDocumentDB.module
    {
        class QuerySample1
        {
            public static async void QueryPageByPage()
            {
                // Number of documents per page
                const int PAGE_SIZE = 2;
    
                int currentPageNumber = 1;
                int documentNumber = 1;
    
                // Continuation token for subsequent queries (NULL for the very first request/page)
                string continuationToken = null;
    
                do
                {
                    Console.WriteLine($"----- PAGE {currentPageNumber} -----");
    
                    // Loads ALL documents for the current page
                    KeyValuePair<string, IEnumerable<Student>> currentPage = await QueryDocumentsByPage(currentPageNumber, PAGE_SIZE, continuationToken);
    
                    foreach (Student student in currentPage.Value)
                    {
                        Console.WriteLine($"[{documentNumber}] {student.Name}");
                        documentNumber++;
                    }
    
                    // Ensure the continuation token is kept for the next page query execution
                    continuationToken = currentPage.Key;
                    currentPageNumber++;
                } while (continuationToken != null);
    
                Console.WriteLine("\n--- END: Finished Querying ALL Dcuments ---");
            }
    
    
            public static async Task<KeyValuePair<string, IEnumerable<Student>>> QueryDocumentsByPage(int pageNumber, int pageSize, string continuationToken)
            {
                DocumentClient documentClient = new DocumentClient(new Uri("https://***.documents.azure.com:443/"), "***");
    
                var feedOptions = new FeedOptions
                {
                    MaxItemCount = pageSize,
                    EnableCrossPartitionQuery = true,
    
                    // IMPORTANT: Set the continuation token (NULL for the first ever request/page)
                    RequestContinuation = continuationToken
                };
    
                IQueryable<Student> filter = documentClient.CreateDocumentQuery<Student>("dbs/db/colls/item", feedOptions);
                IDocumentQuery<Student> query = filter.AsDocumentQuery();
    
                FeedResponse<Student> feedRespose = await query.ExecuteNextAsync<Student>();
    
                List<Student> documents = new List<Student>();
                foreach (Student t in feedRespose)
                {
                    documents.Add(t);
                }
    
                // IMPORTANT: Ensure the continuation token is kept for the next requests
                return new KeyValuePair<string, IEnumerable<Student>>(feedRespose.ResponseContinuation, documents);
            }
        }
    }
    

    输出:

    enter image description here

    希望对你有帮助。


    更新答案:

    没有这样的功能 ROW_NUMBER() [ How do I use ROW_NUMBER()? ]在宇宙数据库里。我还以为史基普和托普。但是, top 支持并跳过( feedback )。看来skip已经在处理中,将来会发布。

    我想你可以推送与寻呼功能相关的反馈。 延续标记 作为临时解决方案。

    推荐文章