使用 Azure CLI 创建 Azure Cosmos DB for Table 无服务器帐户和表

适用对象:

本文中的脚本可创建 Azure Cosmos DB for Table 无服务器帐户和表。

先决条件

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

  • 此脚本需要使用 Azure CLI 2.12.1 或更高版本。 运行 az --version 即可查找版本。 如果需要进行安装或升级,请参阅安装 Azure CLI

    注意

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

    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 serverless account and table

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

# 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 EnableServerless --default-consistency-level Eventual --locations regionName="$location" failoverPriority=0 isZoneRedundant=False \

# Create a Table API Table
az cosmosdb table create --account-name $account --resource-group $resourceGroup --name $table

此脚本使用以下命令:

清理资源

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

az group delete --name $resourceGroup

后续步骤

Azure Cosmos DB CLI 文档