使用 Azure Databricks 將資料從 MongoDB 移轉至以虛擬核心為基礎的 Azure Cosmos DB for MongoDB

已完成

Azure Databricks 可簡化將大型 MongoDB 資料集移至以 V 核心為基礎的 Azure Cosmos DB for MongoDB,讓複雜資料的傳輸更為簡單。 它可讓您詳細控制移轉步調和資料轉換,有效地處理大型的資料集。

為何要使用 Azure Databricks?

Azure Databricks 是適用於 Apache Spark 的平台即服務 (PaaS),引進了一種簡化且有效率的方法,以從 MongoDB 進行離線與線上的資料庫移轉。 其架構支援廣泛的資料集而設計,使之成為您的移轉專案的適當選擇。

必要條件

開始移轉之前,務必備妥下列必要條件:

  • 確認以虛擬核心為基礎的 Azure Cosmos DB for MongoDB 帳戶處於作用中且已正確設定。 請確定您已為 Azure MongoDB 帳戶啟用受控識別,並授與存取來源 MongoDB 資料庫的必要權限。
  • 佈建 Azure Databricks 叢集,選取 Databricks 執行時間 7.6 版,以獲得最佳 Spark 3.0 相容性。

如何將 MongoDB 移轉擴充功能新增至 Azure Data Studio 的螢幕擷取畫面。

  • 將 Maven MongoDB Connector for Spark 連結庫新增至叢集。 此連結庫可連結到原生 MongoDB 和 Azure Cosmos DB 端點。

執行移轉。

您可以在 Azure Databricks 中建立 Python 或 Scala 筆記本,以執行移轉。 例如,如果您想要使用 Python Notebook 將資料從 MongoDB 移轉至以虛擬核心為基礎的 Azure Cosmos DB for MongoDB,您會將此程式碼片段新增至筆記本:

from pyspark.sql import SparkSession
from azure.identity import DefaultAzureCredential

# Set up the managed identity credential
credential = DefaultAzureCredential()

# Retrieve the access token for Azure Cosmos DB
token = credential.get_token("https://cosmos.azure.com/.default")

# Construct the target connection string using the access token
target_account_name = "<ACCOUNTNAME>"  # Replace with your Cosmos DB account name
target_connection_string = (
    f"mongodb://:@{target_account_name}.mongo.cosmos.azure.com:10255/"
    f"?ssl=true&replicaSet=globaldb&retrywrites=false&maxIdleTimeMS=120000&appName=@{target_account_name}@"
    f"&authMechanism=PLAIN&authSource=$external&password={token.token}"
)

# MongoDB source connection details
source_connection_string = "mongodb://<USERNAME>:<PASSWORD>@<HOST>:<PORT>/<AUTHDB>"  # Replace with your source MongoDB connection
source_db = "<DB NAME>"
source_collection = "<COLLECTIONNAME>"
target_db = "<DB NAME>"
target_collection = "<COLLECTIONNAME>"

# Initialize Spark session
my_spark = SparkSession \
    .builder \
    .appName("MongoDB to CosmosDB Migration") \
    .config("spark.mongodb.input.uri", source_connection_string) \
    .config("spark.mongodb.output.uri", target_connection_string) \
    .getOrCreate()

# Read from source MongoDB
df = my_spark.read.format("com.mongodb.spark.sql.DefaultSource") \
    .option("uri", source_connection_string) \
    .option("database", source_db) \
    .option("collection", source_collection) \
    .load()

# Write to Azure Cosmos DB (MongoDB API)
df.write.format("mongo") \
    .mode("append") \
    .option("uri", target_connection_string) \
    .option("database", target_db) \
    .option("collection", target_collection) \
    .save()

執行移轉

執行移轉筆記本,監視任何潛在的速率限制錯誤之程序,並視需要調整參數,以確保資料傳輸順暢。

強化移轉效能

根據您的以虛擬核心為基礎的 Azure Cosmos DB for MongoDB 叢集層,建議您調整一些設定,以改善移轉效能。 其中幾個設定包括:

  • Spark 叢集設定:最佳化背景工作角色和核心數目,會直接影響到處理資料移轉的計算能力。
  • 批次大小調整:微調 maxBatchSize 參數,有助於降低速率限制錯誤。 此參數會控制每個批次中傳送至目標資料庫的文件數目。

無論您要進行線上或離線移轉,使用 Azure Databricks 進行移轉專案,都可提供健全且可調整的解決方案,以將大型資料集從 MongoDB 傳輸到以虛擬核心為基礎的 Azure Cosmos DB for MongoDB。 它提供調整移轉速度和資料轉換的彈性,以確保移轉程序成功。