使用 Azure CLI 创建 Azure Cosmos DB for Table 帐户和表(具有自动缩放功能)

适用对象:

本文中的脚本会创建 Azure Cosmos DB for Table 帐户和表(具有自动缩放功能)。

先决条件

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

  • 此脚本需要使用 Azure CLI 2.12.1 或更高版本。

    注意

    在可以在由世纪互联运营的 Microsoft Azure 中使用 Azure CLI 之前,请先运行 az cloud set -n AzureChinaCloud 来更改云环境。 若要切换回 Azure 公有云,请再次运行 az cloud set -n AzureCloud

    可以使用 az account set 通过其他订阅登录,将 <subscriptionId> 替换为你的 Azure 订阅 ID。

    subscription="<subscriptionId>" # add subscription here
    
    az account set -s $subscription # ...or use 'az login'
    

示例脚本

运行以下脚本以创建 Azure 资源组、Azure Cosmos DB for Table 帐户和 API for Table 表(具有自动缩放功能)。 资源的创建可能需要一点时间。

# Create a Table API table with autoscale

# Variable block
let "randomIdentifier=$RANDOM*$RANDOM"
location="chinaeast2"
resourceGroup="msdocs-cosmosdb-rg-$randomIdentifier"
tag="autoscale-table-cosmosdb"
account="msdocs-account-cosmos-$randomIdentifier" #needs to be lower case
table="msdocs-table-cosmos-$randomIdentifier"
maxThroughput=1000 #minimum = 1000

# Create a resource group
echo "Creating $resourceGroup in $location..."
az group create --name $resourceGroup --location "$location" --tags $tag

# Create a Cosmos account for Table API
echo "Creating $account"
az cosmosdb create --name $account --resource-group $resourceGroup --capabilities EnableTable --default-consistency-level Eventual --locations regionName="$location" failoverPriority=0 isZoneRedundant=False

# Create a Table API Table with autoscale
echo "Create $table with $maxThroughput"
az cosmosdb table create --account-name $account --resource-group $resourceGroup --name $table --max-throughput $maxThroughput

此脚本使用以下命令:

  • az group create 创建资源组来存储所有资源。
  • az cosmosdb create--capabilities EnableTable 配合使用可创建适用于 API for Table 的 Azure Cosmos DB 帐户。
  • az cosmosdb table create--max-throughput 1000 配合使用可创建 Azure Cosmos DB for Table 表(具有自动缩放功能)。

清理资源

如果不再需要创建的资源,请使用 az group delete 命令删除资源组及其包含的所有资源。 这些资源包括 Azure Cosmos DB 帐户和表。 资源的删除可能需要一点时间。

az group delete --name $resourceGroup

后续步骤