是否可以使用CosmosDB数据迁移工具将多对多关系映射到字符串数组?

问题描述 投票:0回答:1

我正在将一些数据从Azure SQL迁移到CosmosDB。我正在使用CosmosDB数据迁移工具。在SQL数据库中,“个人”和“收藏夹”之间存在多对多关系。我想将“收藏夹名称”迁移到CosmosDB文档中的一个简单数组中。我还没有找到一种方法。该文档建议我使用FOR JSON AUTO将子查询转换为JSON对象数组。

下面是现在的查询。这不是我想要的。

SELECT 
    p.Id AS [id],
    p.FirstName AS [name.first],
    p.LastName AS [name.last],
    o.City AS [office.City],
    o.[State] AS [office.State], 
    c.ReminderEmailEnabled AS [reminderEmail], 
    (SELECT f.[Name] FROM dbo.Favorites f JOIN dbo.PersonFavorite pf ON pf.FavoriteId = f.Id AND pf.PersonId = p.Id  FOR JSON AUTO) AS [favorites]
FROM dbo.Persons p
    LEFT JOIN dbo.Offices o ON o.Id = p.OfficeId

使用此查询在CosmosDB中生成的JSON文档如下所示:

{
    "name": {
        "first": "Donald",
        "last": "Duck"
    },
    "office": {
        "City": "Ann Arbor",
        "State": "MI"
    },
    "reminderEmail": true,
    "favorites": "[{\"Name\":\"Red\"},{\"Name\":\"Green\"}]",
    "id": "some guid",
}

将其转换为字符串,这就是为什么它随后添加转义字符以及JSON对象数组的原因。

我想要的是:

{
    "name": {
        "first": "Donald",
        "last": "Duck"
    },
    "office": {
        "City": "Ann Arbor",
        "State": "MI"
    },
    "reminderEmail": true,
    "favorites": ["Red","Green"],
    "id": "some guid",
}

如何使用数据迁移工具修改查询以实现此目的?

sql-server azure azure-cosmosdb data-migration
1个回答
0
投票

数据迁移工具无法处理此问题,但是您可以使用ADF和Databricks来完成。我们最近在Cosmos DB博客中写了一篇关于此的博客文章。

Migrating Relational Data into Azure Cosmos DB using Azure Data Factory and Azure Databricks

希望这会有所帮助。

© www.soinside.com 2019 - 2024. All rights reserved.