Aracılığıyla paylaş


Azure Cosmos DB için veritabanı ve kapsayıcı oluşturma - NoSQL için API

UYGULANANLAR: NoSQL

Not

Azure ile etkileşim kurmak için Azure Az PowerShell modülünü kullanmanızı öneririz. Başlamak için bkz . Azure PowerShell'i yükleme. Az PowerShell modülüne nasıl geçeceğinizi öğrenmek için bkz. Azure PowerShell’i AzureRM’den Az’ye geçirme.

Bu örnek Için Azure PowerShell Az 5.4.0 veya üzeri gerekir. Hangi sürümlerin yüklü olduğunu görmek için komutunu çalıştırın Get-Module -ListAvailable Az . Yüklemeniz gerekiyorsa bkz . Azure PowerShell modülünü yükleme.

Azure'da oturum açmak için Connect-AzAccount komutunu çalıştırın.

Örnek betik

Bu betik, oturum düzeyi tutarlılığı, veritabanı ve bölüm anahtarı, özel dizin oluşturma ilkesi, benzersiz anahtar ilkesi, TTL, ayrılmış aktarım hızı ve son yazıcı tarafından kullanıldığında kullanılacak multipleWriteLocations=trueözel bir çakışma çözümleme yolu ile çakışma çözümleme ilkesi kazanır iki bölgede NoSQL için API için bir Azure Cosmos DB hesabı oluşturur.

# 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

Dağıtımı temizleme

Betik örneği çalıştırıldıktan sonra, kaynak grubunu ve onunla ilişkili tüm kaynakları kaldırmak için aşağıdaki komut kullanılabilir.

Remove-AzResourceGroup -ResourceGroupName "myResourceGroup"

Betik açıklaması

Bu betik aşağıdaki komutları kullanır. Tablodaki her komut, komuta özgü belgelere yönlendirir.

Command Notlar
Azure Cosmos DB
New-AzCosmosDBAccount Bir Azure Cosmos DB Hesabı oluşturur.
New-AzCosmosDBSqlDatabase Azure Cosmos DB SQL Veritabanı oluşturur.
New-AzCosmosDBSqlUniqueKey New-AzCosmosDBSqlUniqueKeyPolicy parametresi olarak kullanılan bir PSSqlUniqueKey nesnesi oluşturur.
New-AzCosmosDBSqlUniqueKeyPolicy New-AzCosmosDBSqlContainer parametresi olarak kullanılan bir PSSqlUniqueKeyPolicy nesnesi oluşturur.
New-AzCosmosDBSqlCompositePath New-AzCosmosDBSqlIndexingPolicy parametresi olarak kullanılan bir PSCompositePath nesnesi oluşturur.
New-AzCosmosDBSqlIncludedPathIndex New-AzCosmosDBSqlIncludedPath parametresi olarak kullanılan bir PSIndexes nesnesi oluşturur.
New-AzCosmosDBSqlIncludedPath New-AzCosmosDBSqlIndexingPolicy parametresi olarak kullanılan bir PSIncludedPath nesnesi oluşturur.
New-AzCosmosDBSqlIndexingPolicy New-AzCosmosDBSqlContainer için parametre olarak kullanılan bir PSSqlIndexingPolicy nesnesi oluşturur.
New-AzCosmosDBSqlConflictResolutionPolicy New-AzCosmosDBSqlContainer parametresi olarak kullanılan bir PSSqlConflictResolutionPolicy nesnesi oluşturur.
New-AzCosmosDBSqlContainer Yeni bir Azure Cosmos DB SQL Kapsayıcısı oluşturur.
Azure Kaynak Grupları
Remove-AzResourceGroup Bir kaynak grubunu tüm iç içe geçmiş kaynaklar dahil siler.

Sonraki adımlar

Azure PowerShell hakkında daha fazla bilgi için bkz. Azure PowerShell belgeleri.