有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

azure cosmosdb为什么从浏览器调用Cosmos存储过程与从Java调用Cosmos存储过程的运行方式不同?

我在Cosmos DB Emulator中有一个存储过程。这个过程所做的只是:从mycollection删除所有文档。当我在浏览器(https://localhost:8081/_explorer/index.html)中运行它时,效果非常好。然后我试着从Java中调用它:

RequestOptions requestOptions = new RequestOptions(); 
requestOptions.setPartitionKey(new PartitionKey(null));
System.out.println("START DELETE PROCEDURE"); 
StoredProcedureResponse spr = client.executeStoredProcedure(sprocLink, requestOptions, null);     
System.out.println(spr.getResponseAsString()); 

并得到以下结果:{"deleted": 0,"continuation": false}

这太疯狂了。我正在从浏览器中运行这个存储过程,并得到以下结果:{"deleted": 10,"continuation": false}。然后(当然是加回那10个文档)从Java运行这个结果,得到这个结果:{"deleted": 0,"continuation": false}

因此,当存储过程由Java运行时,它被调用,但没有完成任务。没有删除任何内容。。。。为什么会这样


下面是存储过程

   function testStoredProcedure( ) {
       var collection = getContext().getCollection();
        var collectionLink = collection.getSelfLink();
        var response = getContext().getResponse();
        var responseBody = {
            deleted: 0,
            continuation: true
        };

        var query = 'SELECT * FROM mycollection   ';

    // Validate input.
    if (!query) throw new Error("The query is undefined or null.");

    tryQueryAndDelete();

    // Recursively runs the query w/ support for continuation tokens.
    // Calls tryDelete(documents) as soon as the query returns documents.
    function tryQueryAndDelete(continuation)) {
        var requestOptions = {continuation: continuation};

        var isAccepted = collection.queryDocuments(collectionLink, query, requestOptions, function (err, retrievedDocs, responseOptions) {
            if (err) throw err;

            if (retrievedDocs.length > 0) {
                // Begin deleting documents as soon as documents are returned form the query results.
                // tryDelete() resumes querying after deleting; no need to page through continuation tokens.
                //  - this is to prioritize writes over reads given timeout constraints.
                tryDelete(retrievedDocs);
            } else if (responseOptions.continuation) {
                // Else if the query came back empty, but with a continuation token; repeat the query w/ the token.
                tryQueryAndDelete(responseOptions.continuation);
            } else {
                // Else if there are no more documents and no continuation token - we are finished deleting documents.
                responseBody.continuation = false;
                response.setBody(responseBody);
            }
        });

        // If we hit execution bounds - return continuation: true.
        if (!isAccepted) {
            response.setBody(responseBody);
        }
    }

    // Recursively deletes documents passed in as an array argument.
    // Attempts to query for more on empty array.
    function tryDelete(documents) {
        if (documents.length > 0) {
            // Delete the first document in the array.
            var isAccepted = collection.deleteDocument(documents[0]._self, {}, function (err, responseOptions) {
                if (err) throw err;

                responseBody.deleted++;
                documents.shift();
                // Delete the next document in the array.
                tryDelete(documents);
            });

            // If we hit execution bounds - return continuation: true.
            if (!isAccepted) {
                response.setBody(responseBody);
            }
        } else {
            // If the document array is empty, query for more documents.
            tryQueryAndDelete();
        }
    }
}

共 (2) 个答案

  1. # 1 楼答案

    对于分区容器,在执行存储过程时,必须在请求选项中提供分区键值。存储过程的作用域总是一个分区键。具有不同分区键值的项对存储过程不可见。这也适用于触发器

    您正在requestOptions中将分区键设置为“null”。“null”是一个有效的分区键值。看起来“null”不是10个文档的分区键值