【问题标题】:Time trigger function with CosmosDB inputs具有 CosmosDB 输入的时间触发功能
【发布时间】:2019-08-22 06:49:38
【问题描述】:

我在 azure 函数中创建了一个时间触发函数,并添加了一个 CosmosDB 输入,如下所示。

下面是.csx文件

#r "Microsoft.Azure.Documents.Client"

using System;
using Microsoft.Azure.Documents;
using Microsoft.Azure.Documents.Client;

public static async Task Run(TimerInfo myTimer, string[] inputDocument, TraceWriter log)
{
    log.Info($"C# Timer trigger function executed at: {DateTime.Now}");

    // string [] = bindings.inputDocument;

    DocumentClient client;
}

如何将 cosmosDb 中的输入文档获取到这个 csx 文件中?

我不熟悉C#,在javascript中我们将使用var Data = context.bindings.DataInput; 如何在 c# 中做同样的事情?

【问题讨论】:

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


    【解决方案1】:

    你可以像下面的sn-p那样使用它

    public static void Run(TimerInfo myTimer, IEnumerable<dynamic> documents)
        {
            foreach (var doc in documents)
            {
                // operate on each document
            }
        }
    

    documentation中的更多示例

    来自cmets的问题

    如果我们有多个 cosmos Db 输入,我们需要添加如下吗?

    即使您有多个输入,也不会使用IEnumerable&lt;dynamic&gt; documents。并且您可以迭代列表。

    如果我们有 cosmosDB 输出,如何添加?

    out object 在 this 中使用,它指向您的绑定。

    public static void Run(string myQueueItem, out object employeeDocument, ILogger log)
        {
          log.LogInformation($"C# Queue trigger function processed: {myQueueItem}");
    
          dynamic employee = JObject.Parse(myQueueItem);
    
          employeeDocument = new {
            id = employee.name + "-" + employee.employeeId,
            name = employee.name,
            employeeId = employee.employeeId,
            address = employee.address
          };
        }
    

    更多关于Output的信息

    【讨论】:

    • 只是为了澄清,1.如果我们有多个cosmos Db输入,我们需要添加如下吗? public static void Run(TimerInfo myTimer, IEnumerable&lt;dynamic&gt; documents01, IEnumerable&lt;dynamic&gt; documents02) 2. 如果我们有 cosmosDB 输出,如何添加?和我们添加IEnumerable&lt;dynamic&gt; documentsOut这样的cosmosDB输入一样吗?
    • @Antony 我已经更新了答案请看一下
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-11-06
    • 2019-11-20
    • 2021-08-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多