Membuat database dan kontainer untuk API Azure Cosmos DB - SQL
BERLAKU UNTUK:
SQL API
Catatan
Artikel ini menggunakan modul Azure Az PowerShell, yang merupakan modul PowerShell yang direkomendasikan untuk berinteraksi dengan Azure. Untuk mulai menggunakan modul Az PowerShell, lihat Menginstal Azure PowerShell. Untuk mempelajari cara bermigrasi ke modul Az PowerShell, lihat Memigrasikan Azure PowerShell dari AzureRM ke Az.
Sampel ini memerlukan Azure PowerShell Az 5.4.0 atau yang lebih baru. Jalankan Get-Module -ListAvailable Az untuk melihat versi mana yang dipasang.
Jika Anda perlu memasang, lihat Pasang modul Azure PowerShell.
Jalankan Connect-AzAccount untuk masuk ke Azure.
Skrip sampel
Skrip ini membuat akun Cosmos untuk API SQL (Core) di dua wilayah dengan konsistensi tingkat sesi, database, dan kontainer dengan kunci partisi, kebijakan pengindeksan kustom, kebijakan kunci unik, TTL, throughput khusus, dan penulis terakhir memenangkan kebijakan resolusi konflik dengan jalur resolusi konflik kustom yang akan digunakan ketika 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
Membersihkan penyebaran
Setelah sampel skrip dijalankan, perintah berikut dapat digunakan untuk menghapus grup sumber daya dan semua sumber daya yang terkait.
Remove-AzResourceGroup -ResourceGroupName "myResourceGroup"
Penjelasan skrip
Skrip ini menggunakan perintah berikut. Setiap perintah dalam tabel menautkan ke dokumentasi perintah tertentu.
| Perintah | Catatan |
|---|---|
| Azure Cosmos DB | |
| New-AzCosmosDBAccount | Membuat Akun Cosmos DB. |
| New-AzCosmosDBSqlDatabase | Membuat Cosmos DB SQL Database. |
| Baru-AzCosmosDBSqlUniqueKey | Membuat objek PSSqlUniqueKey yang digunakan sebagai parameter untuk Baru-AzCosmosDBSqlUniqueKeyPolicy. |
| Baru-AzCosmosDBSqlUniqueKeyPolicy | Membuat objek PSSqlUniqueKey yang digunakan sebagai parameter untuk Baru-AzCosmosDBSqlContainer. |
| Baru-AzCosmosDBSqlCompositePath | Membuat objek PSCompositePath yang digunakan sebagai parameter untuk New-AzCosmosDBSqlIndexingPolicy. |
| Baru-AzCosmosDBSqlIncludedPathIndex | Membuat objek PSIndexes yang digunakan sebagai parameter untuk Baru-AzCosmosDBSqlIncludedPath. |
| Baru-AzCosmosDBSqlIncludedPath | Membuat objek PSIncludedPath yang digunakan sebagai parameter untuk Baru-AzCosmosDBSqlIndexingPolicy. |
| Baru-AzCosmosDBSqlIndexingPolicy | Membuat objek PSSqlIndexingPolicy yang digunakan sebagai parameter untuk Baru-AzCosmosDBSqlContainer. |
| Baru-AzCosmosDBSqlConflictResolutionPolicy | Membuat objek PSSqlIndexingPolicy yang digunakan sebagai parameter untuk Baru-AzCosmosDBSqlContainer. |
| Baru-AzCosmosDBSqlContainer | Membuat SQL Database Cosmos DB. |
| Grup Sumber Daya Azure | |
| Remove-AzResourceGroup | Menghapus grup sumber daya termasuk semua sumber daya berlapis. |
Langkah berikutnya
Untuk informasi selengkapnya tentang Azure PowerShell, lihat dokumentasi Azure PowerShell.