使用 Azure Data Factory 或 Synapse Analytics 將資料複製到 Azure Cosmos DB for MongoDB 或從中複製資料

適用於:Azure Data Factory Azure Synapse Analytics

提示

試用 Microsoft Fabric 中的 Data Factory,這是適用於企業的全方位分析解決方案。 Microsoft Fabric 涵蓋從資料移動到資料科學、即時分析、商業智慧和報告的所有項目。 了解如何免費啟動新的試用版

本文概述如何使用 Azure Data Factory 中的複製活動和 Synapse Analytics 管線,從 Azure Cosmos DB for MongoDB 資料複製或將資料複製到其中。 本文是以複製活動為基礎,該文提供複製活動的一般概觀。

注意

此連接器僅支援從 Azure Cosmos DB for MongoDB 複製資料或將資料複製到其中。 對於 Azure Cosmos DB for NoSQL,請參閱 Azure Cosmos DB for NoSQL 連接器。 目前不支援其他 API 類型。

支援的功能

下列功能支援此 Azure Cosmos DB for MongoDB 連接器:

支援的功能 IR 受控私人端點
複製活動 (來源/接收) ① ②

① Azure 整合執行階段 ② 自我裝載整合執行階段

您可將資料從 Azure Cosmos DB for MongoDB 複製到任何支援的接收資料存放區,或將資料從任何支援的來源資料存放區複製到 Azure Cosmos DB for MongoDB。 如需複製活動作為來源和接收端支援的資料存放區清單,請參閱支援的資料存放區和格式

您可使用 Azure Cosmos DB for MongoDB 連接器:

  • Azure Cosmos DB for MongoDB複製資料或將資料複製到其中。
  • 寫入 Azure Cosmos DB 作為 insertupsert
  • 依原樣匯入和匯出 JSON 文件,或從表格式資料集複製資料或將資料複製到其中。 範例包括 SQL 資料庫和 CSV 檔案。 若要依原樣將文件複製到 JSON 檔案或另一個 Azure Cosmos DB 集合或複製其中的文件,請參閱「匯入或匯出 JSON 文件」。

開始使用

若要透過管線執行複製活動,您可以使用下列其中一個工具或 SDK:

使用 UI 建立 Azure Cosmos DB for MongoDB 的連結服務

使用下列步驟,在 Azure 入口網站 UI 中建立 Azure Cosmos DB for MongoDB 的連結服務。

  1. 前往 Azure Data Factory 或 Synapse 工作區的 [管理] 索引標籤,選取 [連結服務],然後按一下 [新增]:

  2. 搜尋 Azure Cosmos DB for MongoDB,然後選取該連接器。

    Select the Azure Cosmos DB for MongoDB connector.

  3. 設定服務詳細資料,測試連線,然後建立新的連結服務。

    Configure a linked service to Azure Cosmos DB for MongoDB.

連接器設定詳細資料

以下各節提供屬性的詳細資料,您可用來定義 Azure Cosmos DB for MongoDB 專屬的 Data Factory 實體。

連結服務屬性

以下是 Azure Cosmos DB for MongoDB 連結服務所支援的屬性:

屬性 描述 必要
type type 屬性必須設定為 CosmosDbMongoDbApi Yes
connectionString 為 Azure Cosmos DB for MongoDB 指定連接字串。 您可在 Azure 入口網站 -> 您的 Azure Cosmos DB 刀鋒視窗 -> 中找到主要或次要連接字串。
針對 3.2 伺服器版本,字串模式為 mongodb://<cosmosdb-name>:<password>@<cosmosdb-name>.documents.azure.com:10255/?ssl=true&replicaSet=globaldb
針對 3.6+ 伺服器版本,字串模式為 mongodb://<cosmosdb-name>:<password>@<cosmosdb-name>.mongo.cosmos.azure.com:10255/?ssl=true&replicaSet=globaldb&retrywrites=false&maxIdleTimeMS=120000&appName=@<cosmosdb-name>@

您也可以將密碼放在 Azure Key Vault 中,並從連接字串中提取 password 設定。 如需詳細資訊,請參閱在 Azure Key Vault 中儲存認證
Yes
database 您要存取的資料庫名稱。 Yes
isServerVersionAbove32 指定伺服器版本是否高於 3.2。 允許的值為 TrueFalse (預設值)。 這會決定要在服務中使用的驅動程式。 Yes
connectVia 用來連線到資料存放區的整合執行階段。 您可以使用 Azure Integration Runtime 或自我裝載整合執行階段 (如果您的資料存放區位於私人網路中)。 如果未指定此屬性,則會使用預設的 Azure Integration Runtime。 No

範例

{
    "name": "CosmosDbMongoDBAPILinkedService",
    "properties": {
        "type": "CosmosDbMongoDbApi",
        "typeProperties": {
            "connectionString": "mongodb://<cosmosdb-name>:<password>@<cosmosdb-name>.documents.azure.com:10255/?ssl=true&replicaSet=globaldb",
            "database": "myDatabase",
            "isServerVersionAbove32": "false"
        },
        "connectVia": {
            "referenceName": "<name of Integration Runtime>",
            "type": "IntegrationRuntimeReference"
        }
    }
}

資料集屬性

如需定義資料集的區段和屬性完整清單,請參閱資料集和連結服務。 以下是 Azure Cosmos DB for MongoDB 資料集支援的屬性:

屬性 描述 必要
type 資料集的 type 屬性必須設定為 CosmosDbMongoDbApiCollection Yes
collectionName Azure Cosmos DB 集合的名稱。 Yes

範例

{
    "name": "CosmosDbMongoDBAPIDataset",
    "properties": {
        "type": "CosmosDbMongoDbApiCollection",
        "typeProperties": {
            "collectionName": "<collection name>"
        },
        "schema": [],
        "linkedServiceName":{
            "referenceName": "<Azure Cosmos DB for MongoDB linked service name>",
            "type": "LinkedServiceReference"
        }
    }
}

複製活動屬性

本節提供 Azure Cosmos DB for MongoDB 來源和接收支援的屬性清單。

如需可用來定義活動的區段和屬性完整清單,請參閱管線

Azure Cosmos DB for MongoDB 作為來源

複製活動的 [來源] 區段支援下列屬性:

屬性 描述 必要
type 複製活動來源的 type 屬性必須設定為 CosmosDbMongoDbApiSource Yes
filter 使用查詢運算子指定選取範圍篩選。 若要傳回集合中的所有文件,請省略此參數,或傳遞空白文件 ({})。 No
cursorMethods.project 指定要在文件中傳回以便投影的欄位。 若要傳回比對文件中的所有欄位,請省略此參數。 No
cursorMethods.sort 指定查詢傳回比對文件的順序。 請參閱 cursor.sort() No
cursorMethods.limit 指定伺服器傳回的文件數目上限。 請參閱 cursor.limit() No
cursorMethods.skip 指定要跳過的文件數,以及 MongoDB 開始傳回結果的位置。 請參閱 cursor.skip() No
batchSize 指定要在回應的每個批次中從 MongoDB 執行個體傳回的文件數目。 在大部分情況下,修改批次大小不會影響使用者或應用程式。 Azure Cosmos DB 限制每個批次的大小不能超過 40 MB,也就是文件大小的 batchSize 總數,因此,如果您的文件大小很大,請降低此值。 No
(預設為 100)

提示

ADF 支援在 Strict 模式中取用 BSON 文件。 請確定您的篩選查詢處於 Strict 模式,而非 Shell 模式。 如需詳細說明,請參閱 MongoDB 手冊

範例

"activities":[
    {
        "name": "CopyFromCosmosDBMongoDBAPI",
        "type": "Copy",
        "inputs": [
            {
                "referenceName": "<Azure Cosmos DB for MongoDB input dataset name>",
                "type": "DatasetReference"
            }
        ],
        "outputs": [
            {
                "referenceName": "<output dataset name>",
                "type": "DatasetReference"
            }
        ],
        "typeProperties": {
            "source": {
                "type": "CosmosDbMongoDbApiSource",
                "filter": "{datetimeData: {$gte: ISODate(\"2018-12-11T00:00:00.000Z\"),$lt: ISODate(\"2018-12-12T00:00:00.000Z\")}, _id: ObjectId(\"5acd7c3d0000000000000000\") }",
                "cursorMethods": {
                    "project": "{ _id : 1, name : 1, age: 1, datetimeData: 1 }",
                    "sort": "{ age : 1 }",
                    "skip": 3,
                    "limit": 3
                }
            },
            "sink": {
                "type": "<sink type>"
            }
        }
    }
]

Azure Cosmos DB for MongoDB 作為接收

複製活動的 [接收] 區段支援下列屬性:

屬性 描述 必要
type 複製活動接收的 type 屬性必須設定為 CosmosDbMongoDbApiSink Yes
writeBehavior 描述如何將資料寫入至 Azure Cosmos DB。 允許的值:insertupsert

如果存在具有相同 _id 的文件,upsert 的行為會用來取代文件;否則會插入文件。

附註:如果未在原始文件中或藉由資料行對應來指定 _id,則服務會自動為文件產生 _id。 這表示您必須確定,為了讓 upsert 如預期般運作,您的文件具有識別碼。
No
(預設值為 insert)
writeBatchSize writeBatchSize 屬性可控制在每個批次中寫入的文件大小。 您可以嘗試增加 writeBatchSize 的值來改善效能,如果您的文件大小很大,則可嘗試降低此值。 No
(預設值為 10,000)
writeBatchTimeout 在逾時前等待批次插入作業完成的時間。允許的值為時間範圍。 No
(預設為 00:30:00 - 30 分鐘)

提示

若要以現況匯入 JSON 文件,請參閱匯入或匯出 JSON 文件一節;若要從表格式資料複製,請參閱結構描述對應

範例

"activities":[
    {
        "name": "CopyToCosmosDBMongoDBAPI",
        "type": "Copy",
        "inputs": [
            {
                "referenceName": "<input dataset name>",
                "type": "DatasetReference"
            }
        ],
        "outputs": [
            {
                "referenceName": "<Document DB output dataset name>",
                "type": "DatasetReference"
            }
        ],
        "typeProperties": {
            "source": {
                "type": "<source type>"
            },
            "sink": {
                "type": "CosmosDbMongoDbApiSink",
                "writeBehavior": "upsert"
            }
        }
    }
]

匯入和匯出 JSON 文件

您可以使用 Azure Cosmos DB 連接器輕鬆地:

  • 在兩個 Azure Cosmos DB 集合之間依原樣複製文件。
  • 將 JSON 文件從各種來源,包括從 MongoDB、Azure Blob 儲存體、Azure Data Lake Store 及服務支援的其他檔案型存放區匯入到 Azure Cosmos DB。
  • 將 JSON 文件從 Azure Cosmos DB 集合匯出至各種檔案型存放區。

若要達成無從驗證結構描述的複製:

  • 當您使用複製資料工具時,請選取 [依原樣匯出到 JSON 檔案或 Azure Cosmos DB 集合] 選項。
  • 當您使用活動製作時,請選擇 JSON 格式搭配作為來源或接收的對應檔案存放區。

結構描述對應

若要將資料從 Azure Cosmos DB for MongoDB 複製到表格式接收或執行反向複製,請參閱結構描述對應

特別是針對寫入 Azure Cosmos DB,請確認您從來源資料使用正確的物件識別碼填入 Azure Cosmos DB,例如您在 SQL 資料庫資料表中有個「識別碼」資料行,並想要使用其值作為 MongoDB 中的文件識別碼,以插入/upsert,則必須根據 MongoDB strict 模式定義 (_id.$oid) 設定適當的結構描述對應,如下所示:

Map ID in MongoDB sink

執行複製活動後,系統會在接收中產生以下 BSON ObjectId:

{
    "_id": ObjectId("592e07800000000000000000")
}

如需複製活動支援做為來源和接收器的資料存放區清單,請參閱支援的資料存放區