Skapa en databas och container för Azure Cosmos DB – API för NoSQL

GÄLLER FÖR: NoSQL

Anteckning

Vi rekommenderar att du använder Azure Az PowerShell-modulen för att interagera med Azure. Se Installera Azure PowerShell för att komma igång. Information om hur du migrerar till Az PowerShell-modulen finns i artikeln om att migrera Azure PowerShell från AzureRM till Az.

Det här exemplet kräver Azure PowerShell Az 5.4.0 eller senare. Kör Get-Module -ListAvailable Az för att se vilka versioner som är installerade. Om du behöver installera kan du läsa Installera Azure PowerShell modul.

Kör Connect-AzAccount för att logga in på Azure.

Exempelskript

Det här skriptet skapar ett Azure Cosmos DB-konto för API för NoSQL i två regioner med konsekvens på sessionsnivå, en databas och en container med en partitionsnyckel, anpassad indexeringsprincip, unik nyckelprincip, TTL, dedikerat dataflöde och den senaste skrivaren vinner konfliktlösningsprincip med en anpassad konfliktlösningssökväg som används när multipleWriteLocations=true.

# Reference: Az.CosmosDB | https://docs.microsoft.com/powershell/module/az.cosmosdb
# --------------------------------------------------
# Purpose
# Create Cosmos SQL API account, database, and container with dedicated throughput,
# indexing policy with include, exclude, and composite paths, unique key, and conflict resolution
# --------------------------------------------------
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 = "Sql"
# --------------------------------------------------
# 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 = "Session"
$tags = @{Tag1 = "MyTag1"; Tag2 = "MyTag2"; Tag3 = "MyTag3"}
$databaseName = "myDatabase"
$containerName = "myContainer"
$containerRUs = 400
$partitionKeyPath = "/myPartitionKey"
$indexPathIncluded = "/*"
$compositeIndexPaths1 = @(
    @{ Path = "/myCompositePath1"; Order = "ascending" };
    @{ Path = "/myCompositePath2"; Order = "descending" }
)
$compositeIndexPaths2 = @(
    @{ Path = "/myCompositePath3"; Order = "ascending" };
    @{ Path = "/myCompositePath4"; Order = "descending" }
)
$indexPathExcluded = "/myExcludedPath/*"
$uniqueKeyPath = "/myUniqueKeyPath"
$conflictResolutionPath = "/myResolutionPath"
$ttlInSeconds = 120 # Set this to -1 (or don't use it at all) to never expire
# --------------------------------------------------
Write-Host "Creating account $accountName"

$account = New-AzCosmosDBAccount -ResourceGroupName $resourceGroupName `
    -LocationObject $locations -Name $accountName -ApiKind $apiKind -Tag $tags `
    -DefaultConsistencyLevel $consistencyLevel `
    -EnableAutomaticFailover:$true

Write-Host "Creating database $databaseName"
$database = New-AzCosmosDBSqlDatabase -ParentObject $account -Name $databaseName

$uniqueKey = New-AzCosmosDBSqlUniqueKey -Path $uniqueKeyPath
$uniqueKeyPolicy = New-AzCosmosDBSqlUniqueKeyPolicy -UniqueKey $uniqueKey

$compositePath1 = @()
ForEach ($compositeIndexPath in $compositeIndexPaths1) {
    $compositePath1 += New-AzCosmosDBSqlCompositePath `
        -Path $compositeIndexPath.Path `
        -Order $compositeIndexPath.Order
}

$compositePath2 = @()
ForEach ($compositeIndexPath in $compositeIndexPaths2) {
    $compositePath2 += New-AzCosmosDBSqlCompositePath `
        -Path $compositeIndexPath.Path `
        -Order $compositeIndexPath.Order
}

$includedPathIndex = New-AzCosmosDBSqlIncludedPathIndex -DataType String -Kind Range
$includedPath = New-AzCosmosDBSqlIncludedPath -Path $indexPathIncluded -Index $includedPathIndex

$indexingPolicy = New-AzCosmosDBSqlIndexingPolicy `
    -IncludedPath $includedPath `
    -CompositePath @($compositePath1, $compositePath2) `
    -ExcludedPath $indexPathExcluded `
    -IndexingMode Consistent -Automatic $true

# Conflict resolution policies only apply in multi-master accounts.
# Included here to show custom resolution path.
$conflictResolutionPolicy = New-AzCosmosDBSqlConflictResolutionPolicy `
    -Type LastWriterWins -Path $conflictResolutionPath

Write-Host "Creating container $containerName"
$container = New-AzCosmosDBSqlContainer `
    -ParentObject $database -Name $containerName `
    -Throughput $containerRUs -IndexingPolicy $indexingPolicy `
    -PartitionKeyKind Hash -PartitionKeyPath $partitionKeyPath `
    -UniqueKeyPolicy $uniqueKeyPolicy `
    -ConflictResolutionPolicy $conflictResolutionPolicy `
    -TtlInSeconds $ttlInSeconds

Rensa distribution

När skriptexemplet har körts kan följande kommando användas för att ta bort resursgruppen och alla resurser som är kopplade till den.

Remove-AzResourceGroup -ResourceGroupName "myResourceGroup"

Förklaring av skript

Det här skriptet använder följande kommandon. Varje kommando i tabellen länkar till kommandospecifik dokumentation.

Kommando Kommentarer
Azure Cosmos DB
New-AzCosmosDBAccount Skapar ett Azure Cosmos DB-konto.
New-AzCosmosDBSqlDatabase Skapar en Azure Cosmos DB-SQL Database.
New-AzCosmosDBSqlUniqueKey Skapar ett PSSqlUniqueKey-objekt som används som parameter för New-AzCosmosDBSqlUniqueKeyPolicy.
New-AzCosmosDBSqlUniqueKeyPolicy Skapar ett PSSqlUniqueKeyPolicy-objekt som används som parameter för New-AzCosmosDBSqlContainer.
New-AzCosmosDBSqlCompositePath Skapar ett PSCompositePath-objekt som används som parameter för New-AzCosmosDBSqlIndexingPolicy.
New-AzCosmosDBSqlIncludedPathIndex Skapar ett PSIndexes-objekt som används som parameter för New-AzCosmosDBSqlIncludedPath.
New-AzCosmosDBSqlIncludedPath Skapar ett PSIncludedPath-objekt som används som parameter för New-AzCosmosDBSqlIndexingPolicy.
New-AzCosmosDBSqlIndexingPolicy Skapar ett PSSqlIndexingPolicy-objekt som används som parameter för New-AzCosmosDBSqlContainer.
New-AzCosmosDBSqlConflictResolutionPolicy Skapar ett PSSqlConflictResolutionPolicy-objekt som används som parameter för New-AzCosmosDBSqlContainer.
New-AzCosmosDBSqlContainer Skapar en ny Azure Cosmos DB SQL-container.
Azure-resursgrupper
Remove-AzResourceGroup Tar bort en resursgrupp, inklusive alla kapslade resurser.

Nästa steg

Mer information om Azure PowerShell finns i Azure PowerShell-dokumentationen.