从 Cosmosdb 获取文档 Java 需要跨分区查询但已禁用

Get Documents from Cosmosdb Java Cross partition query is required but disabled

public static Document getDocumentById(String id) {

  FeedOptions queryOptions = new FeedOptions();
     queryOptions.setMaxItemCount(10);
     queryOptions.setEnableCrossPartitionQuery(true);

  // Retrieve the document using the DocumentClient.
  List<Document> documentList = documentClient
        .queryDocuments(getTodoCollection().getSelfLink(),
              "SELECT * FROM c WHERE c.id='" + id + "'",null,queryOptions)
        .getQueryIterable().toList();

  if (documentList.size() > 0) {
     return documentList.get(0);
  } else {
     return null;
  }

您好,我正在尝试使用上面的代码通过 Java 从 Cosmosdb 检索文档方面获得一些帮助。 我收到以下错误:

WARNING: Operation will NOT be retried. Exception: Cross partition query is required but disabled. Please set x-ms-documentdb-query-enablecrosspartition to true, specify x-ms-documentdb-partitionkey, or revise your query to avoid this exception. ActivityId: 09c62e77-f9dc-4cc7-902d-0cd8c5cad8a6, Microsoft.Azure.Documents.Common/2.2.0.0

如果您能为我提供任何帮助,我们将不胜感激! 谢谢

Document DB Java SDK API Document中搜索了queryDocuments方法重载,它似乎与您的示例代码有点不同。我想你设置 FeedOptions 的参数顺序错误。请使用下面的代码,它对我有用。

import com.microsoft.azure.documentdb.*;

import java.util.List;

public class QueryDocumentsTest {

    static private String YOUR_COSMOS_DB_ENDPOINT = "https://***.documents.azure.com:443/";
    static private String YOUR_COSMOS_DB_MASTER_KEY="***";

    public static void main(String[] args) {

        DocumentClient client = new DocumentClient(
                YOUR_COSMOS_DB_ENDPOINT,
                YOUR_COSMOS_DB_MASTER_KEY,
                new ConnectionPolicy(),
                ConsistencyLevel.Session);

        FeedOptions queryOptions = new FeedOptions();
//        queryOptions.setMaxItemCount(10);
        queryOptions.setEnableCrossPartitionQuery(true);
        String id = "b01cf483-15e0-517c-deae-2e71bafe7d12";

        // Retrieve the document using the DocumentClient.
        List<Document> documentList = client
                .queryDocuments("dbs/db/colls/part",
                        "SELECT * FROM c WHERE c.id='" + id + "'",queryOptions)
                .getQueryIterable().toList();

        if (documentList.size() > 0) {
            System.out.println(documentList.get(0));
        } else {
            System.out.println("null");
        }

    }
}

输出: