首页 > 解决方案 > 使用更改源将数据从旧容器迁移到新的分区容器

问题描述

1.是否使用azure cosmosdb changefeed正确的方法,将数据从旧容器迁移到新的分区容器?
2. azure cosmosdb changefeed 会有文件到现在都没有修改过吗?
3.如果上述情况属实,使用 azure cosmosdb change feed 迁移数据的步骤是什么。

标签: azure-cosmosdb

解决方案


您当然可以使用 Change Feed 来实现这一切。Cosmos DB 有多个使用更改源的选项,其中之一是使用 Azure Functions

如果您使用 Functions 选项,则可以快速执行简单的迁移,混合Cosmos DB 触发器(使用StartFromBeginningto true)和Cosmos DB 输出绑定,如下所示:

[FunctionName("Migration")]
public static async Task Run(
    [CosmosDBTrigger(
        databaseName: "your-source-database",
        collectionName: "your-source-collection",
        ConnectionStringSetting = "Connection-String-Setting-Name-For-Source-Account",
        StartFromBeginning = true,
        CreateLeaseCollectionIfNotExists = true,
    )] IReadOnlyList<Document> source,
    [CosmosDB(
        databaseName: "your-destination-database",
        collectionName: "your-destination-collection",
        ConnectionStringSetting = "Connection-String-Setting-Name-For-Destination-Account" // In case your destination is on a different account, otherwise, it could be the same value as the Trigger
    )] IAsyncCollector<Document> destination,
    ILogger log)
{
    foreach(var doc in source){
          await destination.AddAsync(doc);
    }
}

有一些优化可以应用于多主或地理分布式应用程序(例如,PreferredLocations如果您的函数在不同区域运行时使用)。

请记住,函数充当实时迁移。它将开始迁移在部署之前存在的文档,然后如果您的源集合不断获得插入/更新,它将在继续运行的同时继续迁移新文档。

如果您需要的是迁移,您还可以使用数据迁移工具


推荐阅读