Kulcstér és tábla létrehozása az Azure Cosmos DB-hez – API a Cassandra számára

A KÖVETKEZŐKRE VONATKOZIK: Cassandra

Megjegyzés

Javasoljuk, hogy az Azure Az PowerShell-modult használja az Azure-ral való kommunikációhoz. Az első lépésekhez tekintse meg az Azure PowerShell telepítését ismertető szakaszt. Az Az PowerShell-modulra történő migrálás részleteiről lásd: Az Azure PowerShell migrálása az AzureRM modulból az Az modulba.

Ehhez a mintához Azure PowerShell Az 5.4.0-s vagy újabb verzióra van szükség. Futtassa a parancsot Get-Module -ListAvailable Az a telepített verziók megtekintéséhez. Ha telepítenie kell, olvassa el a Azure PowerShell telepítése modult.

Az Azure-ba való bejelentkezéshez futtassa a Connect-AzAccount parancsot .

Példaszkript

# 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 

Az üzemelő példány eltávolítása

A példaszkript futtatása után a következő paranccsal távolítható el az erőforráscsoport és az összes ahhoz kapcsolódó erőforrás.

Remove-AzResourceGroup -ResourceGroupName "myResourceGroup"

Szkript ismertetése

A szkript a következő parancsokat használja. A táblázatban lévő összes parancs a hozzá tartozó dokumentációra hivatkozik.

Parancs Jegyzetek
Azure Cosmos DB
New-AzCosmosDBAccount Létrehoz egy Azure Cosmos DB-fiókot.
New-AzCosmosDBCassandraKeyspace Létrehoz egy Azure Cosmos DB-t az Apache Cassandra Keyspace-hez.
New-AzCosmosDBCassandraClusterKey Létrehoz egy Azure Cosmos DB-t az Apache Cassandra fürtkulcsához.
New-AzCosmosDBCassandraColumn Létrehoz egy Azure Cosmos DB-t az Apache Cassandra-oszlophoz.
New-AzCosmosDBCassandraSchema Létrehoz egy Azure Cosmos DB-t az Apache Cassandra-sémához.
New-AzCosmosDBCassandraTable Létrehoz egy Azure Cosmos DB-t az Apache Cassandra-táblához.
Azure-erőforráscsoportok
Remove-AzResourceGroup Töröl egy erőforráscsoportot az összes beágyazott erőforrással együtt.

Következő lépések

Az Azure PowerShellről további tudnivalókért tekintse meg az Azure PowerShell dokumentációt.