【问题标题】:How update collections using Azure functions如何使用 Azure 函数更新集合
【发布时间】:2017-12-07 10:45:32
【问题描述】:

我想创建绑定到 cosmos DB 的 azure Function。

  • 每当“A”集合中发生某些插入时,我都想更新“B”集合。
  • “B”集合有存储过程,我想在插入集合“A”后调用它。

我是 Azure 和 cosmos-DB 的新手。

建议我需要做什么来完成要求。

到目前为止,我已经创建了 Azure Function

还使用以下代码更新了 function.json。

{
  "bindings": [
    {
      "type": "cosmosDBTrigger",
      "name": "input",
      "direction": "in",
      "leaseCollectionName": "leases",
      "connectionStringSetting": "cdb-swm-dev-001_DOCUMENTDB",
      "databaseName": "admin",
      "collectionName": "aomsorders",
      "createLeaseCollectionIfNotExists": true
    },
    {
      "type": "documentDB",
      "name": "inputDocument",
      "databaseName": "admin",
      "collectionName": "aomsorders",
      "connection": "cdb-swm-dev-001_DOCUMENTDB",
      "direction": "in"
    },
    {
      "type": "documentDB",
      "name": "outputDocument",
      "databaseName": "admin",
      "collectionName": "test",
      "createIfNotExists": true,
      "connection": "cdb-swm-dev-001_DOCUMENTDB",
      "direction": "out"
    }
  ],
  "disabled": false
}

还更新了集成部分如下

任何建议都会受到重视。

【问题讨论】:

  • 到目前为止,您自己做了哪些工作?
  • 让我看看我是否理解正确:您想要一个 Azure 函数,它正在侦听一个集合(使用触发器)然后写入第二个集合(使用输出绑定)?
  • @MatiasQuaranta 是的,完全正确。
  • 我看到您仍在使用门户来编写函数。本周一,Visual Studio 发布了新版本 15.5,这使得使用 CosmosDB 编写功能变得非常容易。在此处查看屏幕投屏sarosh.wordpress.com/2017/12/06/…

标签: azure azure-cosmosdb azure-functions


【解决方案1】:

我发现您的问题不是很具体,但总的来说您至少有两种选择:

  • 从同一个 Azure Function 插入两个集合

  • 从第一个 Azure Function 插入到集合 1,然后让第二个 Azure Function 使用 Cosmos DB trigger 监听集合 1 的更改并更新集合 2

我相信还有其他选择。

【讨论】:

  • 我很想知道您的第二个选项。触发器是否仅限于特定集合?在那种情况下这将如何工作?
  • @GauravMantri 你不会混淆 Cosmos DB 自己的触发器和 Cosmos DB 更改源触发的 Azure Function 吗?函数可以为所欲为。
  • 我仅指 Cosmos DB 触发器。啊...现在我已经正确阅读了您的回复,我明白您在说什么。这对我来说完全有意义。谢谢!
【解决方案2】:

这是一个由 CosmosDBTrigger 触发的 Azure 函数示例,然后它使用 DocumentDB 输出绑定写入第二个集合:

function.json

{
  "bindings": [
    {
      "type": "cosmosDBTrigger",
      "name": "input",
      "direction": "in",
      "databaseName": "your-database",
      "collectionName": "your-collection1",
      "connectionStringSetting": "name-of-connectionstring-setting-for-collection1",
      "leaseCollectionName": "your-lease-collection"
    },
    {
      "type": "documentDB",
      "direction": "out",
      "name": "docsToSave",
      "databaseName": "your-database2",
      "collectionName": "your-collection2",
      "connection": "name-of-connectionstring-setting-for-collection2",
      "createIfNotExists": false
    }
  ]
}

run.csx (C#)

#r "Microsoft.Azure.Documents.Client"
using Microsoft.Azure.Documents;
using System.Collections.Generic;
using System;
public static async Task Run(IReadOnlyList<Document> input, IAsyncCollector<Document> docsToSave)
{
    foreach(var doc in input){
        // Do something here, process the document or do your compute
        // Here I am saving the same document to the second collection but you could send a new document created within the processing logic or send the same document modified by some logic
        await docsToSave.AddAsync(doc);
    }
}

index.js (NodeJS)

module.exports = function(context, input) {
    if(!!input && input.length > 0){
        context.bindings.docsToSave = [];
        for(var i = 0, len=input.length; i<len;i++){
            var doc = input[i];
            // Do something here with the doc or create a new one
            context.bindings.docsToSave.push(doc);
        }
    }
    context.done();
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-11-01
    • 2018-06-02
    • 1970-01-01
    • 2020-08-10
    • 1970-01-01
    • 2018-11-08
    • 2017-09-10
    • 2015-07-08
    相关资源
    最近更新 更多