Bagikan melalui


Membuat keyspace dan tabel dengan skala otomatis untuk Azure Cosmos DB - API untuk Cassandra

BERLAKU UNTUK: Cassandra

Catatan

Sebaiknya Anda menggunakan modul Azure Az PowerShell untuk berinteraksi dengan Azure. Untuk memulai, 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.

Sampel skrip

# 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 with autoscale throughput
# --------------------------------------------------
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 = "mjbArmTest"#"myResourceGroup" # Resource Group must already exist
$accountName = "cosmos-$uniqueId" # Must be all lower case
$consistencyLevel = "Session"
$keyspaceName = "mykeyspace"
$tableName = "mytable"
$autoscaleMaxThroughput = 4000 #minimum = 4000
$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 `
    -DefaultConsistencyLevel $consistencyLevel `
    -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 -AutoscaleMaxThroughput  $autoscaleMaxThroughput 

Bersihkan 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 ditautkan ke dokumentasi spesifik perintah.

Perintah Catatan
Azure Cosmos DB
New-AzCosmosDBAccount Membuat Akun Azure Cosmos DB.
New-AzCosmosDBCassandraKeyspace Membuat Azure Cosmos DB untuk Apache Cassandra Keyspace.
New-AzCosmosDBCassandraClusterKey Membuat Azure Cosmos DB untuk Kunci Kluster Apache Cassandra.
New-AzCosmosDBCassandraColumn Membuat Azure Cosmos DB untuk Kolom Apache Cassandra.
New-AzCosmosDBCassandraSchema Membuat Azure Cosmos DB untuk Apache Cassandra Schema.
New-AzCosmosDBCassandraTable Membuat Azure Cosmos DB untuk Tabel Apache Cassandra.
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