使用 Azure CLI 对 Azure Cosmos DB 的密钥空间或表执行吞吐量 (RU/s) 操作 - Cassandra APIThroughput (RU/s) operations with Azure CLI for a keyspace or table for Azure Cosmos DB - Cassandra API

适用于: Cassandra API

先决条件Prerequisites

  • 如果需要,请安装 Azure CLI 来运行 CLI 参考命令。If you prefer, install the Azure CLI to run CLI reference commands.

    • 如果使用的是本地安装,请使用 az login 命令登录到 Azure CLI。If you're using a local installation, sign in to the Azure CLI by using the az login command. 若要完成身份验证过程,请遵循终端中显示的步骤。To finish the authentication process, follow the steps displayed in your terminal. 有关其他登录选项,请参阅登录 Azure CLIFor additional sign-in options, see Sign in with the Azure CLI.

    • 出现提示时,请在首次使用时安装 Azure CLI 扩展。When you're prompted, install Azure CLI extensions on first use. 有关扩展详细信息,请参阅使用 Azure CLI 的扩展For more information about extensions, see Use extensions with the Azure CLI.

    • 运行 az version 以查找安装的版本和依赖库。Run az version to find the version and dependent libraries that are installed. 若要升级到最新版本,请运行 az upgradeTo upgrade to the latest version, run az upgrade.

  • 本文需要 Azure CLI 版本 2.12.1 或更高版本。This article requires version 2.12.1 or later of the Azure CLI.

备注

请先运行 az cloud set -n AzureChinaCloud 更改云环境,然后才能在 Azure 中国中使用 Azure CLI。Before you can use Azure CLI in Azure China , please run az cloud set -n AzureChinaCloud first to change the cloud environment. 若要切换回 Azure 公有云,请再次运行 az cloud set -n AzureCloudIf you want to switch back to Azure Public Cloud, run az cloud set -n AzureCloud again.

示例脚本Sample script

此脚本创建一个具有共享吞吐量的 Cassandra 密钥空间和一个具有专用吞吐量的 Cassandra 表,并更新该密钥空间和该表的吞吐量。This script creates a Cassandra keyspace with shared throughput and a Cassandra table with dedicated throughput, then updates the throughput for both the keyspace and table. 脚本随后从标准吞吐量迁移到自动缩放吞吐量,然后在迁移后读取自动缩放吞吐量的值。The script then migrates from standard to autoscale throughput then reads the value of the autoscale throughput after it has been migrated.

#!/bin/bash
# Reference: az cosmosdb | https://docs.azure.cn/cli/cosmosdb
# --------------------------------------------------
#
# Throughput operations for a Cassandra keyspace and table
#
#

# Sign in the Azure China Cloud
az cloud set -n AzureChinaCloud
az login

# Variables for Cassandra API resources
uniqueId=$RANDOM
resourceGroupName="Group-$uniqueId"
location='chinanorth2'
accountName="cosmos-$uniqueId" #needs to be lower case
keySpaceName='keyspace1'
tableName='table1'
originalThroughput=400
updateThroughput=500

# Create a resource group, Cosmos account, keyspace and table
az group create -n $resourceGroupName -l $location
az cosmosdb create -n $accountName -g $resourceGroupName --capabilities EnableCassandra
az cosmosdb cassandra keyspace create -a $accountName -g $resourceGroupName -n $keySpaceName --throughput $originalThroughput

# Define the schema for the table and create the table
schema=$(cat << EOF 
{
    "columns": [
        {"name": "columnA","type": "uuid"}, 
        {"name": "columnB","type": "text"}
    ],
    "partitionKeys": [{"name": "columnA"}]
} 
EOF )
echo "$schema" > "schema-$uniqueId.json"
az cosmosdb cassandra table create -a $accountName -g $resourceGroupName -k $keySpaceName -n $tableName --throughput $originalThroughput --schema @schema-$uniqueId.json
rm -f "schema-$uniqueId.json"

# Throughput operations for Cassandra API keyspace
# Read the current throughput
# Read the minimum throughput
# Make sure the updated throughput is not less than the minimum
# Update the throughput
# Migrate between standard (manual) and autoscale throughput
# Read the autoscale max throughput

read -p 'Press any key to get current provisioned Keyspace throughput'

az cosmosdb cassandra keyspace throughput show \
    -a $accountName \
    -g $resourceGroupName \
    -n $keySpaceName \
    --query resource.throughput \
    -o tsv

read -p 'Press any key to get minimum allowable Keyspace throughput'

minimumThroughput=$(az cosmosdb cassandra keyspace throughput show \
    -a $accountName \
    -g $resourceGroupName \
    -n $keySpaceName \
    --query resource.minimumThroughput \
    -o tsv)

echo $minimumThroughput

# Make sure the updated throughput is not less than the minimum allowed throughput
if [ $updateThroughput -lt $minimumThroughput ]; then
    updateThroughput=$minimumThroughput
fi

read -p 'Press any key to update Keyspace throughput'

az cosmosdb cassandra keyspace throughput update \
    -a $accountName \
    -g $resourceGroupName \
    -n $keySpaceName \
    --throughput $updateThroughput

read -p 'Press any key to migrate the keyspace from standard (manual) throughput to autoscale throughput'

az cosmosdb cassandra keyspace throughput migrate \
    -a $accountName \
    -g $resourceGroupName \
    -n $keySpaceName \
    -t 'autoscale'

read -p 'Press any key to read current autoscale provisioned max throughput on the keyspace'

az cosmosdb cassandra keyspace throughput show \
    -g $resourceGroupName \
    -a $accountName \
    -n $keySpaceName \
    --query resource.autoscaleSettings.maxThroughput \
    -o tsv

# Throughput operations for Cassandra API table
# Read the current throughput
# Read the minimum throughput
# Make sure the updated throughput is not less than the minimum
# Update the throughput
# Migrate between standard (manual) and autoscale throughput
# Read the autoscale max throughput

read -p 'Press any key to get current provisioned Table throughput'

az cosmosdb cassandra table throughput show \
    -a $accountName \
    -g $resourceGroupName \
    -k $keySpaceName \
    -n $tableName \
    --query resource.throughput \
    -o tsv

read -p 'Press any key to get minimum allowable Table throughput'

minimumThroughput=$(az cosmosdb cassandra table throughput show \
    -a $accountName \
    -g $resourceGroupName \
    -k $keySpaceName \
    -n $tableName \
    --query resource.minimumThroughput \
    -o tsv)

echo $minimumThroughput

# Make sure the updated throughput is not less than the minimum allowed throughput
if [ $updateThroughput -lt $minimumThroughput ]; then
    updateThroughput=$minimumThroughput
fi

read -p 'Press any key to update Table throughput'

az cosmosdb cassandra table throughput update \
    -a $accountName \
    -g $resourceGroupName \
    -k $keySpaceName \
    -n $tableName \
    --throughput $updateThroughput

read -p 'Press any key to migrate the table from standard (manual) throughput to autoscale throughput'

az cosmosdb cassandra table throughput migrate \
    -a $accountName \
    -g $resourceGroupName \
    -k $keySpaceName \
    -n $tableName \
    -t 'autoscale'

read -p 'Press any key to read current autoscale provisioned max throughput on the table'

az cosmosdb cassandra table throughput show \
    -g $resourceGroupName \
    -a $accountName \
    -k $keySpaceName \
    -n $tableName \
    --query resource.autoscaleSettings.maxThroughput \
    -o tsv

清理部署Clean up deployment

运行脚本示例后,可以使用以下命令删除资源组以及与其关联的所有资源。After the script sample has been run, the following command can be used to remove the resource group and all resources associated with it.

az group delete --name $resourceGroupName

脚本说明Script explanation

此脚本使用以下命令。This script uses the following commands. 表中的每条命令均链接到特定于命令的文档。Each command in the table links to command specific documentation.

命令Command 说明Notes
az group createaz group create 创建用于存储所有资源的资源组。Creates a resource group in which all resources are stored.
az cosmosdb createaz cosmosdb create 创建 Azure Cosmos DB 帐户。Creates an Azure Cosmos DB account.
az cosmosdb cassandra keyspace createaz cosmosdb cassandra keyspace create 创建 Azure Cosmos Cassandra 密钥空间。Creates an Azure Cosmos Cassandra keyspace.
az cosmosdb cassandra table createaz cosmosdb cassandra table create 创建 Azure Cosmos Cassandra 表。Creates an Azure Cosmos Cassandra table.
az cosmosdb cassandra keyspace throughput updateaz cosmosdb cassandra keyspace throughput update 更新 Azure Cosmos Cassandra 密钥空间的 RU/秒。Update RU/s for an Azure Cosmos Cassandra keyspace.
az cosmosdb cassandra table throughput updateaz cosmosdb cassandra table throughput update 更新 Azure Cosmos Cassandra 表的 RU/秒。Update RU/s for an Azure Cosmos Cassandra table.
az cosmosdb cassandra 密钥空间吞吐量迁移az cosmosdb cassandra keyspace throughput migrate 迁移 Azure Cosmos Cassandra 密钥空间的吞吐量。Migrate throughput for an Azure Cosmos Cassandra keyspace.
az cosmosdb cassandra 表吞吐量迁移az cosmosdb cassandra table throughput migrate 迁移 Azure Cosmos Cassandra 表的吞吐量。Migrate throughput for an Azure Cosmos Cassandra table.
az group deleteaz group delete 删除资源组,包括所有嵌套的资源。Deletes a resource group including all nested resources.

后续步骤Next steps

有关 Azure Cosmos DB CLI 的详细信息,请参阅 Azure Cosmos DB CLI 文档For more information on the Azure Cosmos DB CLI, see Azure Cosmos DB CLI documentation.

可以在 Azure Cosmos DB CLI GitHub 存储库中找到所有 Azure Cosmos DB CLI 脚本示例。All Azure Cosmos DB CLI script samples can be found in the Azure Cosmos DB CLI GitHub Repository.