gpt4 book ai didi

Azure Cosmos Db,选择行后?

转载 作者:行者123 更新时间:2023-12-05 06:32:29 26 4
gpt4 key购买 nike

我正在尝试选择 x 行之后的一些行,例如:

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

不幸的是,ROWNUM 在 azure cosmos db 中似乎未解析。

还有其他方法可以做到这一点吗?我已经考虑过使用连续 token ,但如果用户跳到第 50 页,这没有帮助,我是否需要继续使用连续 token 进行查询才能到达第 50 页?

我尝试过使用页面大小选项,但它在一次可以返回的内容数量方面存在一些限制。

最佳答案

For example I have 1,000,000 records in Azure. I want to query rows 500,000 to 500,010. I can't do SELECT * from collection WHERE ROWNUM >= 500,000 and ROWNUM <= 500,010 so how do I achieve this?

如果您没有任何过滤器,则目前无法通过直接在 cosmos db 中查询 sql 检索特定范围内的项目。因此,您需要使用分页来定位您想要的项目。据我所知,到目前为止,仅基于延续 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()? ] 到目前为止在 Cosmos 数据库中。我也想过跳过和顶部。但是,top已支持并跳过( feedback )。看来跳过已经在处理中,并将在将来发布。

我认为您可以推送与分页功能相关的反馈。或者暂时将上面的延续 token 作为解决方法。

关于Azure Cosmos Db,选择行后?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51254389/

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