Azure Cosmos DB:使用 Azure CLI 创建 Cassandra API 帐户Azure Cosmos DB: Create a Cassandra API account using Azure CLI

此示例 CLI 脚本创建了 Azure Cosmos DB Cassandra API 帐户、数据库和表。This sample CLI script creates an Azure Cosmos DB Cassandra API account, database, and table.


在 Azure China 中使用 Azure CLI 2.0 之前,请首先运行 az cloud set -n AzureChinaCloud 更改云环境。Before you can use Azure CLI 2.0 in Azure China, please run az cloud set -n AzureChinaCloud first to change the cloud environment. 如果要切换回全局 Azure,请再次运行 az cloud set -n AzureCloudIf you want to switch back to Global Azure, run az cloud set -n AzureCloud again.

如果选择在本地安装并使用 CLI,本主题要求运行 Azure CLI 2.0 版或更高版本。If you choose to install and use the CLI locally, this topic requires that you are running the Azure CLI version 2.0 or later. 运行 az --version 即可查找版本。Run az --version to find the version. 如果需要进行安装或升级,请参阅安装 Azure CLIIf you need to install or upgrade, see Install Azure CLI.

示例脚本Sample script


# Set variables for the new Cassandra API account, database, and table
accountName='myaccountname' #needs to be lower case

# Create a resource group
az group create \
    --name $resourceGroupName \
    --location $location

# Create a Cassandra API Cosmos DB account with consistent prefix (LOCAL_ONE) consistency and multi-master enabled
az cosmosdb create \
    --resource-group $resourceGroupName \
    --name $accountName \
    --capabilities EnableCassandra \
    --locations regionName="China East" failoverPriority=0 \
    --locations regionName="China North" failoverPriority=1 \
    --default-consistency-level "ConsistentPrefix" \
    --enable-multiple-write-locations true

# Create a database
az cosmosdb database create \
    --resource-group $resourceGroupName \
    --name $accountName \
    --db-name $keysetName


Azure CLI 不直接支持创建 Cassandra 表。Azure CLI does not directly support creating a Cassandra table. 请使用 Azure 门户或 CQL Shell。Use Azure Portal or CQL Shell.

清理部署Clean up deployment

运行脚本示例后,可以使用以下命令删除资源组以及与其关联的所有资源。After the script sample has been run, the following command can be used to remove the resource group and all resources associated with it.

az group delete --name $resourceGroupName

脚本说明Script explanation

此脚本使用以下命令。This script uses the following commands. 表中的每条命令均链接到特定于命令的文档。Each command in the table links to command specific documentation.

命令Command 注释Notes
az group createaz group create 创建用于存储所有资源的资源组。Creates a resource group in which all resources are stored.
az cosmosdb createaz cosmosdb create 创建 Azure Cosmos DB 帐户。Creates an Azure Cosmos DB account.
az cosmosdb database createaz cosmosdb database create 创建 Azure Cosmos 数据库。Creates an Azure Cosmos database.
az group deleteaz group delete 删除资源组,包括所有嵌套的资源。Deletes a resource group including all nested resources.

后续步骤Next steps

有关 Azure CLI 的详细信息,请参阅 Azure CLI 文档For more information on the Azure CLI, see Azure CLI documentation.

有关其他 Azure Cosmos DB CLI 脚本示例,请参见 Azure Cosmos DB CLI 文档Additional Azure Cosmos DB CLI script samples can be found in the Azure Cosmos DB CLI documentation.