ExecuteStoredProcedureAsync 运行 它跨分区键

ExecuteStoredProcedureAsync running it across partition keys

我需要一个存储过程,我需要在不同的分区键上 运行。我的集合在一个键实体名称上分区,我想在分区中的每个实体上执行存储过程。

sproc = await client.CreateStoredProcedureAsync(collectionLink, sproc,
    new RequestOptions { PartitionKey = new PartitionKey(partitionkey) });

StoredProcedureResponse<int> scriptResult = await client.ExecuteStoredProcedureAsync<int>(
    sproc.SelfLink,
    new RequestOptions { PartitionKey = new PartitionKey(partitionkey) },
    args);

我得到以下异常:

Requests originating from scripts cannot reference partition keys other than the one for which client request was submitted

  1. 是否需要在每个分区中根据key创建一个存储过程?
  2. 是否可以有一个存储过程可以对所有键执行?

当从客户端执行存储过程时,RequestOptions 指定分区键,存储过程将运行在此分区的上下文中,并且不能对具有不同分区键值的文档进行操作(例如创建)。

您可以为每个分区键从客户端执行存储过程。例如,如果存储过程要批量创建文档,您可以按分区键对文档进行分组,然后将每个组(可以并行完成)发送到在 RequestOptions 中提供分区键值的存储过程。这会有帮助吗?

您不必为每个分区键创建存储过程,只需创建一次而不提供分区键。

我已经在 Java 中开发了以上内容。

我们在使用 Java SDK 和存储过程时的实现方式有所不同。

请注意 'string' 的使用以及需要根据分区键分隔记录。

使用的批量导入存储过程:https://docs.microsoft.com/en-us/azure/documentdb/documentdb-programming

下面是调用存储过程的客户端

public void CallStoredProcedure(Map <String, List<String>> finalMap)
{
    for ( String key : finalMap.keySet() ) {
        try
        {
            ExecuteStoredProcedure(finalMap.get(key),key);
        }
        catch(Exception ex)
        {
            LOG.info(ex.getMessage());
        }
    }       
}
public void ExecuteStoredProcedure(List<String> documents, String partitionKey)
{

    try {

        if (documentClient == null) {
            documentClient = new DocumentClient(documentDBHostUrl, documentDBAccessKey, null, ConsistencyLevel.Session);
        }

        options = new RequestOptions();
        options.setPartitionKey(new PartitionKey(partitionKey));

        Object[] sprocsParams = new Object[2] ;
        sprocsParams[0] = documents;
        sprocsParams[1] = null;

        StoredProcedureResponse rs = documentClient.executeStoredProcedure(selflink, options, sprocsParams);

    } catch (Exception ex) {

    }
}