你当前正在访问 Microsoft Azure Global Edition 技术文档网站。 如果需要访问由世纪互联运营的 Microsoft Azure 中国技术文档网站,请访问 https://docs.azure.cn

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

适用对象:

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

先决条件

  • 如果没有 Azure 订阅,请在开始之前创建一个 Azure 免费帐户

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

    • 可以在 Azure Cloud Shell的 Bash 环境中运行脚本。 Cloud Shell 打开时,请确保 Bash 显示在 shell 窗口左上角的环境字段中。 Cloud Shell 始终有最新版本的 Azure CLI。

      Cloud Shell 会自动使用你在登录 Azure 门户时使用的帐户进行身份验证。 可以使用 az account set 通过其他订阅登录,将 <subscriptionId> 替换为你的 Azure 订阅 ID。

      subscription="<subscriptionId>" # add subscription here
      
      az account set -s $subscription # ...or use 'az login'
      
    • 如果愿意,可以安装 Azure CLI 以在本地运行脚本。 运行 az version 以查找安装的 Azure CLI 版本和依赖库。如果需要升级,请运行 az upgrade安装 Azure CLI 扩展(如果系统提示)。 如果你是在运行 Windows 或 macOS,请考虑在 Docker 容器中运行 Azure CLI

      如果使用的是本地安装,请通过运行 az login 并按提示操作登录到 Azure。 有关其他登录选项,请参阅使用 Azure CLI 登录

示例脚本

运行以下脚本以创建 Azure 资源组、Azure Cosmos DB for Table 无服务器帐户和 API for Table 表。 资源的创建可能需要一点时间。

# Create a Table API serverless account and table

# Variable block
let "randomIdentifier=$RANDOM*$RANDOM"
location="East US"
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 文档