繁体   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