簡體   English   中英

了解 cosmos db 中的 changefeed

[英]understanding on changefeed in cosmos db

下面是我的代碼,我試圖將某些文檔從一個容器復制到另一個容器。 我正在使用 Azure function 和帶有 changefeed 的 Cosmos db 觸發器。 當我在調試模式下運行它時,我沒有得到任何文檔,並且在控制台中它顯示在 Host locked state 這意味着沒有新文檔可讀。 根據我的理解,azure function 應該在我的第一次運行中從頭開始讀取,然后在后續運行中它應該通過讀取租用容器中的令牌從它離開的地方開始讀取。 正在尋找專家來確認這一點,我沒有在這里閱讀任何項目的原因是什么? 我將調試器點放在第一行,但沒有命中它。

[FunctionName( "MigrateWithChangeFeed" )]
        public static async Task Run( [CosmosDBTrigger(
            databaseName: "sourcedb",
            collectionName: "collec1",
            StartFromBeginning = true,
            ConnectionStringSetting = "CosmosDBConnection",
            LeaseCollectionName = "leases",            
            CreateLeaseCollectionIfNotExists = true
    )] IReadOnlyList<Document> source,
    ILogger log )
        {           
            Uri taskCollectionUri = UriFactory.CreateDocumentCollectionUri( source_databaseId, source_containerId );
            DocumentClient client = new DocumentClient( new Uri( _endpointUrl ), _primaryKey );
            if( source != null && source.Count > 0 )
            {
                foreach( var item in source )
                {
                    string customerName = string.Empty;
                    var jsonContent = (JObject)JsonConvert.DeserializeObject( item.ToString() );
                    if( !string.IsNullOrEmpty( (string)jsonContent["ClassId"] ) )
                    {
                        customerName = (string)jsonContent["Customer"];
                        if( string.IsNullOrEmpty( customerName ) )
                        {
                            customerName = projectCustomerNameMapping
                                .FirstOrDefault( q => q.Key.IndexOf( (string)jsonContent["Project"] ) != -1 ||
                                ((string)jsonContent["Project"]).IndexOf( q.Key ) != -1 ).Value ?? "Unknown";
                        }
                        string partitionkeyValue = string.Concat( (string)jsonContent["ClassId"],
                            "|",
                            (string)jsonContent["Project"],
                            "|",
                            customerName );
                        jsonContent.Add( new JProperty( "PartitionKey", partitionkeyValue ) );
                        await client.CreateDocumentAsync( UriFactory.CreateDocumentCollectionUri(
                       source_databaseId, target_containerId ),
                       jsonContent );
                    }

                }
            }

首先,請不要在 Function 中創建DocumentClient ,您應該使用單例/靜態實例,否則您將很快耗盡可用連接。 參考: https://learn.microsoft.com/azure/azure-functions/manage-connections#static-clients

Change Feed 的工作原理是,第一次運行這個 Function 時,它將從頭開始讀取(基本上讀取受監視集合中的所有現有文檔),之后(假設它仍在運行),它將拾取任何新創建的或更新文件。 Trigger 基本上檢查包含標記先前存儲的檢查點的文檔的租賃集合,如果沒有,則表示它是第一次啟動,因此它尊重 StartFromBeginning 標志,之后,當有文檔時,它會忽略它。

但請記住,如果您在本地運行此 Function 並且還在雲中進行了部署,那么它們將充當同一 Function ( https://learn.microsoft.com/azure/cosmos-db/ troubleshoot-changefeed-functions#some-changes-are-missing-in-my-trigger )除非每個人都使用不同的租約集合或使用租約前綴( https://learn.microsoft.com/azure/cosmos-db /how-to-create-multiple-cosmos-db-triggers )。

暫無
暫無

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

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