你当前正在访问 Microsoft Azure Global Edition 技术文档网站。 如果需要访问由世纪互联运营的 Microsoft Azure 中国技术文档网站,请访问 https://docs.azure.cn。
使用 PowerShell 管理 Azure Cosmos DB for NoSQL 资源
适用范围: NoSQL
以下指南介绍了如何使用 PowerShell 通过脚本来自动管理 Azure Cosmos DB for NoSQL 资源,其中包括 Azure Cosmos DB 帐户、数据库、容器和吞吐量。 对于适用于其他 API 的 PowerShell cmdlet,请参阅适用于 Cassandra 的 PowerShell 示例、适用于 API for MongoDB 的 PowerShell 示例、适用于 Gremlin 的 PowerShell 示例、适用于 Table 的 PowerShell 示例
注意
本文中的示例使用 Az.CosmosDB 管理 cmdlet。 有关最新更改,请参阅 Az.CosmosDB API 参考页。
若要跨平台管理 Azure Cosmos DB,可在使用 Az
和 Az.CosmosDB
cmdlet 时结合使用跨平台 PowerShell 以及 Azure CLI、REST API 或 Azure 门户。
注意
建议使用 Azure Az PowerShell 模块与 Azure 交互。 若要开始,请参阅安装 Azure PowerShell。 若要了解如何迁移到 Az PowerShell 模块,请参阅 将 Azure PowerShell 从 AzureRM 迁移到 Az。
入门
请按照如何安装和配置 Azure PowerShell 中的说明,安装 PowerShell 并在其中登录 Azure 帐户。
重要
无法重命名 Azure Cosmos DB 资源,因为这违反了 Azure 资源管理器与资源 URI 的工作方式。
Azure Cosmos DB 帐户
以下部分演示了如何管理 Azure Cosmos DB 帐户,具体包括:
- 创建 Azure Cosmos DB 帐户
- 更新 Azure Cosmos DB 帐户
- 列出订阅中的所有 Azure Cosmos DB 帐户
- 获取 Azure Cosmos DB 帐户
- 删除 Azure Cosmos DB 帐户
- 更新 Azure Cosmos DB 帐户的标记
- 修改 Azure Cosmos DB 帐户的故障转移优先级
- 触发 Azure Cosmos DB 帐户的手动故障转移
- 列出 Azure Cosmos DB 帐户上的资源锁
创建 Azure Cosmos DB 帐户
此命令创建一个 Azure Cosmos DB 数据库帐户,该帐户使用多区域、由服务管理的故障转移和有限过期一致性策略。
$resourceGroupName = "myResourceGroup"
$accountName = "mycosmosaccount"
$apiKind = "Sql"
$consistencyLevel = "BoundedStaleness"
$maxStalenessInterval = 300
$maxStalenessPrefix = 100000
$locations = @()
$locations += New-AzCosmosDBLocationObject -LocationName "East US" -FailoverPriority 0 -IsZoneRedundant 0
$locations += New-AzCosmosDBLocationObject -LocationName "West US" -FailoverPriority 1 -IsZoneRedundant 0
New-AzCosmosDBAccount `
-ResourceGroupName $resourceGroupName `
-LocationObject $locations `
-Name $accountName `
-ApiKind $apiKind `
-EnableAutomaticFailover:$true `
-DefaultConsistencyLevel $consistencyLevel `
-MaxStalenessIntervalInSeconds $maxStalenessInterval `
-MaxStalenessPrefix $maxStalenessPrefix
$resourceGroupName
:要在其中部署 Azure Cosmos DB 帐户的 Azure 资源组。 它必须已存在。$locations
:数据库帐户的区域,带有FailoverPriority 0
的区域为写入区域。$accountName
:Azure Cosmos DB 帐户的名称。 必须独一无二且必须为小写,仅包含字母数字和“-”字符,长度为 3 到 31 个字符。$apiKind
:要创建的 Azure Cosmos DB 帐户类型。 有关详细信息,请参阅 Azure Cosmos DB 中的 API。$consistencyPolicy
、$maxStalenessInterval
和$maxStalenessPrefix
:Azure Cosmos DB 帐户的默认一致性级别和设置。 有关详细信息,请参阅 Azure Cosmos DB 中的一致性级别。
可以为 Azure Cosmos DB 帐户配置 IP 防火墙、虚拟网络服务终结点和专用终结点。 有关如何为 Azure Cosmos DB 配置 IP 防火墙的信息,请参阅配置 IP 防火墙。 若要了解如何为 Azure Cosmos DB 启用服务终结点,请参阅配置从虚拟网络进行访问的权限。 若要了解如何为 Azure Cosmos DB 启用专用终结点,请参阅配置从专用终结点进行访问的权限。
列出资源组中的所有 Azure Cosmos DB 帐户
该命令列出资源组中的所有 Azure Cosmos DB 帐户。
$resourceGroupName = "myResourceGroup"
Get-AzCosmosDBAccount -ResourceGroupName $resourceGroupName
获取 Azure Cosmos DB 帐户的属性
使用此命令可获取现有 Azure Cosmos DB 帐户的属性。
$resourceGroupName = "myResourceGroup"
$accountName = "mycosmosaccount"
Get-AzCosmosDBAccount -ResourceGroupName $resourceGroupName -Name $accountName
更新 Azure Cosmos DB 帐户
此命令可更新 Azure Cosmos DB 数据库帐户属性。 可更新的属性包括:
- 添加或删除区域
- 更改默认的一致性策略
- 更改 IP 范围筛选器
- 更改虚拟网络配置
- 启用多区域写入
注意
不能同时添加或删除区域 (locations
) 并更改 Azure Cosmos DB 帐户的其他属性。 修改区域的操作必须作为单独的操作与任何其他对帐户的更改操作分开执行。
提示
添加新区域时,必须将所有数据完全复制并提交到新区域,然后才能将该区域标记为可用。 此操作所需的时间取决于帐户中存储的数据量。 如果正在进行异步吞吐量缩放操作,则吞吐量纵向扩展操作会暂停,并且会在添加/删除区域操作完成后自动继续。
# Create account with two regions
$resourceGroupName = "myResourceGroup"
$accountName = "mycosmosaccount"
$apiKind = "Sql"
$consistencyLevel = "Session"
$enableAutomaticFailover = $true
$locations = @()
$locations += New-AzCosmosDBLocationObject -LocationName "East US" -FailoverPriority 0 -IsZoneRedundant 0
$locations += New-AzCosmosDBLocationObject -LocationName "West US" -FailoverPriority 1 -IsZoneRedundant 0
# Create the Azure Cosmos DB account
New-AzCosmosDBAccount `
-ResourceGroupName $resourceGroupName `
-LocationObject $locations `
-Name $accountName `
-ApiKind $apiKind `
-EnableAutomaticFailover:$enableAutomaticFailover `
-DefaultConsistencyLevel $consistencyLevel
# Add a region to the account
$locationObject2 = @()
$locationObject2 += New-AzCosmosDBLocationObject -LocationName "East US" -FailoverPriority 0 -IsZoneRedundant 0
$locationObject2 += New-AzCosmosDBLocationObject -LocationName "West US" -FailoverPriority 1 -IsZoneRedundant 0
$locationObject2 += New-AzCosmosDBLocationObject -LocationName "South Central US" -FailoverPriority 2 -IsZoneRedundant 0
Update-AzCosmosDBAccountRegion `
-ResourceGroupName $resourceGroupName `
-Name $accountName `
-LocationObject $locationObject2
Write-Host "Update-AzCosmosDBAccountRegion returns before the region update is complete."
Write-Host "Check account in Azure portal or using Get-AzCosmosDBAccount for region status."
Write-Host "When region was added, press any key to continue."
$HOST.UI.RawUI.ReadKey("NoEcho,IncludeKeyDown") | OUT-NULL
$HOST.UI.RawUI.Flushinputbuffer()
# Remove West US region from the account
$locationObject3 = @()
$locationObject3 += New-AzCosmosDBLocationObject -LocationName "East US" -FailoverPriority 0 -IsZoneRedundant 0
$locationObject3 += New-AzCosmosDBLocationObject -LocationName "South Central US" -FailoverPriority 1 -IsZoneRedundant 0
Update-AzCosmosDBAccountRegion `
-ResourceGroupName $resourceGroupName `
-Name $accountName `
-LocationObject $locationObject3
Write-Host "Update-AzCosmosDBAccountRegion returns before the region update is complete."
Write-Host "Check account in Azure portal or using Get-AzCosmosDBAccount for region status."
为 Azure Cosmos DB 帐户启用多个写入区域
$resourceGroupName = "myResourceGroup"
$accountName = "mycosmosaccount"
$enableAutomaticFailover = $false
$enableMultiMaster = $true
# First disable service-managed failover - cannot have both service-managed
# failover and multi-region writes on an account
Update-AzCosmosDBAccount `
-ResourceGroupName $resourceGroupName `
-Name $accountName `
-EnableAutomaticFailover:$enableAutomaticFailover
# Now enable multi-region writes
Update-AzCosmosDBAccount `
-ResourceGroupName $resourceGroupName `
-Name $accountName `
-EnableMultipleWriteLocations:$enableMultiMaster
删除 Azure Cosmos DB 帐户
此命令可删除现有 Azure Cosmos DB 帐户。
$resourceGroupName = "myResourceGroup"
$accountName = "mycosmosaccount"
Remove-AzCosmosDBAccount `
-ResourceGroupName $resourceGroupName `
-Name $accountName `
-PassThru:$true
更新 Azure Cosmos DB 帐户的标记
此命令设置 Azure Cosmos DB 帐户的 Azure 资源标记。 标记可以在创建帐户时使用 New-AzCosmosDBAccount
来设置,也可以在更新帐户时使用 Update-AzCosmosDBAccount
来设置。
$resourceGroupName = "myResourceGroup"
$accountName = "mycosmosaccount"
$tags = @{dept = "Finance"; environment = "Production";}
Update-AzCosmosDBAccount `
-ResourceGroupName $resourceGroupName `
-Name $accountName `
-Tag $tags
启用由服务管理的故障转移
以下命令会将 Azure Cosmos DB 帐户设置为在主要区域变得不可用时执行到其次要区域的服务托管故障转移。
$resourceGroupName = "myResourceGroup"
$accountName = "mycosmosaccount"
$enableAutomaticFailover = $true
$enableMultiMaster = $false
# First disable multi-region writes - cannot have both automatic
# failover and multi-region writes on an account
Update-AzCosmosDBAccount `
-ResourceGroupName $resourceGroupName `
-Name $accountName `
-EnableMultipleWriteLocations:$enableMultiMaster
# Now enable service-managed failover
Update-AzCosmosDBAccount `
-ResourceGroupName $resourceGroupName `
-Name $accountName `
-EnableAutomaticFailover:$enableAutomaticFailover
修改故障转移优先级
对于配置了服务托管故障转移的帐户,可以更改 Azure Cosmos DB 在主要副本不可用时将次要副本提升为主要副本的顺序。
对于以下示例,假定当前的故障转移优先级为 West US = 0
、East US = 1
、South Central US = 2
。 此命令会将其更改为 West US = 0
、South Central US = 1
、East US = 2
。
注意
在 failoverPriority=0
的情况下更改位置会触发 Azure Cosmos DB 帐户的手动故障转移。 任何其他的优先级更改不会触发故障转移。
$resourceGroupName = "myResourceGroup"
$accountName = "mycosmosaccount"
$locations = @("West US", "South Central US", "East US") # Regions ordered by UPDATED failover priority
Update-AzCosmosDBAccountFailoverPriority `
-ResourceGroupName $resourceGroupName `
-Name $accountName `
-FailoverPolicy $locations
触发手动故障转移
对于配置了手动故障转移的帐户,可以通过将设置修改为 failoverPriority=0
来进行故障转移并将所有次要副本提升为主要副本。 此操作可用于启动灾难恢复演练以测试灾难恢复规划。
在下面的示例中,假设帐户的当前故障转移优先级为 West US = 0
和 East US = 1
,然后将区域互换。
注意
在 failoverPriority=0
的情况下更改 locationName
会触发 Azure Cosmos DB 帐户的手动故障转移。 任何其他优先级更改都不会触发故障转移。
注意
如果在异步吞吐量缩放操作正在进行时执行手动故障转移操作,则会暂停吞吐量纵向扩展操作。 故障转移操作完成后,它会自动继续。
$resourceGroupName = "myResourceGroup"
$accountName = "mycosmosaccount"
$locations = @("East US", "West US") # Regions ordered by UPDATED failover priority
Update-AzCosmosDBAccountFailoverPriority `
-ResourceGroupName $resourceGroupName `
-Name $accountName `
-FailoverPolicy $locations
列出 Azure Cosmos DB 帐户上的资源锁
资源锁可放置在数据库和集合等 Azure Cosmos DB 资源上。 以下示例演示了如何在 Azure Cosmos DB 帐户上列出所有的 Azure 资源锁。
$resourceGroupName = "myResourceGroup"
$resourceTypeAccount = "Microsoft.DocumentDB/databaseAccounts"
$accountName = "mycosmosaccount"
Get-AzResourceLock `
-ResourceGroupName $resourceGroupName `
-ResourceType $resourceTypeAccount `
-ResourceName $accountName
Azure Cosmos DB 数据库
以下部分演示了如何管理 Azure Cosmos DB 数据库,具体包括:
- 创建 Azure Cosmos DB 数据库
- 创建共享吞吐量的 Azure Cosmos DB 数据库
- 获取 Azure Cosmos DB 数据库的吞吐量
- 将数据库吞吐量迁移到自动缩放
- 列出帐户中的所有 Azure Cosmos DB 数据库
- 获取单个 Azure Cosmos DB 数据库
- 删除 Azure Cosmos DB 数据库
- 在 Azure Cosmos DB 数据库上创建资源锁以防止删除
- 删除 Azure Cosmos DB 数据库上的资源锁
创建 Azure Cosmos DB 数据库
$resourceGroupName = "myResourceGroup"
$accountName = "mycosmosaccount"
$databaseName = "myDatabase"
New-AzCosmosDBSqlDatabase `
-ResourceGroupName $resourceGroupName `
-AccountName $accountName `
-Name $databaseName
创建共享吞吐量的 Azure Cosmos DB 数据库
$resourceGroupName = "myResourceGroup"
$accountName = "mycosmosaccount"
$databaseName = "myDatabase"
$databaseRUs = 400
New-AzCosmosDBSqlDatabase `
-ResourceGroupName $resourceGroupName `
-AccountName $accountName `
-Name $databaseName `
-Throughput $databaseRUs
获取 Azure Cosmos DB 数据库的吞吐量
$resourceGroupName = "myResourceGroup"
$accountName = "mycosmosaccount"
$databaseName = "myDatabase"
Get-AzCosmosDBSqlDatabaseThroughput `
-ResourceGroupName $resourceGroupName `
-AccountName $accountName `
-Name $databaseName
将数据库吞吐量迁移到自动缩放
$resourceGroupName = "myResourceGroup"
$accountName = "mycosmosaccount"
$databaseName = "myDatabase"
Invoke-AzCosmosDBSqlDatabaseThroughputMigration `
-ResourceGroupName $resourceGroupName `
-AccountName $accountName `
-Name $databaseName `
-ThroughputType Autoscale
获取帐户中的所有 Azure Cosmos DB 数据库
$resourceGroupName = "myResourceGroup"
$accountName = "mycosmosaccount"
Get-AzCosmosDBSqlDatabase `
-ResourceGroupName $resourceGroupName `
-AccountName $accountName
获取单个 Azure Cosmos DB 数据库
$resourceGroupName = "myResourceGroup"
$accountName = "mycosmosaccount"
$databaseName = "myDatabase"
Get-AzCosmosDBSqlDatabase `
-ResourceGroupName $resourceGroupName `
-AccountName $accountName `
-Name $databaseName
删除 Azure Cosmos DB 数据库
$resourceGroupName = "myResourceGroup"
$accountName = "mycosmosaccount"
$databaseName = "myDatabase"
Remove-AzCosmosDBSqlDatabase `
-ResourceGroupName $resourceGroupName `
-AccountName $accountName `
-Name $databaseName
在 Azure Cosmos DB 数据库上创建资源锁以防止删除
$resourceGroupName = "myResourceGroup"
$resourceType = "Microsoft.DocumentDB/databaseAccounts/sqlDatabases"
$accountName = "mycosmosaccount"
$databaseName = "myDatabase"
$resourceName = "$accountName/$databaseName"
$lockName = "myResourceLock"
$lockLevel = "CanNotDelete"
New-AzResourceLock `
-ResourceGroupName $resourceGroupName `
-ResourceType $resourceType `
-ResourceName $resourceName `
-LockName $lockName `
-LockLevel $lockLevel
删除 Azure Cosmos DB 数据库上的资源锁
$resourceGroupName = "myResourceGroup"
$resourceType = "Microsoft.DocumentDB/databaseAccounts/sqlDatabases"
$accountName = "mycosmosaccount"
$databaseName = "myDatabase"
$resourceName = "$accountName/$databaseName"
$lockName = "myResourceLock"
Remove-AzResourceLock `
-ResourceGroupName $resourceGroupName `
-ResourceType $resourceType `
-ResourceName $resourceName `
-LockName $lockName
Azure Cosmos DB 容器
以下部分演示了如何管理 Azure Cosmos DB 容器,具体包括:
- 创建 Azure Cosmos DB 容器
- 使用自动缩放功能创建 Azure Cosmos DB 容器
- 使用大分区键创建 Azure Cosmos DB 容器
- 获取 Azure Cosmos DB 容器的吞吐量
- 将容器吞吐量迁移到自动缩放
- 使用自定义索引创建 Azure Cosmos DB 容器
- 在索引关闭的情况下创建 Azure Cosmos DB 容器
- 创建键和 TTL 都独一无二的 Azure Cosmos DB 容器
- 创建带冲突解决方案的 Azure Cosmos DB 容器
- 列出数据库中的所有 Azure Cosmos DB 容器
- 获取数据库中的单个 Azure Cosmos DB 容器
- 删除 Azure Cosmos DB 容器
- 在 Azure Cosmos DB 容器上创建资源锁以防止删除
- 删除 Azure Cosmos DB 容器上的资源锁
创建 Azure Cosmos DB 容器
# Create an Azure Cosmos DB container with default indexes and throughput at 400 RU
$resourceGroupName = "myResourceGroup"
$accountName = "mycosmosaccount"
$databaseName = "myDatabase"
$containerName = "myContainer"
$partitionKeyPath = "/myPartitionKey"
$throughput = 400 #minimum = 400
New-AzCosmosDBSqlContainer `
-ResourceGroupName $resourceGroupName `
-AccountName $accountName `
-DatabaseName $databaseName `
-Name $containerName `
-PartitionKeyKind Hash `
-PartitionKeyPath $partitionKeyPath `
-Throughput $throughput
使用自动缩放功能创建 Azure Cosmos DB 容器
# Create an Azure Cosmos DB container with default indexes and autoscale throughput at 4000 RU
$resourceGroupName = "myResourceGroup"
$accountName = "mycosmosaccount"
$databaseName = "myDatabase"
$containerName = "myContainer"
$partitionKeyPath = "/myPartitionKey"
$autoscaleMaxThroughput = 4000 #minimum = 4000
New-AzCosmosDBSqlContainer `
-ResourceGroupName $resourceGroupName `
-AccountName $accountName `
-DatabaseName $databaseName `
-Name $containerName `
-PartitionKeyKind Hash `
-PartitionKeyPath $partitionKeyPath `
-AutoscaleMaxThroughput $autoscaleMaxThroughput
使用大分区键大小创建 Azure Cosmos DB 容器
# Create an Azure Cosmos DB container with a large partition key value (version = 2)
$resourceGroupName = "myResourceGroup"
$accountName = "mycosmosaccount"
$databaseName = "myDatabase"
$containerName = "myContainer"
$partitionKeyPath = "/myPartitionKey"
New-AzCosmosDBSqlContainer `
-ResourceGroupName $resourceGroupName `
-AccountName $accountName `
-DatabaseName $databaseName `
-Name $containerName `
-PartitionKeyKind Hash `
-PartitionKeyPath $partitionKeyPath `
-PartitionKeyVersion 2
获取 Azure Cosmos DB 容器的吞吐量
$resourceGroupName = "myResourceGroup"
$accountName = "mycosmosaccount"
$databaseName = "myDatabase"
$containerName = "myContainer"
Get-AzCosmosDBSqlContainerThroughput `
-ResourceGroupName $resourceGroupName `
-AccountName $accountName `
-DatabaseName $databaseName `
-Name $containerName
将容器吞吐量迁移到自动缩放
$resourceGroupName = "myResourceGroup"
$accountName = "mycosmosaccount"
$databaseName = "myDatabase"
$containerName = "myContainer"
Invoke-AzCosmosDBSqlContainerThroughputMigration `
-ResourceGroupName $resourceGroupName `
-AccountName $accountName `
-DatabaseName $databaseName `
-Name $containerName `
-ThroughputType Autoscale
使用自定义索引策略创建 Azure Cosmos DB 容器
# Create a container with a custom indexing policy
$resourceGroupName = "myResourceGroup"
$accountName = "mycosmosaccount"
$databaseName = "myDatabase"
$containerName = "myContainer"
$partitionKeyPath = "/myPartitionKey"
$indexPathIncluded = "/*"
$indexPathExcluded = "/myExcludedPath/*"
$includedPathIndex = New-AzCosmosDBSqlIncludedPathIndex -DataType String -Kind Range
$includedPath = New-AzCosmosDBSqlIncludedPath -Path $indexPathIncluded -Index $includedPathIndex
$indexingPolicy = New-AzCosmosDBSqlIndexingPolicy `
-IncludedPath $includedPath `
-ExcludedPath $indexPathExcluded `
-IndexingMode Consistent `
-Automatic $true
New-AzCosmosDBSqlContainer `
-ResourceGroupName $resourceGroupName `
-AccountName $accountName `
-DatabaseName $databaseName `
-Name $containerName `
-PartitionKeyKind Hash `
-PartitionKeyPath $partitionKeyPath `
-IndexingPolicy $indexingPolicy
在索引关闭的情况下创建 Azure Cosmos DB 容器
# Create an Azure Cosmos DB container with no indexing
$resourceGroupName = "myResourceGroup"
$accountName = "mycosmosaccount"
$databaseName = "myDatabase"
$containerName = "myContainer"
$partitionKeyPath = "/myPartitionKey"
$indexingPolicy = New-AzCosmosDBSqlIndexingPolicy `
-IndexingMode None
New-AzCosmosDBSqlContainer `
-ResourceGroupName $resourceGroupName `
-AccountName $accountName `
-DatabaseName $databaseName `
-Name $containerName `
-PartitionKeyKind Hash `
-PartitionKeyPath $partitionKeyPath `
-IndexingPolicy $indexingPolicy
创建键策略和 TTL 都独一无二的 Azure Cosmos DB 容器
# Create a container with a unique key policy and TTL of one day
$resourceGroupName = "myResourceGroup"
$accountName = "mycosmosaccount"
$databaseName = "myDatabase"
$containerName = "myContainer"
$partitionKeyPath = "/myPartitionKey"
$uniqueKeyPath = "/myUniqueKeyPath"
$ttlInSeconds = 86400 # Set this to -1 (or don't use it at all) to never expire
$uniqueKey = New-AzCosmosDBSqlUniqueKey `
-Path $uniqueKeyPath
$uniqueKeyPolicy = New-AzCosmosDBSqlUniqueKeyPolicy `
-UniqueKey $uniqueKey
New-AzCosmosDBSqlContainer `
-ResourceGroupName $resourceGroupName `
-AccountName $accountName `
-DatabaseName $databaseName `
-Name $containerName `
-PartitionKeyKind Hash `
-PartitionKeyPath $partitionKeyPath `
-UniqueKeyPolicy $uniqueKeyPolicy `
-TtlInSeconds $ttlInSeconds
创建带冲突解决方案的 Azure Cosmos DB 容器
若要将所有冲突都写入 ConflictsFeed,然后分别进行处理,请传递 -Type "Custom" -Path ""
。
# Create container with last-writer-wins conflict resolution policy
$resourceGroupName = "myResourceGroup"
$accountName = "mycosmosaccount"
$databaseName = "myDatabase"
$containerName = "myContainer"
$partitionKeyPath = "/myPartitionKey"
$conflictResolutionPath = "/myResolutionPath"
$conflictResolutionPolicy = New-AzCosmosDBSqlConflictResolutionPolicy `
-Type LastWriterWins `
-Path $conflictResolutionPath
New-AzCosmosDBSqlContainer `
-ResourceGroupName $resourceGroupName `
-AccountName $accountName `
-DatabaseName $databaseName `
-Name $containerName `
-PartitionKeyKind Hash `
-PartitionKeyPath $partitionKeyPath `
-ConflictResolutionPolicy $conflictResolutionPolicy
若要创建冲突解决策略以使用存储过程,请调用 New-AzCosmosDBSqlConflictResolutionPolicy
并传递参数 -Type
和 -ConflictResolutionProcedure
。
# Create container with custom conflict resolution policy using a stored procedure
$resourceGroupName = "myResourceGroup"
$accountName = "mycosmosaccount"
$databaseName = "myDatabase"
$containerName = "myContainer"
$partitionKeyPath = "/myPartitionKey"
$conflictResolutionSprocName = "mysproc"
$conflictResolutionSproc = "/dbs/$databaseName/colls/$containerName/sprocs/$conflictResolutionSprocName"
$conflictResolutionPolicy = New-AzCosmosDBSqlConflictResolutionPolicy `
-Type Custom `
-ConflictResolutionProcedure $conflictResolutionSproc
New-AzCosmosDBSqlContainer `
-ResourceGroupName $resourceGroupName `
-AccountName $accountName `
-DatabaseName $databaseName `
-Name $containerName `
-PartitionKeyKind Hash `
-PartitionKeyPath $partitionKeyPath `
-ConflictResolutionPolicy $conflictResolutionPolicy
列出数据库中的所有 Azure Cosmos DB 容器
# List all Azure Cosmos DB containers in a database
$resourceGroupName = "myResourceGroup"
$accountName = "mycosmosaccount"
$databaseName = "myDatabase"
Get-AzCosmosDBSqlContainer `
-ResourceGroupName $resourceGroupName `
-AccountName $accountName `
-DatabaseName $databaseName
获取数据库中的单个 Azure Cosmos DB 容器
# Get a single Azure Cosmos DB container in a database
$resourceGroupName = "myResourceGroup"
$accountName = "mycosmosaccount"
$databaseName = "myDatabase"
$containerName = "myContainer"
Get-AzCosmosDBSqlContainer `
-ResourceGroupName $resourceGroupName `
-AccountName $accountName `
-DatabaseName $databaseName `
-Name $containerName
删除 Azure Cosmos DB 容器
# Delete an Azure Cosmos DB container
$resourceGroupName = "myResourceGroup"
$accountName = "mycosmosaccount"
$databaseName = "myDatabase"
$containerName = "myContainer"
Remove-AzCosmosDBSqlContainer `
-ResourceGroupName $resourceGroupName `
-AccountName $accountName `
-DatabaseName $databaseName `
-Name $containerName
在 Azure Cosmos DB 容器上创建资源锁以防止删除
$resourceGroupName = "myResourceGroup"
$resourceType = "Microsoft.DocumentDB/databaseAccounts/sqlDatabases/containers"
$accountName = "mycosmosaccount"
$databaseName = "myDatabase"
$containerName = "myContainer"
$resourceName = "$accountName/$databaseName/$containerName"
$lockName = "myResourceLock"
$lockLevel = "CanNotDelete"
New-AzResourceLock `
-ResourceGroupName $resourceGroupName `
-ResourceType $resourceType `
-ResourceName $resourceName `
-LockName $lockName `
-LockLevel $lockLevel
删除 Azure Cosmos DB 容器上的资源锁
$resourceGroupName = "myResourceGroup"
$resourceType = "Microsoft.DocumentDB/databaseAccounts/sqlDatabases/containers"
$accountName = "mycosmosaccount"
$databaseName = "myDatabase"
$containerName = "myContainer"
$resourceName = "$accountName/$databaseName/$containerName"
$lockName = "myResourceLock"
Remove-AzResourceLock `
-ResourceGroupName $resourceGroupName `
-ResourceType $resourceType `
-ResourceName $resourceName `
-LockName $lockName