使用 Azure CLI 建立適用於無伺服器數據表帳戶和數據表的 Azure Cosmos DB

適用於:

本文中的腳本會建立適用於無伺服器數據表帳戶和數據表的 Azure Cosmos DB。

必要條件

  • 如果您沒有 Azure 訂用帳戶,請在開始之前先建立 Azure 免費帳戶

  • 此指令碼需要 Azure CLI 2.12.1 版或更新版本。

    • 您可以在 Azure Cloud Shell 的 Bash 環境中執行指令碼。 Cloud Shell 開啟時,請確定在殼層視窗左上方的環境欄位中出現 [Bash]。 Cloud Shell 一律會具有最新版的 Azure CLI。

      Cloud Shell 會使用您用來登入 Azure 入口網站的帳戶自動進行驗證。 您可以使用 az account set 以不同的訂用帳戶來登入,並將 <subscriptionId> 取代為您的 Azure 訂用帳戶識別碼。

      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,以及數據表數據表的 API。 資源的建立可能需要一些時間。

# 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 文件