Używanie interfejsu wiersza polecenia platformy Azure do tworzenia interfejsu API dla konta Cassandra, przestrzeni kluczy i tabeli z autoskalowaniem

DOTYCZY: Cassandra

Skrypt w tym artykule tworzy usługę Azure Cosmos DB dla konta, przestrzeni kluczy i tabeli apache Cassandra z autoskalowaniem.

Wymagania wstępne

Przykładowy skrypt

W tym skrypcie użyto następujących poleceń:

  • az group create tworzy grupę zasobów do przechowywania wszystkich zasobów.
  • az cosmosdb create z parametrem --capabilities EnableCassandra tworzy interfejs API dla konta usługi Azure Cosmos DB z obsługą bazy danych Cassandra.
  • az cosmosdb cassandra keyspace create tworzy przestrzeń kluczy Cassandra usługi Azure Cosmos DB.
  • az cosmosdb cassandra table create with the --max-throughput parameter set to minimum create an Azure Cosmos DB Cassandra table with autoscale (Az cosmosdb cassandra table with the parameter set to minimum 4000 create an Azure Cosmos DB Cassandra table with autoscale).
# 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"

Usuwanie zasobów

Jeśli nie potrzebujesz utworzonych zasobów, użyj polecenia az group delete , aby usunąć grupę zasobów i wszystkie zawarte w niej zasoby, w tym konto usługi Azure Cosmos DB i przestrzeń kluczy.

az group delete --name $resourceGroup

Następne kroki

Dokumentacja interfejsu wiersza polecenia usługi Azure Cosmos DB