【问题标题】:Azure Functions unable to update Multiple Cosmos DocumentAzure Functions 无法更新多个 Cosmos 文档
【发布时间】:2021-06-16 13:13:38
【问题描述】:

几天以来一直在尝试,似乎是 Azure Functions 中的一个错误。我正在尝试使用计时器触发器,从 Cosmos DB(输入绑定)读取文档并更新每个文档的一些属性(Cosmos DB 输出绑定)。不管我尝试什么,新属性都不会推送到 Cosmos DB。我确保 id 保持不变。

下面是我的function.json:

{
  "bindings": [
    {
      "name": "myTimer",
      "type": "timerTrigger",
      "direction": "in",
      "schedule": "0 */10 * * * *"
    },
    {
      "name": "DocumentInput",
      "type": "cosmosDB",
      "databaseName": "MyDB1",
      "collectionName": "MyCC1",
      "sqlQuery": "SELECT * FROM c",
      "partitionKey": "/key",
      "connectionStringSetting": "CosmosDBConnectionExp",
      "direction": "in"
    },
    {
      "name": "DocumentOutput",
      "type": "cosmosDB",
      "databaseName": "MyDB1",
      "collectionName": "MyCC1",
      "createIfNotExists": false,
      "partitionKey": "/key",
      "connectionStringSetting": "CosmosDBConnectionExp",
      "direction": "out"
    }
  ]
}

我的功能如下:

const PushDataChanges = async (context) => {
    
    var cosmosOutput = context.bindings.DocumentOutput;
    var cosmosInput = context.bindings.DocumentInput;
   
    cosmosOutput = cosmosInput;
 

    for(var i = 0; i < cosmosOutput.length; i++) {
   
        var document = cosmosOutput[i];
        var docid = cosmosOutput[i].id;
        var now = new Date().toISOString();

        document.id = docid; //Making sure the id key is same so upsert or update happens.
        document.experiment.lastProcessedTime = "now";
        document.experiment.updateStatus = "This is now updated";
    }

    context.done();

}


module.exports = function (context, Timer) {
    
    context.log('Starting Delta Data Extraction and Sentiment Analysis');
    PushDataChanges(context);
     
};

任何帮助将不胜感激。

【问题讨论】:

  • 你不应该把await 换成PushDataChanges(context); 吗?
  • 这不相关!!!添加 await 并不能解决我的问题!它可以捕获错误。我原来的问题仍然存在......请提供任何帮助

标签: javascript azure azure-functions azure-cosmosdb azure-cosmosdb-sqlapi


【解决方案1】:

经过大量试验和错误后,这终于对我有用了!用于 Azure 函数的 cosmos 绑定的文档记录很差。我花了很多时间从文档中的给定示例中推断出如何仅更新一个文档。下面的代码提供了一种通过 Azure Functions 使用 Java 脚本更新/更新多个 cosmos DB 文档的方法。

功能代码:

const PushDataChanges = async (context) => {
    
    var inputdoc = context.bindings.DocumentInput;
   
    for(var i = 0; i < inputdoc.length; i++) {

        console.log(inputdoc.length);

        var now2 = new Date().toISOString();

        inputdoc[i].experiment.updateStatus = "This is updated now";
        inputdoc[i].experiment.lastProcessedTime = now2;
        context.bindings.DocumentOutput = inputdoc;
    }


context.done;
}

module.exports = function (context, Timer) {
    
    context.log('Starting the Azure Function');

    PushDataChanges(context);

    context.done();
     
};

以下是我的绑定:

{
  "bindings": [
    {
      "name": "myTimer",
      "type": "timerTrigger",
      "direction": "in",
      "schedule": "0 0 */8 * * *"
    },
    {
      "type": "cosmosDB",
      "name": "DocumentInput",
      "databaseName": "gsk-next-internal-datastore",
      "collectionName": "experiment",
      "sqlQuery": "SELECT * FROM c",
      "connectionStringSetting": "CosmosDBConnectionExp",
      "direction": "in"
    },
    {
      "type": "cosmosDB",
      "name": "DocumentOutput",
      "databaseName": "gsk-next-internal-datastore",
      "collectionName": "experiment",
      "createIfNotExists": false,
      "connectionStringSetting": "CosmosDBConnectionExp",
      "direction": "out"
    }
  ]
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-02-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-12-19
    相关资源
    最近更新 更多