![](/img/trans.png)
[英]Not able to run Cosmos DB Change Feed Trigger Azure Function locally
[英]Azure Cosmos DB Trigger event timestamp
我使用非常简单的 Azure Function Cosmos DB Trigger。 它只是将相同的消息推送到存储队列中。 稍后从该存储队列中处理消息。 我试图在 Cosmos DB 中触发 Cosmos DB 触发器的时间。 我需要一个递增的时间戳(理想情况下是时间),稍后我可以在存储队列处理函数中使用它来区分首先是哪个 Cosmos DB 触发器。
我的 AZ Cosmos DB 触发器如下所示
[FunctionName("CosmosTrigger")]
public static void Run([CosmosDBTrigger(
databaseName: "DbTesting",
containerName: "test",
Connection = "CosmosDBConnection",
LeaseContainerName = "test_leases",
CreateLeaseContainerIfNotExists = true,
FeedPollDelay = 1000)]
IReadOnlyList<DbModel> modifiedModels,
ILogger log)
{
** Push model message to Storage Queue for later processing **
}
这背后的想法是,当消息处理失败时,它会被推回存储队列进行重试。
是否有任何属性/对象可以添加到函数声明中以获取事件时间戳?
我不想使用这种方法
{
var etag = DateTime.Now;
** Push model message with **etag** to Storage Queue for later processing **
}
因为如果第一个 AZ 的处理被延迟或到达那个 etag 点的时间比第二个要长怎么办? 那么第二个AZ的时间会更短吗?
您可以在 Cosmos DB 中添加日期时间值并在 Azure CosmosDb 函数中获取它。 如自爆代码所示。
using System;
using System.Collections.Generic;
using Microsoft.Azure.Documents;
using Microsoft.Azure.WebJobs;
using Microsoft.Azure.WebJobs.Host;
using Microsoft.Extensions.Logging;
using Newtonsoft.Json.Linq;
namespace _72966054_3
{
public static class Function1
{
[FunctionName("Function1")]
public static void Run([CosmosDBTrigger(
databaseName: "Studenttable",
collectionName: "students",
ConnectionStringSetting = "ConnectionStrings:cosmosdb",
LeaseCollectionName = "leases", CreateLeaseCollectionIfNotExists =true)]IReadOnlyList<Document> input,
ILogger log)
{
if (input != null && input.Count > 0)
{
dynamic list = JObject.Parse(input[0].ToString());
string t = Convert.ToString(list.Date +" "+ list.Time);
log.LogInformation("CosmQuery Time " + t); // This time come from Azure Cosmos Db.
log.LogInformation("Timestamp Time " + input[0].Timestamp);
log.LogInformation("Function Time " + System.DateTime.UtcNow);
}
}
}
}
输出 :-
Azure Cosmos Db 数据:-
是否有任何属性/对象可以添加到函数声明中以获取事件时间戳?
您可以存储此时间并处理您的要求,如果处理失败,则您有 cosmos 事件时间。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.