Skapa en container med en stor partitionsnyckel i ett Azure Cosmos DB-konto med hjälp av PowerShell

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

# Reference: Az.CosmosDB | https://docs.microsoft.com/powershell/module/az.cosmosdb
# --------------------------------------------------
# Purpose
# Create Cosmos DB SQL API account, database, and container with dedicated throughput and no indexing policy
# --------------------------------------------------
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"
# --------------------------------------------------
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

Write-Host "Creating container $containerName"
$container = New-AzCosmosDBSqlContainer `
    -ParentObject $database -Name $containerName `
    -Throughput $containerRUs -PartitionKeyVersion 2 `
    -PartitionKeyKind Hash -PartitionKeyPath $partitionKeyPath

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-AzCosmosDBSqlContainer Skapar en 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.