java - 使用存储过程和分区在 CosmosDb 中批量导入

标签 java azure azure-cosmosdb

我使用 Azure 的 cosmosDb 一段时间了。最近我使用 stored procedure 完成了批量导入在我的数据库集合中,并且过去工作得很好。现在我必须在另一个使用分区的集合中执行相同的操作;我搜索了 azure 代码示例并修改了之前的批量插入函数,如下所示:

public void createMany(JSONArray aDocumentList, PartitionKey aPartitionKey)  throws DocumentClientException {
        List<String> aList = new ArrayList<String>();
        for(int aIndex = 0; aIndex < aDocumentList.length(); aIndex++) {
                JSONObject aJsonObj = aDocumentList.getJSONObject(aIndex);
                aList.add(aJsonObj.toString());
        }

        String aSproc = getCollectionLink() + BULK_INSERTION_PROCEDURE;
        RequestOptions requestOptions = new RequestOptions();
        requestOptions.setPartitionKey(aPartitionKey);

        String result = documentClient.executeStoredProcedure(aSproc,
                        requestOptions
                        , new Object[] { aList}).getResponseAsString();

}

但是这段代码给了我错误:

com.microsoft.azure.documentdb.DocumentClientException: Message: {"Errors":["Encountered exception while executing function. Exception = Error: {\"Errors\":[\"Requests originating from scripts cannot reference partition keys other than the one for which client request was submitted.\"]}\r\nStack trace: Error: {\"Errors\":[\"Requests originating from scripts cannot reference partition keys other than the one for which client request was submitted.\"]}\n   at callback (bulkInsertionStoredProcedure.js:1:1749)\n   at Anonymous function (bulkInsertionStoredProcedure.js:689:29)"]}

我不太确定该错误的实际含义。由于 partitionKey 只是文档中的 JSON 键,为什么其他文档中也需要它。我是否还需要将其附加到我的文档中(使用 partitionKey 键)。有人可以告诉我我在这里缺少什么吗?我在互联网上进行了搜索,但没有找到任何有用的东西可以使其发挥作用。

最佳答案

I've already answered this question here 。其要点是,您使用 SPROC 插入的文档必须具有与您通过请求选项传递的分区 key 相匹配的分区 key

// ALL documents inserted must have a parititionKey value that matches
// "aPartitionKey" value
requestOptions.setPartitionKey(aPartitionKey);

因此,如果aPartitionKey == 123456,那么您使用 SPROC 插入的所有文档都必须属于该分区。如果您想要批量插入跨越多个分区的文档,则必须按分区键对它们进行分组,并为每个分组单独运行 SPROC。

关于java - 使用存储过程和分区在 CosmosDb 中批量导入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45075129/

相关文章:

java - Java 类型转换的奇怪输出

Azure表存储: Order by

c# - 在 Azure 实例上实现简单的本地内存缓存

azure - 有没有办法将数据直接加载到 json 或 csv 格式到 cosmos db gremlin api?

jupyter-notebook - Cosmos DB 缺少 Jupyter Notebook 选项

c# - 在 C# 中避免 Azure DocumentDB 中的任务取消异常

java - 如何设置具有安全约束的 Faces Servlet 映射

java - 如果release()出现在acquire()之前,二进制信号量不起作用

c# - 将参数传递给 CosmosDB 存储过程

java - 执行由 Jasmin 生成的文件 .classes 时出错