如何在document DB java SDK中指定用于删除文档的无分区键?

如何在document DB java SDK中指定用于删除文档的无分区键?,java,azure,azure-storage,azure-cosmosdb,Java,Azure,Azure Storage,Azure Cosmosdb,我只有一个集合,当我尝试使用下面的代码删除文档时 PartitionKey partitionKey = new PartitionKey("undefined"); RequestOptions requestOptions=new RequestOptions(); requestOptions.setPartitionKey(partitionKey); for(Document currentDocument: existingIMEIDevice){

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

    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:消息:{“错误”:[“未找到资源”]} 活动ID:4353e7c0-0b24-4b2a-8ec6-fc2db4059aa0,请求URI:/apps/708ed403-166f-44e4-847f-ccaa0cd22d9c/服务/d1e2ed4d-7e69-4a3d-9575-3e24b96621b4/分区/e3fc6138-06a5-4876-a629-a4be69917ded/副本/131533416718986721p,状态代码:未找到 位于com.microsoft.azure.documentdb.internal.ErrorUtils.maybeThrowException(ErrorUtils.java:69) 位于com.microsoft.azure.documentdb.internal.GatewayProxy.performDeleteRequest(GatewayProxy.java:187) 位于com.microsoft.azure.documentdb.internal.GatewayProxy.dodelite(GatewayProxy.java:99) 位于com.microsoft.azure.documentdb.internal.GatewayProxy.processMessage(GatewayProxy.java:332) 位于com.microsoft.azure.documentdb.DocumentClient$7.apply(DocumentClient.java:2877) 位于com.microsoft.azure.documentdb.internal.RetryUtility.ExecutedDocumentClientRequest(RetryUtility.java:58) 位于com.microsoft.azure.documentdb.DocumentClient.dodelite(DocumentClient.java:2883) 位于com.microsoft.azure.documentdb.DocumentClient.deleteDocument(DocumentClient.java:956) 在com.moveinsync.centraldevices.persistence.AzureCommDAOImpl.replaceDocument上(AzureCommDAOImpl.java:45) 在com.moveinsync.centraldevices.persistence.AzureCommDAOImpl.documentDbBulkInsert上(AzureCommDAOImpl.java:85) 在com.moveinsync.centraldevices.jobs.ToAzureJob.executeInternal(ToAzureJob.java:27) 位于org.springframework.scheduling.quartz.QuartzJobBean.execute(QuartzJobBean.java:75) 位于org.quartz.core.JobRunShell.run(JobRunShell.java:202) 位于org.quartz.siml.SimpleThreadPool$WorkerThread.run(SimpleThreadPool.java:573) 如果我不提供RequestOptions,它会要求我提供一个分区键。 我没有分区键,因为下面的代码不返回任何内容


如何解决此问题?

根据我的经验,如果您的集合没有分区键,则在操作数据库时不需要为分区键设置查询条件

在post中,集合没有分区键,您将分区键设置为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
'name',所以这里我有两个分区:'jay''jay1'

因此,这里您应该将
partitionkey
属性设置为'jay'或'jay2',而不是'name'

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

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

        client.deleteDocument("/dbs/"+databaseName+"/colls/"+collectionName+"/docs/1",requestOptions);
线程“main”java.lang.UnsupportedOperationException中出现异常: 必须为此操作提供PartitionKey值。在 com.microsoft.azure.documentdb.DocumentClient.addPartitionKeyInformation(DocumentClient.java:3199) 在 com.microsoft.azure.documentdb.DocumentClient.addPartitionKeyInformation(DocumentClient.java:3180) 在 com.microsoft.azure.documentdb.DocumentClient.deleteDocument(DocumentClient.java:959) 在DeleteDocuments.main(DeleteDocuments.java:32)

我需要将partition key参数设置为操作文档存储的分区

 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代码中,只需将partition key设置为
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);

希望对您有所帮助。

创建文档时,是否为partitionKey属性指定了值?如果是,那值多少?不,我没有。我已经提到了。按c.partitionKey从c ORDER中选择c.partitionKey。此查询不返回任何结果。到目前为止,我的集合中没有任何分区键。我已经尝试过这样做,但它引发异常2017-11-02 11:16:06调试文档客户端:951-删除文档。documentLink:[dbs/5Ck4AA==/colls/5Ck4ALpusQA=/docs/5ck4alpusqaaaaaaaa==/]2017-11-02 11:16:09错误JobRunShell:211-Job com.hassan.temp.toAzureJob引发了未经处理的异常:java.lang.UnsupportedOperationException:必须为此操作提供PartitionKey值。@SyedHassanAshraf您的集合似乎已经有了分区键。但是,您在回复评论中说您的集合中没有任何分区键。所以,我有点困惑。请发布数据库结构的截图好吗?@SyedHassanAshraf另外,请发布一些存储在数据库中的示例文档。我不能这样做。我不允许这样做。但您可以告诉我如何验证或检查我的集合是否已经有分区密钥?这会有很大的帮助。好的,我知道RequestOptions中partition key属性的值应该是多少,但是假设我有一个包含{name,class,city,company,}字段的集合,并且其他人创建了分区并填充了它。现在,如何知道这里哪个是PartitionKey,就像在您的例子中是“name”一样。问题是,我只得到了读/写键、数据库和集合链接。如何知道我的集合中是否有分区键(如“name”)(如果没有,那么该怎么办)。
 RequestOptions requestOptions = new RequestOptions();
 requestOptions.setOfferThroughput(400);
 PartitionKey partitionKey = new PartitionKey("jay");
 requestOptions.setPartitionKey(partitionKey);

        client.deleteDocument("/dbs/"+databaseName+"/colls/"+collectionName+"/docs/1",requestOptions);
 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);