Hantera Azure Cosmos DB för NoSQL-resurser med hjälp av PowerShell

GÄLLER FÖR: NoSQL

I följande guide beskrivs hur du använder PowerShell för att skripta och automatisera hanteringen av Azure Cosmos DB för NoSQL-resurser, inklusive Azure Cosmos DB-kontot, databasen, containern och dataflödet. PowerShell-cmdletar för andra API:er finns i PowerShell-exempel för Cassandra, PowerShell-exempel för API för MongoDB, PowerShell-exempel för Gremlin, PowerShell-exempel för tabell

Kommentar

Exempel i den här artikeln använder cmdletar för Az.CosmosDB-hantering . Se referenssidan för Az.CosmosDB API för de senaste ändringarna.

För plattformsoberoende hantering av Azure Cosmos DB kan du använda Az cmdletarna och Az.CosmosDB med plattformsoberoende PowerShell, samt Azure CLI, REST API eller Azure-portalen.

Kommentar

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.

Komma igång

Följ anvisningarna i Installera och konfigurera Azure PowerShell för att installera och logga in på ditt Azure-konto i PowerShell.

Viktigt!

Azure Cosmos DB-resurser kan inte byta namn eftersom det strider mot hur Azure Resource Manager fungerar med resurs-URI:er.

Azure Cosmos DB-konton

Följande avsnitt visar hur du hanterar Azure Cosmos DB-kontot, inklusive:

Skapa ett Azure Cosmos DB-konto

Det här kommandot skapar ett Azure Cosmos DB-databaskonto med flera regioner, tjänsthanterad redundans och konsekvensprincip för begränsad inaktuellhet.

$resourceGroupName = "myResourceGroup"
$accountName = "mycosmosaccount"
$apiKind = "Sql"
$consistencyLevel = "BoundedStaleness"
$maxStalenessInterval = 300
$maxStalenessPrefix = 100000
$locations = @()
$locations += New-AzCosmosDBLocationObject -LocationName "East US" -FailoverPriority 0 -IsZoneRedundant 0
$locations += New-AzCosmosDBLocationObject -LocationName "West US" -FailoverPriority 1 -IsZoneRedundant 0

New-AzCosmosDBAccount `
    -ResourceGroupName $resourceGroupName `
    -LocationObject $locations `
    -Name $accountName `
    -ApiKind $apiKind `
    -EnableAutomaticFailover:$true `
    -DefaultConsistencyLevel $consistencyLevel `
    -MaxStalenessIntervalInSeconds $maxStalenessInterval `
    -MaxStalenessPrefix $maxStalenessPrefix
  • $resourceGroupName Den Azure-resursgrupp som Azure Cosmos DB-kontot ska distribueras till. Det måste redan finnas.
  • $locations Regionerna för databaskontot, regionen med FailoverPriority 0 är skrivregionen.
  • $accountName Namnet på Azure Cosmos DB-kontot. Måste vara unikt, gemener, innehålla endast alfanumeriska och "-" tecken och mellan 3 och 31 tecken långa.
  • $apiKind Typen av Azure Cosmos DB-konto som ska skapas. Mer information finns i API:er i Azure Cosmos DB.
  • $consistencyPolicy, $maxStalenessIntervaloch $maxStalenessPrefix Standardkonsekvensnivån och inställningarna för Azure Cosmos DB-kontot. Mer information finns i Konsekvensnivåer i Azure Cosmos DB.

Azure Cosmos DB-konton kan konfigureras med IP-brandväggen, tjänstslutpunkter för virtuellt nätverk och privata slutpunkter. Information om hur du konfigurerar IP-brandväggen för Azure Cosmos DB finns i Konfigurera IP-brandvägg. Information om hur du aktiverar tjänstslutpunkter för Azure Cosmos DB finns i Konfigurera åtkomst från virtuella nätverk. Information om hur du aktiverar privata slutpunkter för Azure Cosmos DB finns i Konfigurera åtkomst från privata slutpunkter.

Visa en lista över alla Azure Cosmos DB-konton i en resursgrupp

Det här kommandot visar alla Azure Cosmos DB-konton i en resursgrupp.

$resourceGroupName = "myResourceGroup"

Get-AzCosmosDBAccount -ResourceGroupName $resourceGroupName

Hämta egenskaperna för ett Azure Cosmos DB-konto

Med det här kommandot kan du hämta egenskaperna för ett befintligt Azure Cosmos DB-konto.

$resourceGroupName = "myResourceGroup"
$accountName = "mycosmosaccount"

Get-AzCosmosDBAccount -ResourceGroupName $resourceGroupName -Name $accountName

Uppdatera ett Azure Cosmos DB-konto

Med det här kommandot kan du uppdatera egenskaperna för ditt Azure Cosmos DB-databaskonto. Egenskaper som kan uppdateras är följande:

  • Lägga till eller ta bort regioner
  • Ändra standardkonsekvensprincip
  • Ändra IP-intervallfilter
  • Ändra konfigurationer för virtuellt nätverk
  • Aktivera skrivningar i flera regioner

Kommentar

Du kan inte samtidigt lägga till eller ta bort regioner (locations) och ändra andra egenskaper för ett Azure Cosmos DB-konto. Att ändra regioner måste utföras som en separat åtgärd från andra ändringar av kontot.

Kommentar

Med det här kommandot kan du lägga till och ta bort regioner, men du kan inte ändra redundansprioriteringar eller utlösa en manuell redundansväxling. Se Ändra redundansprioritet och Utlösa manuell redundans.

Dricks

När en ny region läggs till måste alla data replikeras fullständigt och checkas in i den nya regionen innan regionen markeras som tillgänglig. Hur lång tid den här åtgärden tar beror på hur mycket data som lagras i kontot. Om en asynkron dataflödesskalning pågår pausas uppskalningsåtgärden för dataflöde och återupptas automatiskt när åtgärden för att lägga till/ta bort region är klar.

# Create account with two regions
$resourceGroupName = "myResourceGroup"
$accountName = "mycosmosaccount"
$apiKind = "Sql"
$consistencyLevel = "Session"
$enableAutomaticFailover = $true
$locations = @()
$locations += New-AzCosmosDBLocationObject -LocationName "East US" -FailoverPriority 0 -IsZoneRedundant 0
$locations += New-AzCosmosDBLocationObject -LocationName "West US" -FailoverPriority 1 -IsZoneRedundant 0

# Create the Azure Cosmos DB account
New-AzCosmosDBAccount `
    -ResourceGroupName $resourceGroupName `
    -LocationObject $locations `
    -Name $accountName `
    -ApiKind $apiKind `
    -EnableAutomaticFailover:$enableAutomaticFailover `
    -DefaultConsistencyLevel $consistencyLevel

# Add a region to the account
$locationObject2 = @()
$locationObject2 += New-AzCosmosDBLocationObject -LocationName "East US" -FailoverPriority 0 -IsZoneRedundant 0
$locationObject2 += New-AzCosmosDBLocationObject -LocationName "West US" -FailoverPriority 1 -IsZoneRedundant 0
$locationObject2 += New-AzCosmosDBLocationObject -LocationName "South Central US" -FailoverPriority 2 -IsZoneRedundant 0

Update-AzCosmosDBAccountRegion `
    -ResourceGroupName $resourceGroupName `
    -Name $accountName `
    -LocationObject $locationObject2

Write-Host "Update-AzCosmosDBAccountRegion returns before the region update is complete."
Write-Host "Check account in Azure portal or using Get-AzCosmosDBAccount for region status."
Write-Host "When region was added, press any key to continue."
$HOST.UI.RawUI.ReadKey("NoEcho,IncludeKeyDown") | OUT-NULL
$HOST.UI.RawUI.Flushinputbuffer()

# Remove West US region from the account
$locationObject3 = @()
$locationObject3 += New-AzCosmosDBLocationObject -LocationName "East US" -FailoverPriority 0 -IsZoneRedundant 0
$locationObject3 += New-AzCosmosDBLocationObject -LocationName "South Central US" -FailoverPriority 1 -IsZoneRedundant 0

Update-AzCosmosDBAccountRegion `
    -ResourceGroupName $resourceGroupName `
    -Name $accountName `
    -LocationObject $locationObject3

Write-Host "Update-AzCosmosDBAccountRegion returns before the region update is complete."
Write-Host "Check account in Azure portal or using Get-AzCosmosDBAccount for region status."

Aktivera flera skrivregioner för ett Azure Cosmos DB-konto

$resourceGroupName = "myResourceGroup"
$accountName = "mycosmosaccount"
$enableAutomaticFailover = $false
$enableMultiMaster = $true

# First disable service-managed failover - cannot have both service-managed
# failover and multi-region writes on an account
Update-AzCosmosDBAccount `
    -ResourceGroupName $resourceGroupName `
    -Name $accountName `
    -EnableAutomaticFailover:$enableAutomaticFailover

# Now enable multi-region writes
Update-AzCosmosDBAccount `
    -ResourceGroupName $resourceGroupName `
    -Name $accountName `
    -EnableMultipleWriteLocations:$enableMultiMaster

Ta bort ett Azure Cosmos DB-konto

Det här kommandot tar bort ett befintligt Azure Cosmos DB-konto.

$resourceGroupName = "myResourceGroup"
$accountName = "mycosmosaccount"

Remove-AzCosmosDBAccount `
    -ResourceGroupName $resourceGroupName `
    -Name $accountName `
    -PassThru:$true

Uppdatera taggar för ett Azure Cosmos DB-konto

Det här kommandot anger Azure-resurstaggar för ett Azure Cosmos DB-konto. Taggar kan anges både när kontot skapas med hjälp av New-AzCosmosDBAccount och vid kontouppdatering med hjälp av Update-AzCosmosDBAccount.

$resourceGroupName = "myResourceGroup"
$accountName = "mycosmosaccount"
$tags = @{dept = "Finance"; environment = "Production";}

Update-AzCosmosDBAccount `
    -ResourceGroupName $resourceGroupName `
    -Name $accountName `
    -Tag $tags

Lista kontonycklar

När du skapar ett Azure Cosmos DB-konto genererar tjänsten två primära åtkomstnycklar som kan användas för autentisering när Azure Cosmos DB-kontot används. Skrivskyddade nycklar för att autentisera skrivskyddade åtgärder genereras också. Genom att tillhandahålla två åtkomstnycklar kan du med Azure Cosmos DB återskapa och rotera en nyckel i taget utan avbrott i ditt Azure Cosmos DB-konto. Azure Cosmos DB-konton har två skrivskyddade nycklar (primära och sekundära) och två skrivskyddade nycklar (primära och sekundära).

$resourceGroupName = "myResourceGroup"
$accountName = "mycosmosaccount"

Get-AzCosmosDBAccountKey `
    -ResourceGroupName $resourceGroupName `
    -Name $accountName `
    -Type "Keys"

Lista Anslut ionssträngar

Följande kommando hämtar anslutningssträng för att ansluta appar till Azure Cosmos DB-kontot.

$resourceGroupName = "myResourceGroup"
$accountName = "mycosmosaccount"

Get-AzCosmosDBAccountKey `
    -ResourceGroupName $resourceGroupName `
    -Name $accountName `
    -Type "ConnectionStrings"

Återskapa kontonycklar

Åtkomstnycklar till ett Azure Cosmos DB-konto bör återskapas regelbundet för att hålla anslutningarna säkra. En primär och sekundär åtkomstnyckel tilldelas till kontot. Detta gör att klienter kan behålla åtkomsten medan en nyckel i taget återskapas. Det finns fyra typer av nycklar för ett Azure Cosmos DB-konto (primärt, sekundärt, PrimaryReadonly och SecondaryReadonly)

$resourceGroupName = "myResourceGroup" # Resource Group must already exist
$accountName = "mycosmosaccount" # Must be all lower case
$keyKind = "primary" # Other key kinds: secondary, primaryReadonly, secondaryReadonly

New-AzCosmosDBAccountKey `
    -ResourceGroupName $resourceGroupName `
    -Name $accountName `
    -KeyKind $keyKind

Aktivera tjänsthanterad redundans

Följande kommando anger ett Azure Cosmos DB-konto för att utföra en tjänsthanterad redundansväxling till den sekundära regionen om den primära regionen blir otillgänglig.

$resourceGroupName = "myResourceGroup"
$accountName = "mycosmosaccount"
$enableAutomaticFailover = $true
$enableMultiMaster = $false

# First disable multi-region writes - cannot have both automatic
# failover and multi-region writes on an account
Update-AzCosmosDBAccount `
    -ResourceGroupName $resourceGroupName `
    -Name $accountName `
    -EnableMultipleWriteLocations:$enableMultiMaster

# Now enable service-managed failover
Update-AzCosmosDBAccount `
    -ResourceGroupName $resourceGroupName `
    -Name $accountName `
    -EnableAutomaticFailover:$enableAutomaticFailover

Ändra redundansprioritet

För konton som konfigurerats med tjänsthanterad redundans kan du ändra i vilken ordning Azure Cosmos DB befordrar sekundära repliker till primära om den primära blir otillgänglig.

Anta i exemplet nedan att den aktuella redundansprioriteten är West US = 0, East US = 1, South Central US = 2. Kommandot ändrar detta till West US = 0, South Central US = 1, East US = 2.

Varning

Om du ändrar platsen för failoverPriority=0 utlöses en manuell redundansväxling för ett Azure Cosmos DB-konto. Andra prioritetsändringar utlöser ingen redundansväxling.

$resourceGroupName = "myResourceGroup"
$accountName = "mycosmosaccount"
$locations = @("West US", "South Central US", "East US") # Regions ordered by UPDATED failover priority

Update-AzCosmosDBAccountFailoverPriority `
    -ResourceGroupName $resourceGroupName `
    -Name $accountName `
    -FailoverPolicy $locations

Utlösa manuell redundans

För konton som har konfigurerats med manuell redundansväxling kan du redundansväxla och befordra valfri sekundär replik till primär genom att ändra till failoverPriority=0. Den här åtgärden kan användas för att initiera ett haveriberedskapstest för att testa planeringen för haveriberedskap.

I exemplet nedan förutsätter du att kontot har en aktuell redundansprioritet West US = 0 för och East US = 1 vänder regionerna.

Varning

Om du ändrar locationName för failoverPriority=0 utlöses en manuell redundansväxling för ett Azure Cosmos DB-konto. Andra prioritetsändringar utlöser ingen redundansväxling.

Kommentar

Om du utför en manuell redundansåtgärd medan en asynkron skalningsåtgärd för dataflöde pågår pausas skalningsåtgärden för dataflöde. Den återupptas automatiskt när redundansåtgärden är klar.

$resourceGroupName = "myResourceGroup"
$accountName = "mycosmosaccount"
$locations = @("East US", "West US") # Regions ordered by UPDATED failover priority

Update-AzCosmosDBAccountFailoverPriority `
    -ResourceGroupName $resourceGroupName `
    -Name $accountName `
    -FailoverPolicy $locations

Lista resurslås på ett Azure Cosmos DB-konto

Resurslås kan placeras på Azure Cosmos DB-resurser, inklusive databaser och samlingar. Exemplet nedan visar hur du listar alla Azure-resurslås på ett Azure Cosmos DB-konto.

$resourceGroupName = "myResourceGroup"
$resourceTypeAccount = "Microsoft.DocumentDB/databaseAccounts"
$accountName = "mycosmosaccount"

Get-AzResourceLock `
    -ResourceGroupName $resourceGroupName `
    -ResourceType $resourceTypeAccount `
    -ResourceName $accountName

Azure Cosmos DB Database

Följande avsnitt visar hur du hanterar Azure Cosmos DB-databasen, inklusive:

Skapa en Azure Cosmos DB-databas

$resourceGroupName = "myResourceGroup"
$accountName = "mycosmosaccount"
$databaseName = "myDatabase"

New-AzCosmosDBSqlDatabase `
    -ResourceGroupName $resourceGroupName `
    -AccountName $accountName `
    -Name $databaseName

Skapa en Azure Cosmos DB-databas med delat dataflöde

$resourceGroupName = "myResourceGroup"
$accountName = "mycosmosaccount"
$databaseName = "myDatabase"
$databaseRUs = 400

New-AzCosmosDBSqlDatabase `
    -ResourceGroupName $resourceGroupName `
    -AccountName $accountName `
    -Name $databaseName `
    -Throughput $databaseRUs

Hämta dataflödet för en Azure Cosmos DB-databas

$resourceGroupName = "myResourceGroup"
$accountName = "mycosmosaccount"
$databaseName = "myDatabase"

Get-AzCosmosDBSqlDatabaseThroughput `
    -ResourceGroupName $resourceGroupName `
    -AccountName $accountName `
    -Name $databaseName

Migrera databasdataflöde till autoskalning

$resourceGroupName = "myResourceGroup"
$accountName = "mycosmosaccount"
$databaseName = "myDatabase"

Invoke-AzCosmosDBSqlDatabaseThroughputMigration `
    -ResourceGroupName $resourceGroupName `
    -AccountName $accountName `
    -Name $databaseName `
    -ThroughputType Autoscale

Hämta alla Azure Cosmos DB-databaser i ett konto

$resourceGroupName = "myResourceGroup"
$accountName = "mycosmosaccount"

Get-AzCosmosDBSqlDatabase `
    -ResourceGroupName $resourceGroupName `
    -AccountName $accountName

Hämta en enda Azure Cosmos DB-databas

$resourceGroupName = "myResourceGroup"
$accountName = "mycosmosaccount"
$databaseName = "myDatabase"

Get-AzCosmosDBSqlDatabase `
    -ResourceGroupName $resourceGroupName `
    -AccountName $accountName `
    -Name $databaseName

Ta bort en Azure Cosmos DB-databas

$resourceGroupName = "myResourceGroup"
$accountName = "mycosmosaccount"
$databaseName = "myDatabase"

Remove-AzCosmosDBSqlDatabase `
    -ResourceGroupName $resourceGroupName `
    -AccountName $accountName `
    -Name $databaseName

Skapa ett resurslås på en Azure Cosmos DB-databas för att förhindra borttagning

$resourceGroupName = "myResourceGroup"
$resourceType = "Microsoft.DocumentDB/databaseAccounts/sqlDatabases"
$accountName = "mycosmosaccount"
$databaseName = "myDatabase"
$resourceName = "$accountName/$databaseName"
$lockName = "myResourceLock"
$lockLevel = "CanNotDelete"

New-AzResourceLock `
    -ResourceGroupName $resourceGroupName `
    -ResourceType $resourceType `
    -ResourceName $resourceName `
    -LockName $lockName `
    -LockLevel $lockLevel

Ta bort ett resurslås på en Azure Cosmos DB-databas

$resourceGroupName = "myResourceGroup"
$resourceType = "Microsoft.DocumentDB/databaseAccounts/sqlDatabases"
$accountName = "mycosmosaccount"
$databaseName = "myDatabase"
$resourceName = "$accountName/$databaseName"
$lockName = "myResourceLock"

Remove-AzResourceLock `
    -ResourceGroupName $resourceGroupName `
    -ResourceType $resourceType `
    -ResourceName $resourceName `
    -LockName $lockName

Azure Cosmos DB-container

Följande avsnitt visar hur du hanterar Azure Cosmos DB-containern, inklusive:

Skapa en Azure Cosmos DB-container

# Create an Azure Cosmos DB container with default indexes and throughput at 400 RU
$resourceGroupName = "myResourceGroup"
$accountName = "mycosmosaccount"
$databaseName = "myDatabase"
$containerName = "myContainer"
$partitionKeyPath = "/myPartitionKey"
$throughput = 400 #minimum = 400

New-AzCosmosDBSqlContainer `
    -ResourceGroupName $resourceGroupName `
    -AccountName $accountName `
    -DatabaseName $databaseName `
    -Name $containerName `
    -PartitionKeyKind Hash `
    -PartitionKeyPath $partitionKeyPath `
    -Throughput $throughput

Skapa en Azure Cosmos DB-container med autoskalning

# Create an Azure Cosmos DB container with default indexes and autoscale throughput at 4000 RU
$resourceGroupName = "myResourceGroup"
$accountName = "mycosmosaccount"
$databaseName = "myDatabase"
$containerName = "myContainer"
$partitionKeyPath = "/myPartitionKey"
$autoscaleMaxThroughput = 4000 #minimum = 4000

New-AzCosmosDBSqlContainer `
    -ResourceGroupName $resourceGroupName `
    -AccountName $accountName `
    -DatabaseName $databaseName `
    -Name $containerName `
    -PartitionKeyKind Hash `
    -PartitionKeyPath $partitionKeyPath `
    -AutoscaleMaxThroughput $autoscaleMaxThroughput

Skapa en Azure Cosmos DB-container med en stor partitionsnyckelstorlek

# Create an Azure Cosmos DB container with a large partition key value (version = 2)
$resourceGroupName = "myResourceGroup"
$accountName = "mycosmosaccount"
$databaseName = "myDatabase"
$containerName = "myContainer"
$partitionKeyPath = "/myPartitionKey"

New-AzCosmosDBSqlContainer `
    -ResourceGroupName $resourceGroupName `
    -AccountName $accountName `
    -DatabaseName $databaseName `
    -Name $containerName `
    -PartitionKeyKind Hash `
    -PartitionKeyPath $partitionKeyPath `
    -PartitionKeyVersion 2

Hämta dataflödet för en Azure Cosmos DB-container

$resourceGroupName = "myResourceGroup"
$accountName = "mycosmosaccount"
$databaseName = "myDatabase"
$containerName = "myContainer"

Get-AzCosmosDBSqlContainerThroughput `
    -ResourceGroupName $resourceGroupName `
    -AccountName $accountName `
    -DatabaseName $databaseName `
    -Name $containerName

Migrera containerdataflöde till autoskalning

$resourceGroupName = "myResourceGroup"
$accountName = "mycosmosaccount"
$databaseName = "myDatabase"
$containerName = "myContainer"

Invoke-AzCosmosDBSqlContainerThroughputMigration `
    -ResourceGroupName $resourceGroupName `
    -AccountName $accountName `
    -DatabaseName $databaseName `
    -Name $containerName `
    -ThroughputType Autoscale

Skapa en Azure Cosmos DB-container med anpassad indexprincip

# Create a container with a custom indexing policy
$resourceGroupName = "myResourceGroup"
$accountName = "mycosmosaccount"
$databaseName = "myDatabase"
$containerName = "myContainer"
$partitionKeyPath = "/myPartitionKey"
$indexPathIncluded = "/*"
$indexPathExcluded = "/myExcludedPath/*"

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

$indexingPolicy = New-AzCosmosDBSqlIndexingPolicy `
    -IncludedPath $includedPath `
    -ExcludedPath $indexPathExcluded `
    -IndexingMode Consistent `
    -Automatic $true

New-AzCosmosDBSqlContainer `
    -ResourceGroupName $resourceGroupName `
    -AccountName $accountName `
    -DatabaseName $databaseName `
    -Name $containerName `
    -PartitionKeyKind Hash `
    -PartitionKeyPath $partitionKeyPath `
    -IndexingPolicy $indexingPolicy

Skapa en Azure Cosmos DB-container med indexering inaktiverat

# Create an Azure Cosmos DB container with no indexing
$resourceGroupName = "myResourceGroup"
$accountName = "mycosmosaccount"
$databaseName = "myDatabase"
$containerName = "myContainer"
$partitionKeyPath = "/myPartitionKey"

$indexingPolicy = New-AzCosmosDBSqlIndexingPolicy `
    -IndexingMode None

New-AzCosmosDBSqlContainer `
    -ResourceGroupName $resourceGroupName `
    -AccountName $accountName `
    -DatabaseName $databaseName `
    -Name $containerName `
    -PartitionKeyKind Hash `
    -PartitionKeyPath $partitionKeyPath `
    -IndexingPolicy $indexingPolicy

Skapa en Azure Cosmos DB-container med unik nyckelprincip och TTL

# Create a container with a unique key policy and TTL of one day
$resourceGroupName = "myResourceGroup"
$accountName = "mycosmosaccount"
$databaseName = "myDatabase"
$containerName = "myContainer"
$partitionKeyPath = "/myPartitionKey"
$uniqueKeyPath = "/myUniqueKeyPath"
$ttlInSeconds = 86400 # Set this to -1 (or don't use it at all) to never expire

$uniqueKey = New-AzCosmosDBSqlUniqueKey `
    -Path $uniqueKeyPath

$uniqueKeyPolicy = New-AzCosmosDBSqlUniqueKeyPolicy `
    -UniqueKey $uniqueKey

New-AzCosmosDBSqlContainer `
    -ResourceGroupName $resourceGroupName `
    -AccountName $accountName `
    -DatabaseName $databaseName `
    -Name $containerName `
    -PartitionKeyKind Hash `
    -PartitionKeyPath $partitionKeyPath `
    -UniqueKeyPolicy $uniqueKeyPolicy `
    -TtlInSeconds $ttlInSeconds

Skapa en Azure Cosmos DB-container med konfliktlösning

Om du vill skriva alla konflikter till ConflictsFeed och hantera separat skickar du -Type "Custom" -Path "".

# Create container with last-writer-wins conflict resolution policy
$resourceGroupName = "myResourceGroup"
$accountName = "mycosmosaccount"
$databaseName = "myDatabase"
$containerName = "myContainer"
$partitionKeyPath = "/myPartitionKey"
$conflictResolutionPath = "/myResolutionPath"

$conflictResolutionPolicy = New-AzCosmosDBSqlConflictResolutionPolicy `
    -Type LastWriterWins `
    -Path $conflictResolutionPath

New-AzCosmosDBSqlContainer `
    -ResourceGroupName $resourceGroupName `
    -AccountName $accountName `
    -DatabaseName $databaseName `
    -Name $containerName `
    -PartitionKeyKind Hash `
    -PartitionKeyPath $partitionKeyPath `
    -ConflictResolutionPolicy $conflictResolutionPolicy

Om du vill skapa en konfliktlösningsprincip för att använda en lagrad procedur anropar New-AzCosmosDBSqlConflictResolutionPolicy och skickar du parametrar -Type och -ConflictResolutionProcedure.

# Create container with custom conflict resolution policy using a stored procedure
$resourceGroupName = "myResourceGroup"
$accountName = "mycosmosaccount"
$databaseName = "myDatabase"
$containerName = "myContainer"
$partitionKeyPath = "/myPartitionKey"
$conflictResolutionSprocName = "mysproc"

$conflictResolutionSproc = "/dbs/$databaseName/colls/$containerName/sprocs/$conflictResolutionSprocName"

$conflictResolutionPolicy = New-AzCosmosDBSqlConflictResolutionPolicy `
    -Type Custom `
    -ConflictResolutionProcedure $conflictResolutionSproc

New-AzCosmosDBSqlContainer `
    -ResourceGroupName $resourceGroupName `
    -AccountName $accountName `
    -DatabaseName $databaseName `
    -Name $containerName `
    -PartitionKeyKind Hash `
    -PartitionKeyPath $partitionKeyPath `
    -ConflictResolutionPolicy $conflictResolutionPolicy

Visa en lista över alla Azure Cosmos DB-containrar i en databas

# List all Azure Cosmos DB containers in a database
$resourceGroupName = "myResourceGroup"
$accountName = "mycosmosaccount"
$databaseName = "myDatabase"

Get-AzCosmosDBSqlContainer `
    -ResourceGroupName $resourceGroupName `
    -AccountName $accountName `
    -DatabaseName $databaseName

Hämta en enda Azure Cosmos DB-container i en databas

# Get a single Azure Cosmos DB container in a database
$resourceGroupName = "myResourceGroup"
$accountName = "mycosmosaccount"
$databaseName = "myDatabase"
$containerName = "myContainer"

Get-AzCosmosDBSqlContainer `
    -ResourceGroupName $resourceGroupName `
    -AccountName $accountName `
    -DatabaseName $databaseName `
    -Name $containerName

Ta bort en Azure Cosmos DB-container

# Delete an Azure Cosmos DB container
$resourceGroupName = "myResourceGroup"
$accountName = "mycosmosaccount"
$databaseName = "myDatabase"
$containerName = "myContainer"

Remove-AzCosmosDBSqlContainer `
    -ResourceGroupName $resourceGroupName `
    -AccountName $accountName `
    -DatabaseName $databaseName `
    -Name $containerName

Skapa ett resurslås på en Azure Cosmos DB-container för att förhindra borttagning

$resourceGroupName = "myResourceGroup"
$resourceType = "Microsoft.DocumentDB/databaseAccounts/sqlDatabases/containers"
$accountName = "mycosmosaccount"
$databaseName = "myDatabase"
$containerName = "myContainer"
$resourceName = "$accountName/$databaseName/$containerName"
$lockName = "myResourceLock"
$lockLevel = "CanNotDelete"

New-AzResourceLock `
    -ResourceGroupName $resourceGroupName `
    -ResourceType $resourceType `
    -ResourceName $resourceName `
    -LockName $lockName `
    -LockLevel $lockLevel

Ta bort ett resurslås på en Azure Cosmos DB-container

$resourceGroupName = "myResourceGroup"
$resourceType = "Microsoft.DocumentDB/databaseAccounts/sqlDatabases/containers"
$accountName = "mycosmosaccount"
$databaseName = "myDatabase"
$containerName = "myContainer"
$resourceName = "$accountName/$databaseName/$containerName"
$lockName = "myResourceLock"

Remove-AzResourceLock `
    -ResourceGroupName $resourceGroupName `
    -ResourceType $resourceType `
    -ResourceName $resourceName `
    -LockName $lockName

Nästa steg