Share via


Azure CLI を使用して、Cassandra 用 API アカウント、キースペース、自動スケーリングするテーブルを作成する

適用対象: Cassandra

この記事のスクリプトでは、Azure Cosmos DB for Apache Cassandra のアカウント、キースペース、自動スケーリングするテーブルが作成されます。

前提条件

  • 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 にサインインします。

サンプル スクリプト

このスクリプトでは以下のコマンドを使用します。

  • az group create: すべてのリソースを格納するリソース グループが作成されます。
  • az cosmosdb create (--capabilities EnableCassandra パラメーターを指定): Cassandra 対応の Azure Cosmos DB アカウント用の API が作成されます。
  • az cosmosdb cassandra keyspace create: Azure Cosmos DB の Cassandra キースペースが作成されます。
  • az cosmosdb cassandra table create (--max-throughput パラメーターに最小の 4000 を指定): 自動スケールする Azure CosmosDB Cassandra テーブルが作成されます。
# 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 アカウントとキースペースを含む) を削除します。

az group delete --name $resourceGroup

次の手順

Azure Cosmos DB CLI のドキュメント