Membuat keyspace dan tabel untuk Cassandra API - Azure Cosmos DB
BERLAKU UNTUK:
Cassandra API
Catatan
Artikel ini menggunakan modul Azure Az PowerShell, yang merupakan modul PowerShell yang direkomendasikan untuk berinteraksi dengan Azure. Untuk mulai menggunakan modul Az PowerShell, lihat Menginstal Azure PowerShell. Untuk mempelajari cara bermigrasi ke modul Az PowerShell, lihat Memigrasikan Azure PowerShell dari AzureRM ke Az.
Sampel ini memerlukan Azure PowerShell Az 5.4.0 atau yang lebih baru. Jalankan Get-Module -ListAvailable Az untuk melihat versi mana yang dipasang.
Jika Anda perlu memasang, lihat Pasang modul Azure PowerShell.
Jalankan Connect-AzAccount untuk masuk ke Azure.
Skrip sampel
# 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
Membersihkan penyebaran
Setelah sampel skrip dijalankan, perintah berikut dapat digunakan untuk menghapus grup sumber daya dan semua sumber daya yang terkait.
Remove-AzResourceGroup -ResourceGroupName "myResourceGroup"
Penjelasan skrip
Skrip ini menggunakan perintah berikut. Setiap perintah dalam tabel menautkan ke dokumentasi perintah tertentu.
| Perintah | Catatan |
|---|---|
| Azure Cosmos DB | |
| New-AzCosmosDBAccount | Membuat Akun Cosmos DB. |
| New-AzCosmosDBCassandraKeyspace | Membuat Keyspace API Cassandra Cosmos DB. |
| New-AzCosmosDBCassandraClusterKey | Membuat Kunci Kluster API Cassandra Cosmos DB. |
| New-AzCosmosDBCassandraColumn | Membuat Kolom API Cassandra Cosmos DB. |
| New-AzCosmosDBCassandraSchema | Membuat Skema API Cassandra Cosmos DB. |
| New-AzCosmosDBCassandraTable | Membuat Tabel API Cassandra Cosmos DB. |
| Grup Sumber Daya Azure | |
| Remove-AzResourceGroup | Menghapus grup sumber daya termasuk semua sumber daya berlapis. |
Langkah berikutnya
Untuk informasi selengkapnya tentang Azure PowerShell, lihat dokumentasi Azure PowerShell.