java CosmosDB:CosmosPatchOperation无法通过存储过程工作

tf7tbtn2  于 2023-05-12  发布在  Java
关注(0)|答案(2)|浏览(95)

我想用Stored Proc替换Cosmos批处理,因为我的要求是upsert 100+记录,而Cosmos批处理不支持。我在List中添加了2个java对象和1个CosmosPatchOperations,并传递给下面的方法。当我添加CosmosPatch对象时,没有行被插入/更新,否则它工作正常。我想在同一个事务中执行插入和补丁操作。有人能指导如何修改SP,使其同时支持插入和修补操作吗?

String rowsUpserted = "";
    try
    {
      rowsUpserted = container
              .getScripts()
              .getStoredProcedure("createEvent")
              .execute(Arrays.asList(listObj), options)
              .getResponseAsString();
    }catch(Exception e){
      e.printStackTrace();
    }

存储过程

function createEvent(items) {

var collection = getContext().getCollection();
var collectionLink = collection.getSelfLink();
var count = 0;

if (!items) throw new Error("The array is undefined or null.");

var numItems = items.length;

if (numItems == 0) {
    getContext().getResponse().setBody(0);
    return;
}

tryCreate(items[count], callback);

function tryCreate(item, callback) {
    var options = { disableAutomaticIdGeneration: false };

    var isAccepted = collection.upsertDocument(collectionLink, item, options, callback);
    
    

    if (!isAccepted) getContext().getResponse().setBody(count);
}

function callback(err, item, options) {
    if (err) throw err;
    count++;
    if (count >= numItems) {
        getContext().getResponse().setBody(count);
    } else {
        tryCreate(items[count], callback);
    }
}
}
af7jpaap

af7jpaap1#

Javascript stored proc API中的Collection类型似乎不支持打补丁。我怀疑这样做是因为它更适合远程调用和SP本地执行,所以它不是真的必要的。
API参考在这里:http://azure.github.io/azure-cosmosdb-js-server/Collection.html
upsertDocument需要完整的文档。

omtl5h9j

omtl5h9j2#

@Loren -在存储过程中支持补丁操作。请参阅此Microsoft文档:开始使用Azure Cosmos DB部分文档更新。您希望跳过第一个JavaScript示例,转到“用于补丁操作的示例存储过程”后面的示例。
以防文档消失,下面是示例存储过程:

function patchDemo() {
    var doc = {
        "id": "exampleDoc",
        "fields": {
            "field1": "exampleString",
            "field2": 20,
            "field3": 40
        }
    };
    
    var isAccepted = __.createDocument(__.getSelfLink(), doc, (err, doc) => {
        if (err) {
            throw err;
        }
        else {
            getContext().getResponse().setBody("Example document successfully created.");
            
            var patchSpec = [
                { "op": "add", "path": "/fields/field1", "value": "newExampleString" },
                { "op": "remove", "path": "/fields/field2" },
                { "op": "incr", "path": "/fields/field3", "value": 10 }
            ];
            
            var isAccepted = __.patchDocument(doc._self, patchSpec, (err, doc) => {
                if (err) {
                    throw err;
                }
                else {
                    getContext().getResponse().appendBody(" Example document successfully patched.");
                }
            });
            
            if (!isAccepted) throw new Error("Patch wasn't accepted");
        }
    });

    if (!isAccepted) throw new Error("Create wasn't accepted.");
}

相关问题