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

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

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(); } 

它抛出exception。

com.microsoft.azure.documentdb.DocumentClientException:消息:{“错误”:[“未找到资源”]} ActivityId:4353e7c0-0b24-4b2a-8ec6-fc2db4059aa0,请求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)com.microsoft.azure.documentdb.internal.GatewayProxy.performDeleteRequest(GatewayProxy.java:187)at com.microsoft.azure.documentdb.internal.GatewayProxy.doDelete(GatewayProxy.java:99)at com。 micros.aure.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)a t 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)位于org.quartz的org.springframework.scheduling.quartz.QuartzJobBean.exe(QuartzJobBean.java:75)的com.moveinsync.centraldevices.jobs.ToAzureJob.executeInternal(ToAzureJob.java:27)。 core.JobRunShell.run(JobRunShell.java:202)at org.quartz.simpl.SimpleThreadPool $ WorkerThread.run(SimpleThreadPool.java:573)如果我没有提供RequestOptions,它会要求我提供一个分区键。 我没有分区键,因为下面没有返回任何内容

 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 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)); } } } 

更新答案:

我认为你误解了options[]partitionkey属性的含义。

例如,我的容器创建如下:

在此处输入图像描述

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

我的文件如下:

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

我的partitionkey‘名字’ ,所以这里有两个partitionkey‘jay’‘jay1’

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

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

  RequestOptions requestOptions = new RequestOptions(); requestOptions.setOfferThroughput(400); client.deleteDocument("/dbs/"+databaseName+"/colls/"+collectionName+"/docs/1",requestOptions); 

线程“main”中的exceptionjava.lang.UnsupportedOperationException:必须为此操作提供PartitionKey值。 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)在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:

我想你想要操作没有设置分区键的文件。

请参考这个完美的博客 ,你会找到答案!

在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); 

希望它能帮到你。