簡體   English   中英

在變更供稿上使用功能觸發器時,如何獲取cosmos DB文檔中的數據

[英]How to get to the data in a cosmos DB document when using Function trigger on the change feed

我正在使用azure函數cosmos DB觸發器V1嘗試分析基於某些值的文檔,但由於某些原因,我在如何從文檔中讀取屬性方面很掙扎。 我正在閱讀此頁面以嘗試理解它,但無法使其正常工作。

觸發功能文檔

我創建了這個示例類,它被命中並很好地接收了更改提要,但是我沒有進入if子句,因為我沒有正確讀取該屬性,而且我真的看不到如何使用此方法更深入地了解JSON。

 public static class Function1
    {
        [FunctionName("Function1")]
        public static void Run([CosmosDBTrigger(
            databaseName: "XXX",
            collectionName: "XXX",
            ConnectionStringSetting = "CosmosDb",
            LeaseCollectionName = "leases", LeaseCollectionPrefix = "local")]IReadOnlyList<Document> documents, TraceWriter log)
        {
            if (documents != null && documents.Count > 0)
            {
                log.Info("Documents modified " + documents.Count);
                log.Info("First document Id " + documents[0].Id);

                foreach(var document in documents)
                {
                    if(document.GetPropertyValue<string>("sourceSystem") == "YYYY")
                    {
                        log.Info("sourceSystem = YYYY");
                    }
                }

            }
        }
    }

將文檔嵌套在JSON數組中時,如何從文檔中讀取數據? 我對getPropertyValue有點困惑,因為它反對使用linq或類似的語法解析JSON,如何從文檔中獲取我的值?

JSON文件被解析為Document Object,該文檔對象提供方法GetPropertyValue<T>(string propertyName) 我們可以使用此方法搜索JSON文件的第一級。 為了更深入,我們需要適當地解析JSON屬性。

例如,讀取此文件。

{
    "id": "test1",
    "myJarray": [
        {
            "mydata1": "testout",
            "mydata2": "testout"
        }
    ]
}

使用JArray進入內部。

foreach (var document in documents)
{
    var array = document.GetPropertyValue<JArray>("myJarray");
    if (array != null)
    {
        var data = ((JObject)array[0]).GetValue("mydata1");
        if (data != null)
        {
            log.Info(data.ToString());
        }
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

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