如何在Document DB java SDK中指定删除文档的NONE分区键? [英] How to specify NONE partition key for deleting a document in Document DB java SDK?

查看:125
本文介绍了如何在Document DB java SDK中指定删除文档的NONE分区键?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have only a single collection and when I try to delete the document using the code below

    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:消息:{错误:[未找到资源]}
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)
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.micr osoft.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)
如果我没有提供RequestOptions,它会要求我提供一个分区键。
我没有分区键,因为下面没有返回任何内容

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

我该如何解决这个问题?

How do I solve this?

推荐答案

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

Per my experience, if your collection does not have a partitioning key, you do not need to set the query criteria for the partitioning key when you operate the database.

在发布时,集合没有分区键,您将分区键设置为RequestOption。所以,数据库肯定不知道在哪里找到要操作的文件。

In you post, the collection does not have a partitioning key, and you set the partitioning key into RequestOption. So , the database certainly doesn't know where to find the document to operate.

你可以参考我的代码片段:

You could refer to my snippet of code:

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






更新答案:


Update Answer:

我认为您误解了的含义选项[] 中的partitionkey 属性。

I think you misunderstand the meaning of partitionkey property in the options[].

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

For example , my container is created like this:

这里的分区键是我的收藏品的名称。您可以查看收藏夹的分区键。

The partition key is "name" for my collection here. You could check your collection's partition key.

我的文件如下:

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

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

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

My partitionkey is 'name', so here I have two paritions : 'jay' and 'jay1'.

所以,在这里你应该将 partitionkey 属性设置为'jay'或'jay2',不是'名字'。

So, here you should set the partitionkey property to 'jay' or 'jay2',not 'name'.

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

At this point, if I run the code below without setting partition key into RequestOptions, I'll meet the same issue as you.

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

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




线程main中的异常java.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)

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)

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

I need to set partition key parameter to be the partition where the operated document stored.

 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:


Update Answer 2:

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

I guess you want to operate the document which does not set partition key.

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

Please refer to this perfect blog, you'll find the answer!

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

In java code , just set partition key to Undefined.Value() then everything will be done.

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

希望对您有帮助。

这篇关于如何在Document DB java SDK中指定删除文档的NONE分区键?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆