【问题标题】:Azure CosmosDB Trigger to update a Collection based on Value in another CollectionAzure CosmosDB 触发器根据另一个集合中的值更新集合
【发布时间】:2019-10-18 16:51:45
【问题描述】:

我必须根据另一个 cosmosDB 集合中的列的值更新一个 cosmosDB 集合(假设为集合 1)(假设为集合 2)。集合 1 应该使用来自其他集合(如集合 3 和集合 4)的值进行更新。我曾尝试在 Collection-2 中编写后触发器,但在触发器内编写函数时卡住了。

请建议是否可以使用 CosmosDB 触发器或建议是否有任何替代方法可以实现这一点。

我为 cosmos DB 创建了一个新的触发函数。

【问题讨论】:

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


【解决方案1】:

我对集合 >>> 触发器和 Azure 函数 Cosmos DB 触发器感到困惑。 我上面的问题的解决方案是创建一个 Azure 函数来触发 cosmos DB。

创建资源 >>​​> 计算 >>> 函数应用程序。

要添加输入和输出,请转到函数应用程序中的集成选项 >>> 您创建的触发器。

Refer the link for function binding:

此函数将在单个 cosmosDB 集合中的数据更新时触发。 我们可以添加 n 个集合作为输入,一个集合作为输出。

下面是定义绑定时会自动创建的function.json文件:

{
  "bindings": [
    {
      "type": "cosmosDBTrigger",
      "name": "documents",
      "direction": "in",
      "leaseCollectionName": "leases",
      "connectionStringSetting": "cosmosdb_DOCUMENTDB",
      "databaseName": "connectivityDB",
      "collectionName": "tblEvent",
      "createLeaseCollectionIfNotExists": true
    },
    {
      "type": "cosmosDB",
      "name": "outputDocument",
      "databaseName": "connectivityDB",
      "collectionName": "tblNewEvent",
      "createIfNotExists": true,
      "connectionStringSetting": "cosmosdb_DOCUMENTDB",
      "partitionKey": "/id",
      "direction": "out"
    }
  ]
}

下面是创建的 index.js 文件:

module.exports = function(context, input) {

     var documents = context.bindings.documents;
     var output = [];

     if(!!input && input.length > 0){

       //write your code here

     }

    context.bindings.outputDocument = output;
    context.done();
}

【讨论】:

    【解决方案2】:

    据我所知,您只能在每个 Azure Function CosmosDB Trigger 中监视一个 cosmos db 集合的更新。

    也就是说,如果Collection-1中的列更新是由Collection 2,3,4的更新决定的,则需要为此创建3个触发器。

    在每个触发器中,请按照document配置收集信息并使用Cosmos DB SDK替换具体文档。


    更新答案:

    完全同意@Matias Quaranta评论中的解释,您在这里混淆了两种触发器。正如您所提到的,当然,需要采用Azure函数触发器。 Cosmos DB 触发器无法监视您收集的任何更新,它是被动触发的。

    例如:

    如果您想在将文档插入 cosmos db 之前添加一列,您可以在使用 insert document cosmos db sdk 时设置触发器名称以激活它。这是cosmos db下的触发器。

    如果你想监控你的cosmos db集合的更新,那么做一些业务,需要采用Azure Function Trigger。

    【讨论】:

    • Collection >> 触发器是完全不同的东西,它们是these 触发器。这些触发器具有与 SQL Server 表触发器类似的概念。 Azure Functions Cosmos DB 触发器是一个无服务器组件,可让您构建在集合中有新数据时触发的 Azure Functions:docs.microsoft.com/azure/cosmos-db/change-feed-functions
    • @Antony 请参考我的更新答案和 Matias 的 cmets。
    • 感谢@Jay Gong。我已经创建了一个函数触发器并且工作正常。
    • @Antony 太好了,你可以标记答案来结束这个案例。祝你有美好的一天。
    • @Matias Quaranta : 我们可以显式触发这个函数来创建数据和更新数据吗?
    猜你喜欢
    • 1970-01-01
    • 2019-11-01
    • 1970-01-01
    • 2013-09-23
    • 1970-01-01
    • 2018-12-11
    • 1970-01-01
    • 2019-08-05
    • 2013-07-31
    相关资源
    最近更新 更多