Use Azure CLI to create a API for Cassandra account, keyspace, and table with autoscale

APPLIES TO: Cassandra

The script in this article creates an Azure Cosmos DB for Apache Cassandra account, keyspace, and table with autoscale.

Prerequisites

  • If you don't have an Azure subscription, create an Azure free account before you begin.

  • This script requires Azure CLI version 2.12.1 or later.

    • You can run the script in the Bash environment in Azure Cloud Shell. When Cloud Shell opens, make sure to select Bash in the environment field at the upper left of the shell window. Cloud Shell has the latest version of Azure CLI.

    • If you prefer, you can install Azure CLI to run the script locally. Run az version to find your Azure CLI version, and run az upgrade if you need to upgrade. Sign in to Azure by running az login.

Sample script

This script uses the following commands:

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

Delete resources

If you don't need the resources you created, use the az group delete command to delete the resource group and all resources it contains, including the Azure Cosmos DB account and keyspace.

az group delete --name $resourceGroup

Next steps

Azure Cosmos DB CLI documentation