我使用 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/