Reputation: 35
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;
}
Hello, I am trying to get some help with retrieving a document from Cosmosdb using Java using the code above. I am getting the following error:
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
Any help you can provide me would be appreciated! Thanks
Upvotes: 2
Views: 1252
Reputation: 23782
Searched for the queryDocuments
method overloads from Document DB Java SDK API Document,it seems a little bit different from your sample code. I suppose you set the FeedOptions
at the wrong order of parameters. Please use below code and it works for me.
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");
}
}
}
Output:
Upvotes: 1