共用方式為


建立 Azure Cosmos DB 資料庫和容器 - API for NoSQL

適用於:NoSQL

注意

建議您使用 Azure Az PowerShell 模組來與 Azure 互動。 請參閱安裝 Azure PowerShell 以開始使用。 若要了解如何移轉至 Az PowerShell 模組,請參閱將 Azure PowerShell 從 AzureRM 移轉至 Az

此範例需要 Azure PowerShell Az 5.4.0 或更新版本。 執行 Get-Module -ListAvailable Az 可查看已安裝的版本。 如果您需要安裝,請參閱安裝 Azure PowerShell 模組

執行 Connect-AzAccount 來登入 Azure。

範例指令碼

此指令碼會在工作階段層級一致的兩個區域中建立 API for NoSQL 的 Azure Cosmos DB 帳戶、建立資料庫,以及建立容器,該容器具有分割區索引碼、自訂索引編製原則、唯一金鑰原則、TTL、專用輸送量,以及具有自訂衝突解決路徑的「最後寫入者為準」衝突解決原則,以便在 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

清除部署

在執行過指令碼範例之後,您可以使用下列命令來移除資源群組和所有與其相關聯的資源。

Remove-AzResourceGroup -ResourceGroupName "myResourceGroup"

指令碼說明

此指令碼會使用下列命令。 下表中的每個命令都會連結至命令特定的文件。

Command 注意
Azure Cosmos DB
New-AzCosmosDBAccount 建立 Azure Cosmos DB 帳戶。
New-AzCosmosDBSqlDatabase 建立 Azure Cosmos DB SQL 資料庫。
New-AzCosmosDBSqlUniqueKey 建立 PSSqlUniqueKey 物件,以作為 New-AzCosmosDBSqlUniqueKeyPolicy 的參數使用。
New-AzCosmosDBSqlUniqueKeyPolicy 建立 PSSqlUniqueKeyPolicy 物件,以作為 New-AzCosmosDBSqlContainer 的參數使用。
New-AzCosmosDBSqlCompositePath 建立 PSCompositePath 物件,以作為 New-AzCosmosDBSqlIndexingPolicy 的參數使用。
New-AzCosmosDBSqlIncludedPathIndex 建立 PSIndexes 物件,以作為 New-AzCosmosDBSqlIncludedPath 的參數使用。
New-AzCosmosDBSqlIncludedPath 建立 PSIncludedPath 物件,以作為 New-AzCosmosDBSqlIndexingPolicy 的參數使用。
New-AzCosmosDBSqlIndexingPolicy 建立 PSSqlIndexingPolicy 物件,以作為 SNew-AzCosmosDBSqlContainer 的參數使用。
New-AzCosmosDBSqlConflictResolutionPolicy 建立 PSSqlConflictResolutionPolicy 物件,以作為 New-AzCosmosDBSqlContainer 的參數使用。
New-AzCosmosDBSqlContainer 建立新的 Azure Cosmos DB SQL 容器。
Azure 資源群組
Remove-AzResourceGroup 刪除資源群組,包括所有的巢狀資源。

下一步

如需有關 Azure PowerShell 的詳細資訊,請參閱 Azure PowerShell 文件