共用方式為


將每個 Azure Cosmos DB 資源從標準輸送量轉換為自動調整輸送量

適用於:NoSQL MongoDB Cassandra Gremlin Table

本文中的指令碼示範如何使用標準佈建的輸送量將每個資源轉換為訂用帳戶內的自動調整。

許多客戶在開發新應用程式時會從標準佈建的輸送量開始。 不過,標準輸送量最適合用於具有持續輸送量需求的工作負載。 大部分的工作負載都是可變的。 這表示自動調整的使用成本通常較低。 在可能有數十或數百個要移轉的資源的情況下,這可能會非常繁瑣且耗時。 此指令碼的設計目的是為了在單一步驟中移轉所有資源。

如果您沒有 Azure 訂閱,請在開始之前,先建立 Azure 免費帳戶

必要條件

  • 本文需要 2.9.1 版或更新版本的 Azure CLI。 如果您是使用 Azure Cloud Shell,就已安裝最新版本。

範例指令碼

啟動 Azure Cloud Shell

Azure Cloud Shell 是免費的互動式 Shell,可讓您用來執行本文中的步驟。 它具有預先安裝和設定的共用 Azure 工具,可與您的帳戶搭配使用。

若要開啟 Cloud Shell,只要選取程式碼區塊右上角的 [試試看] 即可。 您也可以移至 https://shell.azure.com ,從另一個瀏覽器索引標籤啟動 Cloud Shell。

當開啟 Cloud Shell 時,請確認已為您的環境選取 Bash。 後續的工作階段將會在 Bash 環境中使用 Azure CLI,請選取 [複製] 以複製程式碼區塊,並將其貼到 Cloud Shell 中,然後按 Enter 鍵加以執行。

登入 Azure

系統會在登入的初始帳戶下自動驗證 Cloud Shell。 使用下列指令碼透過不同的訂閱登入,並將 <Subscription ID> 取代為您的 Azure 訂用帳戶識別碼。 如果您沒有 Azure 訂用帳戶,請在開始之前先建立 Azure 免費帳戶

subscription="<subscriptionId>" # add subscription here

az account set -s $subscription # ...or use 'az login'

如需詳細資訊,請參閱設定使用中訂閱以互動方式登入

執行指令碼

#!/bin/bash

# Passed validation in Cloud Shell on 7/25/2024

# <FullScript>
# Azure Cosmos DB users can migrate from standard provisioned to autoscale
# throughput and back again. Most users can benefit from autoscale throughput
# to save on costs and avoid over-provisioning. This script migrates all
# provisioned throughput to autoscale throughput for all Cosmos DB accounts
# in the current subscription for NoSQL, MongoDB, Cassandra, Gremlin, and Table
# database and container level resources in the accounts.


# These can remain commented out if running in Azure Cloud Shell
#az login
#az account set -s {your subscription id}

throughtput=0

# Get the list of resource groups in the current subscription
resourceGroups=$(az group list --query "[].name" -o tsv)

# Loop through every resource group in the subscription
for resourceGroup in $resourceGroups; do
    echo "Processing resource group: $resourceGroup"

    # Get the list of Cosmos DB accounts in this resource group
    accounts=$(az cosmosdb list -g $resourceGroup --query "[].name" -o tsv)

    # Loop through every Cosmos DB account in the resource group
    for account in $accounts; do

        echo "Processing account: $account"

        # Get the list of SQL databases in this account
        databases=$(az cosmosdb sql database list -g $resourceGroup -a $account --query "[].name" -o tsv)

        # Loop through SQL databases in the account
        for database in $databases; do
            throughput=$(az cosmosdb sql database throughput show -g $resourceGroup -a $account -n $database --query resource.throughput -o tsv)
            if [ $throughput -gt 0 ]; then
                echo "$database has throughput, attempting to migrate to autoscale"
                # Migrate the database to autoscale throughput
                az cosmosdb sql database throughput migrate -g $resourceGroup -a $account -n $database -t "autoscale"
                if [ $? -eq 0 ]; then
                    echo "Successfully migrated throughput for database: $database in Cosmos DB account $account"
                fi
            else
                echo "$database does not have throughput"
            fi

            # Loop through SQL containers in the database
            containers=$(az cosmosdb sql container list -g $resourceGroup -a $account -d $database --query "[].name" -o tsv)

            for container in $containers; do
                throughput=$(az cosmosdb sql container throughput show -g $resourceGroup -a $account -d $database -n $container --query resource.throughput -o tsv)
                if [ $throughput -gt 0 ]; then
                    echo "$container has throughput, attempting to migrate to autoscale"
                    # Migrate the container to autoscale throughput
                    az cosmosdb sql container throughput migrate -g $resourceGroup -a $account -d $database -n $container -t "autoscale"
                    if [ $? -eq 0 ]; then
                        echo "Successfully migrated throughput for container: $container in Cosmos DB account $account and database $database"
                    fi
                else
                    echo "$container does not have throughput"
                fi
            done
        done

        # Get the list of MongoDB databases in this account
        databases=$(az cosmosdb mongodb database list -g $resourceGroup -a $account --query "[].name" -o tsv)

        # Loop through MongoDB databases in the account
        for database in $databases; do
            throughput=$(az cosmosdb mongodb database throughput show -g $resourceGroup -a $account -n $database --query resource.throughput -o tsv)
            if [ $throughput -gt 0 ]; then
                echo "$database has throughput, attempting to migrate to autoscale"
                # Migrate the database to autoscale throughput
                az cosmosdb mongodb database throughput migrate -g $resourceGroup -a $account -n $database -t "autoscale"
                if [ $? -eq 0 ]; then
                    echo "Successfully migrated throughput for database: $database in Cosmos DB account $account"
                fi
            else
                echo "$database does not have throughput"
            fi

            # Loop through MongoDB collections in the database
            collections=$(az cosmosdb mongodb collection list -g $resourceGroup -a $account -d $database --query "[].name" -o tsv)

            for collection in $collections; do
                throughput=$(az cosmosdb mongodb collection throughput show -g $resourceGroup -a $account -d $database -n $collection --query resource.throughput -o tsv)
                if [ $throughput -gt 0 ]; then
                    echo "$collection has throughput, attempting to migrate to autoscale"
                    # Migrate the collection to autoscale throughput
                    az cosmosdb mongodb collection throughput migrate -g $resourceGroup -a $account -d $database -n $collection -t "autoscale"
                    if [ $? -eq 0 ]; then
                        echo "Successfully migrated throughput for collection: $collection in Cosmos DB account $account and database $database"
                    fi
                else
                    echo "$collection does not have throughput"
                fi
            done
        done

        # Get the list of Cassandra keyspaces in this account
        keyspaces=$(az cosmosdb cassandra keyspace list -g $resourceGroup -a $account --query "[].name" -o tsv)

        # Loop through Cassandra keyspaces in the account
        for keyspace in $keyspaces; do
            throughput=$(az cosmosdb cassandra keyspace throughput show -g $resourceGroup -a $account -n $keyspace --query resource.throughput -o tsv)
            if [ $throughput -gt 0 ]; then
                echo "$keyspace has throughput, attempting to migrate to autoscale"
                # Migrate the keyspace to autoscale throughput
                az cosmosdb cassandra keyspace throughput migrate -g $resourceGroup -a $account -n $keyspace -t "autoscale"
                if [ $? -eq 0 ]; then
                    echo "Successfully migrated throughput for keyspace: $keyspace in Cosmos DB account $account"
                fi
            else
                echo "$keyspace does not have throughput"
            fi

            # Loop through Cassandra tables in the keyspace
            tables=$(az cosmosdb cassandra table list -g $resourceGroup -a $account -k $keyspace --query "[].name" -o tsv)

            for table in $tables; do
                throughput=$(az cosmosdb cassandra table throughput show -g $resourceGroup -a $account -k $keyspace -n $table --query resource.throughput -o tsv)
                if [ $throughput -gt 0 ]; then
                    echo "$table has throughput, attempting to migrate to autoscale"
                    # Migrate the table to autoscale throughput
                    az cosmosdb cassandra table throughput migrate -g $resourceGroup -a $account -k $keyspace -n $table -t "autoscale"
                    if [ $? -eq 0 ]; then
                        echo "Successfully migrated throughput for table: $table in Cosmos DB account $account and keyspace $keyspace"
                    fi
                else
                    echo "$table does not have throughput"
                fi
            done
        done

        # Get the list of Gremlin databases in this account
        databases=$(az cosmosdb gremlin database list -g $resourceGroup -a $account --query "[].name" -o tsv)

        # Loop through Gremlin databases in the account
        for database in $databases; do
            throughput=$(az cosmosdb gremlin database throughput show -g $resourceGroup -a $account -n $database --query resource.throughput -o tsv)
            if [ $throughput -gt 0 ]; then
                echo "$database has throughput, attempting to migrate to autoscale"
                # Migrate the database to autoscale throughput
                az cosmosdb gremlin database throughput migrate -g $resourceGroup -a $account -n $database -t "autoscale"
                if [ $? -eq 0 ]; then
                    echo "Successfully migrated throughput for database: $database in Cosmos DB account $account"
                fi
            else
                echo "$database does not have throughput"
            fi

            # Loop through Gremlin graphs in the database
            graphs=$(az cosmosdb gremlin graph list -g $resourceGroup -a $account -d $database --query "[].name" -o tsv)

            for graph in $graphs; do
                throughput=$(az cosmosdb gremlin graph throughput show -g $resourceGroup -a $account -d $database -n $graph --query resource.throughput -o tsv)
                if [ $throughput -gt 0 ]; then
                    echo "$graph has throughput, attempting to migrate to autoscale"
                    # Migrate the graph to autoscale throughput
                    az cosmosdb gremlin graph throughput migrate -g $resourceGroup -a $account -d $database -n $graph -t "autoscale"
                    if [ $? -eq 0 ]; then
                        echo "Successfully migrated throughput for graph: $graph in Cosmos DB account $account and database $database"
                    fi
                else
                    echo "$graph does not have throughput"
                fi
            done
        done

        # Get the list of Table databases in this account
        tables=$(az cosmosdb table list -g $resourceGroup -a $account --query "[].name" -o tsv)

        # Loop through Table databases in the account
        for table in $tables; do
            throughput=$(az cosmosdb table throughput show -g $resourceGroup -a $account -n $table --query resource.throughput -o tsv)
            if [ $throughput -gt 0 ]; then
                echo "$table has throughput, attempting to migrate to autoscale"
                # Migrate the table to autoscale throughput
                az cosmosdb table throughput migrate -g $resourceGroup -a $account -n $table -t "autoscale"
                if [ $? -eq 0 ]; then
                    echo "Successfully migrated throughput for table: $table in Cosmos DB account $account"
                fi
            else
                echo "$table does not have throughput"
            fi
        done
    done
done

echo "All Done! Enjoy your new autoscale throughput Cosmos DB accounts!"

# </FullScript>

範例參考

此指令碼會使用下列命令。 下表中的每個命令都會連結至命令特定的文件。

Command 注意
az group list 列出 Azure 訂用帳戶中的所有資源群組。
az cosmosdb list 列出資源群組中的所有 Azure Cosmos DB 帳戶。
az cosmosdb sql database list 列出帳戶中的所有 NoSQL 資料庫。
az cosmosdb sql database throughput show 讀取帳戶中 NoSQL 資料庫的輸送量值。
az cosmosdb sql database throughput migrate 移轉 NoSQL 資料庫資源的輸送量。
az cosmosdb sql container list 列出資料庫中的所有 NoSQL 容器。
az cosmosdb sql container throughput show 讀取帳戶中 NoSQL 容器的輸送量值。
az cosmosdb sql container throughput migrate 移轉帳戶中 NoSQL 容器的輸送量。
az cosmosdb mongodb database list 列出帳戶中的所有 MongoDB 資料庫。
az cosmosdb mongodb database throughput show 讀取帳戶中 MongoDB 資料庫的輸送量值。
az cosmosdb mongodb database throughput migrate 移轉 MongoDB 帳戶中資料庫資源的輸送量。
az cosmosdb mongodb collection list 列出資料庫中的所有 MongoDB 集合。
az cosmosdb mongodb collection throughput show 讀取帳戶中 MongoDB 集合的輸送量值。
az cosmosdb mongodb collection throughput migrate 移轉 MongoDB 帳戶中集合資源的輸送量。
az cosmosdb cassandra keyspace list 列出帳戶中的所有 Cassandra Keyspace。
az cosmosdb cassandra keyspace throughput show 讀取帳戶中 Cassandra Keyspace 的輸送量值。
az cosmosdb cassandra keyspace throughput migrate 移轉帳戶中 Cassandra Keyspace 的輸送量。
az cosmosdb cassandra table list 列出 Keyspace 中的所有 Cassandra 資料表。
az cosmosdb cassandra table throughput show 讀取帳戶中 Cassandra 資料表的輸送量值。
az cosmosdb cassandra table throughput migrate 移轉帳戶中 Cassandra 資料表的輸送量。
az cosmosdb gremlin database list 列出帳戶中的所有 Gremlin 資料庫。
az cosmosdb gremlin database throughput show 讀取帳戶中 Gremlin 資料庫的輸送量值。
az cosmosdb gremlin database throughput migrate 移轉 Gremlin 資料庫資源的輸送量。
az cosmosdb gremlin container list 列出資料庫中的所有 Gremlin 圖形。
az cosmosdb gremlin container throughput show 讀取帳戶中 Gremlin 圖形的輸送量值。
az cosmosdb gremlin graph throughput migrate 移轉帳戶中 Gremlin 圖形的輸送量。
az cosmosdb table list 列出帳戶中的所有資料表。
az cosmosdb table throughput show 讀取帳戶中資料表的輸送量值。
az cosmosdb table throughput migrate 移轉帳戶中資料表的輸送量。

下一步

如需 Azure Cosmos DB CLI 的詳細資訊,請參閱 Azure Cosmos DB CLI 文件

如需特定 API 的 Azure CLI 範例,請參閱: