建立 Azure Cosmos DB 資料庫和圖形 - API for Gremlin
適用於: Gremlin
注意
建議您使用 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。
範例指令碼
# Reference: Az.CosmosDB | https://docs.microsoft.com/powershell/module/az.cosmosdb
# --------------------------------------------------
# Purpose
# Create Cosmos Gremlin API account, database, and graph
# with dedicated throughput and conflict resolution policy
# with last writer wins and custom resolver path
# --------------------------------------------------
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 = "Gremlin"
# --------------------------------------------------
# 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"
$graphName = "myGraph"
$graphRUs = 400
$partitionKeys = @("/myPartitionKey")
# --------------------------------------------------
$conflictResolutionPath = "/_ts"
# --------------------------------------------------
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-AzCosmosDBGremlinDatabase -ParentObject $account `
-Name $databaseName
# Prepare conflict resolution policy object for graph
$conflictResolutionPolicy = New-AzCosmosDBGremlinConflictResolutionPolicy `
-Type LastWriterWins -Path $conflictResolutionPath
Write-Host "Creating graph $graphName"
$graph = New-AzCosmosDBGremlinGraph -ParentObject $database `
-Name $graphName -Throughput $graphRUs `
-PartitionKeyKind Hash -PartitionKeyPath $partitionKeys `
-ConflictResolutionPolicy $conflictResolutionPolicy
清除部署
在執行過指令碼範例之後,您可以使用下列命令來移除資源群組和所有與其相關聯的資源。
Remove-AzResourceGroup -ResourceGroupName "myResourceGroup"
指令碼說明
此指令碼會使用下列命令。 下表中的每個命令都會連結至命令特定的文件。
Command | 注意 |
---|---|
Azure Cosmos DB | |
New-AzCosmosDBAccount | 建立 Azure Cosmos DB 帳戶。 |
New-AzCosmosDBGremlinDatabase | 建立 API for Gremlin 資料庫。 |
New-AzCosmosDBGremlinConflictResolutionPolicy | 建立 API for Gremlin 寫入衝突解決原則。 |
New-AzCosmosDBGremlinGraph | 建立 API for Gremlin 圖形。 |
Azure 資源群組 | |
Remove-AzResourceGroup | 刪除資源群組,包括所有的巢狀資源。 |
下一步
如需有關 Azure PowerShell 的詳細資訊,請參閱 Azure PowerShell 文件。