簡體   English   中英

Azure Cosmos DB 觸發器事件時間戳

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM