將每個 Azure Cosmos DB 資源從標準輸送量轉換為自動調整輸送量
適用於:NoSQL MongoDB Cassandra Gremlin Table
本文中的指令碼示範如何使用標準佈建的輸送量將每個資源轉換為訂用帳戶內的自動調整。
許多客戶在開發新應用程式時會從標準佈建的輸送量開始。 不過,標準輸送量最適合用於具有持續輸送量需求的工作負載。 大部分的工作負載都是可變的。 這表示自動調整的使用成本通常較低。 在可能有數十或數百個要移轉的資源的情況下,這可能會非常繁瑣且耗時。 此指令碼的設計目的是為了在單一步驟中移轉所有資源。
如果您沒有 Azure 訂閱,請在開始之前,先建立 Azure 免費帳戶。
必要條件
在 Azure Cloud Shell 中使用 Bash 環境。 如需詳細資訊,請參閱 Azure Cloud Shell 中的 Bash 快速入門。
若要在本地執行 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。
- 本文需要 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。 使用下列腳本使用不同的訂用帳戶登入,並將 subscriptionId 取代為您的 Azure 訂用帳戶標識碼。
如果您沒有 Azure 訂閱,請在開始之前,先建立 Azure 免費帳戶。
subscription="subscriptionId" # Set Azure subscription ID 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>
範例參考
此指令碼會使用下列命令。 下表中的每個命令都會連結至命令特定的文件。
下一步
如需 Azure Cosmos DB CLI 的詳細資訊,請參閱 Azure Cosmos DB CLI 文件。
如需特定 API 的 Azure CLI 範例,請參閱: