将每个 Azure Cosmos DB 资源的吞吐量从标准转换为自动缩放

适用对象: NoSQL MongoDB Cassandra Gremlin

本文中的脚本演示了如何将订阅中的每个资源从标准预配吞吐量转换为自动缩放。

许多客户在开发新应用程序时,会首先选择标准预配吞吐量。 然而,标准吞吐量最适合用于具有持续吞吐量需求的工作负载。 大多数工作负载都是可变的。 这意味着自动缩放的使用成本通常更低。 在可能有数十或数百个要迁移的资源的情况下,这可能会很繁琐且耗时。 此脚本用于在单个步骤中迁移所有资源。

如果没有 Azure 试用版订阅,请在开始前创建一个试用版订阅

先决条件

可以使用本地 Azure CLI。

  • 如果需要,请安装 Azure CLI 来运行 CLI 参考命令。

  • 本地 Azure CLI,请了解如何安装 Azure CLI。 如果在 Windows 或 macOS 上运行,请考虑在 Docker 容器中运行 Azure CLI。 有关详细信息,请参阅如何在 Docker 容器中运行 Azure CLI

    • 通过使用 az login 命令登录到 Azure CLI。 若要完成身份验证过程,请遵循终端中显示的步骤。 有关其他登录选项,请参阅使用 Azure CLI 登录

    • 出现提示时,请在首次使用时安装 Azure CLI 扩展。 有关扩展详细信息,请参阅使用 Azure CLI 的扩展

    • 运行 az version 以查找安装的版本和依赖库。 若要升级到最新版本,请运行 az upgrade

  • 本文需要 Azure CLI 版本 2.9.1 或更高版本。

运行脚本

#!/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>

示例参考

此脚本使用以下命令。 表中的每条命令均链接到特定于命令的文档。

命令 说明
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 数据库吞吐量迁移 迁移 NoSQL 数据库资源的吞吐量。
az cosmosdb sql container list 列出数据库中的所有 NoSQL 容器。
az cosmosdb sql container throughput show 读取帐户中 NoSQL 容器的吞吐量值。
az cosmosdb sql 容器吞吐量迁移 读取帐户中 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 密钥空间。
az cosmosdb cassandra keyspace throughput show 读取帐户中 Cassandra 密钥空间的吞吐量值。
az cosmosdb cassandra 密钥空间吞吐量迁移 迁移帐户中 Cassandra 密钥空间的吞吐量。
az cosmosdb cassandra table list 列出密钥空间中的所有 Cassandra 表。
az cosmosdb cassandra table throughput show 读取帐户中 Cassandra 表的吞吐量值。
az cosmosdb cassandra 表吞吐量迁移 迁移帐户中 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 示例,请参阅: