[英]Why would a Cosmos stored procedure run differently when called from browser vs. called from Java?
I have a stored procedure in Cosmos DB Emulator.我在 Cosmos DB 模拟器中有一个存储过程。 All this procedure does is: delete ALL documents from mycollection
.此过程所做的只是:从mycollection
中删除所有文档。 When I run it in browser ( https://localhost:8081/_explorer/index.html
), it works great.当我在浏览器( https://localhost:8081/_explorer/index.html
)中运行它时,它工作得很好。 Then I try to call it from Java:然后我尝试从 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());
And get the following result: {"deleted": 0,"continuation": false}
并得到以下结果: {"deleted": 0,"continuation": false}
This is CRAZY.这太疯狂了。 I'm running this stored procedure from the browser and getting this result: {"deleted": 10,"continuation": false}
.我正在从浏览器运行这个存储过程并得到这个结果: {"deleted": 10,"continuation": false}
。 Then (of course adding back those 10 documents) running this result from Java and getting this result: {"deleted": 0,"continuation": false}
然后(当然添加回这 10 个文档)从 Java 运行这个结果并得到这个结果: {"deleted": 0,"continuation": false}
So when the stored procedure is ran by Java, it is called but didn't do the job.因此,当存储过程由 Java 运行时,它被调用但没有完成工作。 Deleted nothing.... Why would this happen?什么都没删除......为什么会发生这种情况?
Below is the stored procedure下面是存储过程
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();
}
}
}
For partitioned containers, when executing a stored procedure, a partition key value must be provided in the request options.对于分区容器,在执行存储过程时,必须在请求选项中提供分区键值。 Stored procedures are always scoped to a partition key.存储过程始终限定为分区键。 Items that have a different partition key value will not be visible to the stored procedure.具有不同分区键值的项目对存储过程不可见。 This also applied to triggers as well.这也适用于触发器。
You are setting partition key to "null" in requestOptions.您在 requestOptions 中将分区键设置为“null”。 "null" is a valid partition key value. “null”是一个有效的分区键值。 Looks like "null" is not a partition key value for your 10 documents.看起来“null”不是您的 10 个文档的分区键值。
Humbly reposting @Jay Gong answer How to specify NONE partition key for deleting a document in Document DB java SDK?谦虚地转发@Jay Gong 回答如何指定NONE 分区键来删除文档DB java SDK 中的文档?
Maybe it will help someone.也许它会帮助某人。 Put:放:
PartitionKey partitionKey = new PartitionKey(Undefined.Value());
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.