Azure Cosmos DB - Cassandra 用 API のキースペースとテーブルを作成する

適用対象: Cassandra

注意

Azure を操作するには、Azure Az PowerShell モジュールを使用することをお勧めします。 作業を開始するには、Azure PowerShell のインストールに関する記事を参照してください。 Az PowerShell モジュールに移行する方法については、「AzureRM から Az への Azure PowerShell の移行」を参照してください。

このサンプルには、Azure PowerShell Az 5.4.0 以降が必要です。 Get-Module -ListAvailable Az を実行して、インストールされているバージョンを確認します。 インストールする必要がある場合は、Azure PowerShell モジュールのインストールに関するページを参照してください。

Connect-AzAccount を実行して Azure にサインインします。

サンプル スクリプト

# Reference: Az.CosmosDB | https://docs.microsoft.com/powershell/module/az.cosmosdb
# --------------------------------------------------
# Purpose
# Create Cosmos Cassandra API account with automatic failover,
# a keyspace, and a table with defined schema, dedicated throughput, and
# conflict resolution policy with last writer wins and custom resolver path.
# --------------------------------------------------
Function New-RandomString{Param ([Int]$Length = 10) return $(-join ((97..122) + (48..57) | Get-Random -Count $Length | ForEach-Object {[char]$_}))}
# --------------------------------------------------
$uniqueId = New-RandomString -Length 7 # Random alphanumeric string for unique resource names
$apiKind = "Cassandra"
# --------------------------------------------------
# Variables - ***** SUBSTITUTE YOUR VALUES *****
$locations = @()
$locations += New-AzCosmosDBLocationObject -LocationName "East Us" -FailoverPriority 0 -IsZoneRedundant 0
$locations += New-AzCosmosDBLocationObject -LocationName "West Us" -FailoverPriority 1 -IsZoneRedundant 0

$resourceGroupName = "myResourceGroup" # Resource Group must already exist
$accountName = "cosmos-$uniqueId" # Must be all lower case
$consistencyLevel = "BoundedStaleness"
$maxStalenessInterval = 300
$maxStalenessPrefix = 100000
$tags = @{Tag1 = "MyTag1"; Tag2 = "MyTag2"; Tag3 = "MyTag3"}
$keyspaceName = "mykeyspace"
$tableName = "mytable"
$tableRUs = 400
$partitionKeys = @("machine", "cpu", "mtime")
$clusterKeys = @( 
    @{ name = "loadid"; orderBy = "Asc" };
    @{ name = "duration"; orderBy = "Desc" }
)
$columns = @(
    @{ name = "loadid"; type = "uuid" };
    @{ name = "machine"; type = "uuid" };
    @{ name = "cpu"; type = "int" };
    @{ name = "mtime"; type = "int" };
    @{ name = "load"; type = "float" };
    @{ name = "duration"; type = "float" }
)
# --------------------------------------------------
Write-Host "Creating account $accountName"
$account = New-AzCosmosDBAccount -ResourceGroupName $resourceGroupName `
    -LocationObject $locations -Name $accountName -ApiKind $apiKind -Tag $tags `
    -DefaultConsistencyLevel $consistencyLevel `
    -MaxStalenessIntervalInSeconds $maxStalenessInterval `
    -MaxStalenessPrefix $maxStalenessPrefix `
    -EnableAutomaticFailover:$true

Write-Host "Creating keyspace $keyspaceName"
$keyspace = New-AzCosmosDBCassandraKeyspace -ParentObject $account `
    -Name $keyspaceName

# Table Schema
$psClusterKeys = @()
ForEach ($clusterKey in $clusterKeys) {
    $psClusterKeys += New-AzCosmosDBCassandraClusterKey -Name $clusterKey.name -OrderBy $clusterKey.orderBy
}

$psColumns = @()
ForEach ($column in $columns) {
    $psColumns += New-AzCosmosDBCassandraColumn -Name $column.name -Type $column.type
}

$schema = New-AzCosmosDBCassandraSchema `
    -PartitionKey $partitionKeys `
    -ClusterKey $psClusterKeys `
    -Column $psColumns

Write-Host "Creating table $tableName"
$table = New-AzCosmosDBCassandraTable -ParentObject $keyspace `
    -Name $tableName -Schema $schema -Throughput $tableRUs 

デプロイのクリーンアップ

スクリプト サンプルの実行後は、次のコマンドを使用してリソース グループとすべての関連リソースを削除することができます。

Remove-AzResourceGroup -ResourceGroupName "myResourceGroup"

スクリプトの説明

このスクリプトでは、次のコマンドを使用します。 表内の各コマンドは、それぞれのドキュメントにリンクされています。

コマンド Notes
Azure Cosmos DB
New-AzCosmosDBAccount Azure Cosmos DB アカウントを作成します。
New-AzCosmosDBCassandraKeyspace Azure Cosmos DB for Apache Cassandra のキースペースを作成します。
New-AzCosmosDBCassandraClusterKey Azure Cosmos DB for Apache Cassandra のクラスター キーを作成します。
New-AzCosmosDBCassandraColumn Azure Cosmos DB for Apache Cassandra の列を作成します。
New-AzCosmosDBCassandraSchema Azure Cosmos DB for Apache Cassandra のスキーマを作成します。
New-AzCosmosDBCassandraTable Azure Cosmos DB for Apache Cassandra のテーブルを作成します。
Azure リソース グループ
Remove-AzResourceGroup 入れ子になったリソースすべてを含むリソース グループを削除します。

次のステップ

Azure PowerShell の詳細については、Azure PowerShell のドキュメントを参照してください。