Share via


使用 Azure CLI 建立 API for Cassandra 帳戶、Keyspace 和具有自動調整功能的資料表

適用於: Cassandra

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

必要條件

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

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

    • 您可以在 Azure Cloud Shell 的 Bash 環境中執行指令碼。 Cloud Shell 開啟時,請務必在殼層視窗左上方的環境欄位中選取 Bash。 Cloud Shell 具有最新版的 Azure CLI。

    • 您可以視需要安裝 Azure CLI 以在本機執行指令碼。 執行 az version 以找出您的 Azure CLI 版本,如果需要升級,請執行 az upgrade。 執行 az login 以登入 Azure。

範例指令碼

此指令碼會使用下列命令:

# Create a Cassandra keyspace and table with autoscale

# Variable block
let "randomIdentifier=$RANDOM*$RANDOM"
location="East US"
resourceGroup="msdocs-cosmosdb-rg-$randomIdentifier"
tag="autoscale-casandra-cosmosdb"
account="msdocs-account-cosmos-$randomIdentifier" #needs to be lower case
keySpace="keyspace1-$randomIdentifier"
table="table1-$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 Cassandra API
echo "Creating $account"
az cosmosdb create --name $account --resource-group $resourceGroup --capabilities EnableCassandra --default-consistency-level Eventual --locations regionName="$location" failoverPriority=0 isZoneRedundant=False 

# Create a Cassandra Keyspace
echo "Create $keySpace"
az cosmosdb cassandra keyspace create --account-name $account --resource-group $resourceGroup --name $keySpace

# Define the schema for the table
schema=$(cat << EOF 
{
    "columns": [
        {"name": "columna","type": "uuid"},
        {"name": "columnb","type": "int"},
        {"name": "columnc","type": "text"}
    ],
    "partitionKeys": [
        {"name": "columna"}
    ],
    "clusterKeys": [
        { "name": "columnb", "orderBy": "asc" }
    ]
}
EOF
)
# Persist schema to json file
echo "$schema" > "schema-$randomIdentifier.json"

# Create the Cassandra table
echo "Creating $table"
az cosmosdb cassandra table create --account-name $account --resource-group $resourceGroup --keyspace-name $keySpace --name $table --max-throughput $maxThroughput --schema @schema-$randomIdentifier.json

# Clean up temporary schema file
rm -f "schema-$randomIdentifier.json"

刪除資源

如果您不需要已建立的資源,請使用 az group delete 命令來刪除資源群組及其包含的所有資源,包括 Azure Cosmos DB 帳戶和 Keyspace。

az group delete --name $resourceGroup

下一步

Azure Cosmos DB CLI 文件