gpt4 book ai didi

java - 如何在Document DB java SDK中指定NONE分区键来删除文档?

转载 作者:塔克拉玛干 更新时间:2023-11-02 19:48:56 24 4
gpt4 key购买 nike

我只有一个集合,当我尝试使用下面的代码删除文档时

    PartitionKey partitionKey = new PartitionKey("undefined");
RequestOptions requestOptions=new RequestOptions();
requestOptions.setPartitionKey(partitionKey);
for(Document currentDocument: existingIMEIDevice){
try {
ConfigVariables.documentClient.deleteDocument(currentDocument.getSelfLink(), requestOptions);
} catch (DocumentClientException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

它抛出异常。

com.microsoft.azure.documentdb.DocumentClientException: Message: {"Errors":["Resource Not Found"]} ActivityId: 4353e7c0-0b24-4b2a-8ec6-fc2db4059aa0, Request URI: /apps/708ed403-166f-44e4-847f-ccaa0cd22d9c/services/d1e2ed4d-7e69-4a3d-9575-3e24b96621b4/partitions/e3fc6138-06a5-4876-a629-a4be69917ded/replicas/131533416718986721p, StatusCode: NotFound at com.microsoft.azure.documentdb.internal.ErrorUtils.maybeThrowException(ErrorUtils.java:69) at com.microsoft.azure.documentdb.internal.GatewayProxy.performDeleteRequest(GatewayProxy.java:187) at com.microsoft.azure.documentdb.internal.GatewayProxy.doDelete(GatewayProxy.java:99) at com.microsoft.azure.documentdb.internal.GatewayProxy.processMessage(GatewayProxy.java:332) at com.microsoft.azure.documentdb.DocumentClient$7.apply(DocumentClient.java:2877) at com.microsoft.azure.documentdb.internal.RetryUtility.executeDocumentClientRequest(RetryUtility.java:58) at com.microsoft.azure.documentdb.DocumentClient.doDelete(DocumentClient.java:2883) at com.microsoft.azure.documentdb.DocumentClient.deleteDocument(DocumentClient.java:956) at com.moveinsync.centraldevices.persistance.AzureCommDAOImpl.replaceDocument(AzureCommDAOImpl.java:45) at com.moveinsync.centraldevices.persistance.AzureCommDAOImpl.documentDbBulkInsert(AzureCommDAOImpl.java:85) at com.moveinsync.centraldevices.jobs.ToAzureJob.executeInternal(ToAzureJob.java:27) at org.springframework.scheduling.quartz.QuartzJobBean.execute(QuartzJobBean.java:75) at org.quartz.core.JobRunShell.run(JobRunShell.java:202) at org.quartz.simpl.SimpleThreadPool$WorkerThread.run(SimpleThreadPool.java:573) If I do not provide RequestOptions it asks me to provide a partition key. I don't have a partition key as the below does not returns anything

SELECT c.partitionKey FROM c ORDER BY c.partitionKey

如何解决这个问题?

最佳答案

根据我的经验,如果你的集合没有分区键,那么在操作数据库时就不需要设置分区键的查询条件。

在您的帖子中,集合没有分区键,并且您将分区键设置为RequestOption。所以,数据库肯定不知道去哪里找到要操作的文档。

您可以引用我的代码片段:

import com.microsoft.azure.documentdb.*;

public class DeleteDocuments {
private static String accountName="https://jay.documents.azure.com:443/";

private static String accountKey="Czi66skfjZYLTaXuDhoxNb2JHL4DR98VxAxGXtLkWFnjCa5e7gUXQuPgemlXwyPWjjWJpwrseH1wPMfhkqA8cQ==";

private static String databaseName = "db";

private static String collectionName = "coll";

public static void main(String[] args) throws DocumentClientException {

DocumentClient client = new DocumentClient(
accountName,
accountKey
, new ConnectionPolicy(),
ConsistencyLevel.Session);

FeedOptions options = null;
String sql = "select * from c";
FeedResponse<Document> queryResults = client.queryDocuments("dbs/"+databaseName+"/colls/"+collectionName, sql, options);

System.out.println("before delete :");
for (Document d : queryResults.getQueryIterable()) {
System.out.println(String.format("\tRead %s", d));
}

RequestOptions requestOptions = new RequestOptions();
requestOptions.setOfferThroughput(400);

client.deleteDocument("/dbs/"+databaseName+"/colls/"+collectionName+"/docs/1",requestOptions);

queryResults = client.queryDocuments("dbs/"+databaseName+"/colls/"+collectionName, sql, options);


System.out.println("after delete :");

for (Document d : queryResults.getQueryIterable()) {
System.out.println(String.format("\tRead %s", d));
}
}
}
<小时/>

更新答案:

我认为你误解了 partitionkey 的含义属性(property)在options[]

例如,我的容器是这样创建的:

enter image description here

分区键是我的 Collection 的“名称”。您可以检查您的集合的分区键。

我的文件如下:

{
"id": "1",
"name": "jay"
}

{
"id": "2",
"name": "jay2"
}

我的partitionkey'name',所以这里我有两个分区:'jay''jay1'

所以,这里你应该设置partitionkey属性为“jay”或“jay2”,而不是“name”。

此时,如果我运行下面的代码而不将分区键设置到 RequestOptions 中,我将遇到与您相同的问题。

  RequestOptions requestOptions = new RequestOptions();
requestOptions.setOfferThroughput(400);

client.deleteDocument("/dbs/"+databaseName+"/colls/"+collectionName+"/docs/1",requestOptions);

Exception in thread "main" java.lang.UnsupportedOperationException: PartitionKey value must be supplied for this operation. at com.microsoft.azure.documentdb.DocumentClient.addPartitionKeyInformation(DocumentClient.java:3199) at com.microsoft.azure.documentdb.DocumentClient.addPartitionKeyInformation(DocumentClient.java:3180) at com.microsoft.azure.documentdb.DocumentClient.deleteDocument(DocumentClient.java:959) at DeleteDocuments.main(DeleteDocuments.java:32)

我需要将分区键参数设置为所操作文档存储的分区。

 RequestOptions requestOptions = new RequestOptions();
requestOptions.setOfferThroughput(400);
PartitionKey partitionKey = new PartitionKey("jay");
requestOptions.setPartitionKey(partitionKey);

client.deleteDocument("/dbs/"+databaseName+"/colls/"+collectionName+"/docs/1",requestOptions);
<小时/>

更新答案 2:

我猜你想操作没有设置分区键的文档。

请引用这个完美blog ,你会找到答案的!

在java代码中,只需将分区键设置为Undefined.Value()然后一切都会完成。

 RequestOptions requestOptions = new RequestOptions();
requestOptions.setOfferThroughput(400);
PartitionKey partitionKey = new PartitionKey(Undefined.Value());
requestOptions.setPartitionKey(partitionKey);

client.deleteDocument("/dbs/"+databaseName+"/colls/"+collectionName+"/docs/3",requestOptions);

希望对您有帮助。

关于java - 如何在Document DB java SDK中指定NONE分区键来删除文档?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47049593/

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