Delen via


Azure CLI gebruiken om een API te maken voor cassandra-account, keyspace en tabel met automatische schaalaanpassing

VAN TOEPASSING OP: Cassandra

Met het script in dit artikel maakt u een Azure Cosmos DB voor Apache Cassandra-account, keyspace en tabel met automatische schaalaanpassing.

Vereisten

  • Als u geen Azure-abonnement hebt, kunt u een gratis Azure-account maken voordat u begint.

  • Voor dit script is Azure CLI versie 2.12.1 of hoger vereist.

    • U kunt het script uitvoeren in de Bash-omgeving in Azure Cloud Shell. Wanneer Cloud Shell wordt geopend, moet u Bash selecteren in het omgevingsveld linksboven in het shell-venster. Cloud Shell heeft de nieuwste versie van Azure CLI.

    • Als u wilt, kunt u Azure CLI installeren om het script lokaal uit te voeren. Voer az-versie uit om uw Azure CLI-versie te vinden en voer az upgrade uit als u een upgrade wilt uitvoeren. Meld u aan bij Azure door az login uit te voeren.

Voorbeeldscript

In dit script worden de volgende opdrachten gebruikt:

# 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"

Resources verwijderen

Als u de resources die u hebt gemaakt niet nodig hebt, gebruikt u de opdracht az group delete om de resourcegroep en alle resources die deze bevat te verwijderen, inclusief het Azure Cosmos DB-account en keyspace.

az group delete --name $resourceGroup

Volgende stappen

Documentatie voor Azure Cosmos DB CLI