Share via


使用 Azure CLI 建立具有自動調整功能的 Azure Cosmos DB for Table 帳戶和資料表

適用於: Table

本文中的指令碼會建立具有自動調整功能的 Azure Cosmos DB for Table 帳戶和資料表。

必要條件

  • 如果您沒有 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 for Table 帳戶,以及具有自動調整功能的 API for Table 資料表。 資源的建立可能需要一些時間。

# Create a Table API table with autoscale

# Variable block
let "randomIdentifier=$RANDOM*$RANDOM"
location="East US"
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 會建立資源群組來儲存所有資源。
  • 含有 --capabilities EnableTableaz cosmosdb create 會建立適用於 API for Table 的 Azure Cosmos DB 帳戶。
  • 含有 --max-throughput 1000az cosmosdb table create 會建立具有自動調整功能的 Azure Cosmos DB for Table 資料表。

清除資源

如果您不再需要您所建立的資源,則請使用 az group delete 命令來刪除資源群組和其包含的所有資源。 這些資源包括 Azure Cosmos DB 帳戶和資料表。 資源的刪除可能需要一些時間。

az group delete --name $resourceGroup

下一步