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

时间:2017-07-13 08:17:36

标签: 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密钥)。有人可以告诉我我在这里缺少什么吗?我在互联网上搜索过,但没有找到任何有用的东西可以使它发挥作用。

1 个答案:

答案 0 :(得分:1)

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

Dim Dept_Row As Long
Dim Dept_Clm As Long
Table1 = Sheets("16-Compliancy-Rebuild").Range("D85:D750")
Table2 = Sheets("OpmerkingBackup").Range("B2:F750")
Dept_Row = Sheets("16-Compliancy-Rebuild").Range("H85").Row
Dept_Clm = Sheets("16-Compliancy-Rebuild").Range("H85").Column
For Each cl In Table1
  Sheets("16-Compliancy-Rebuild").Cells(Dept_Row, Dept_Clm) = Application.WorksheetFunction.VLookup(cl, Table2, 5, False)
  Dept_Row = Dept_Row + 1
Next cl

因此,如果// ALL documents inserted must have a parititionKey value that matches // "aPartitionKey" value requestOptions.setPartitionKey(aPartitionKey); ,那么您要使用SPROC插入的所有文档都必须属于该分区。如果您要跨越多个要分批插入的分区的文档,则必须按分区键对它们进行分组,并为每个分组单独运行SPROC。