Share via


Een database en container maken met automatische schaalaanpassing voor Azure Cosmos DB - API voor NoSQL

VAN TOEPASSING OP: NoSQL

Notitie

U wordt aangeraden de Azure Az PowerShell-module te gebruiken om te communiceren met Azure. Zie Azure PowerShell installeren om aan de slag te gaan. Raadpleeg Azure PowerShell migreren van AzureRM naar Az om te leren hoe u naar de Azure PowerShell-module migreert.

Voor dit voorbeeld is Azure PowerShell Az 5.4.0 of hoger vereist. Voer Get-Module -ListAvailable Az uit om te zien welke versies zijn geïnstalleerd. Als u PowerShell moet installeren, raadpleegt u De Azure PowerShell-module installeren.

Voer Connect-AzAccount uit om u aan te melden bij Azure.

Voorbeeldscript

Met dit script maakt u een Azure Cosmos DB-account voor API voor NoSQL in twee regio's met consistentie op sessieniveau, een database en een container met doorvoer voor automatische schaalaanpassing en standaardindexbeleid.

# Reference: Az.CosmosDB | https://docs.microsoft.com/powershell/module/az.cosmosdb
# --------------------------------------------------
# Purpose
# Create Cosmos SQL API account, database, and container 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 = "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"
$databaseName = "myDatabase"
$containerName = "myContainer"
$autoscaleMaxThroughput = 4000 #minimum = 4000
$partitionKeyPath = "/myPartitionKey"

# --------------------------------------------------
Write-Host "Creating account $accountName"
$account = New-AzCosmosDBAccount -ResourceGroupName $resourceGroupName `
    -LocationObject $locations -Name $accountName -ApiKind $apiKind `
    -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 `
    -PartitionKeyKind Hash -PartitionKeyPath $partitionKeyPath `
    -AutoscaleMaxThroughput $autoscaleMaxThroughput

Opschonen van implementatie

Na het uitvoeren van het voorbeeldscript kan de volgende opdracht worden gebruikt om de resourcegroep en alle resources die er aan zijn gekoppeld te verwijderen.

Remove-AzResourceGroup -ResourceGroupName "myResourceGroup"

Uitleg van het script

In dit script worden de volgende opdrachten gebruikt. Elke opdracht in de tabel is een koppeling naar specifieke documentatie over de opdracht.

Opdracht Opmerkingen
Azure Cosmos DB
New-AzCosmosDBAccount Hiermee maakt u een Azure Cosmos DB-account.
New-AzCosmosDBSqlDatabase Hiermee maakt u een Azure Cosmos DB-SQL Database.
New-AzCosmosDBSqlContainer Hiermee maakt u een nieuwe Azure Cosmos DB SQL-container.
Azure-resourcegroepen
Remove-AzResourceGroup Hiermee verwijdert u een resourcegroep met inbegrip van alle geneste resources.

Volgende stappen

Zie Documentatie over Azure PowerShell voor meer informatie over Azure PowerShell.