使用 PowerShell 管理 Azure Cosmos DB SQL API 资源Manage Azure Cosmos DB SQL API resources using PowerShell

以下指南介绍了如何使用 Powershell 通过脚本来自动管理 Azure Cosmos DB 资源,其中包括帐户、数据库、容器和吞吐量。The following guide describes how to use PowerShell to script and automate management of Azure Cosmos DB resources, including account, database, container, and throughput. 管理 Azure Cosmos DB 是直接使用 AzResource cmdlet 通过 Azure Cosmos DB 资源提供程序进行的。Management of Azure Cosmos DB is handled through the AzResource cmdlet directly to the Azure Cosmos DB resource provider.

若要对 Azure Cosmos DB 进行跨平台管理,可以使用 Azure CLIREST APIAzure 门户For cross-platform management of Azure Cosmos DB, you can use Azure CLI, the REST API, or the Azure portal.

备注

本文进行了更新,以便使用新的 Azure PowerShell Az 模块。This article has been updated to use the new Azure PowerShell Az module. 你仍然可以使用 AzureRM 模块,至少在 2020 年 12 月之前,它将继续接收 bug 修补程序。You can still use the AzureRM module, which will continue to receive bug fixes until at least December 2020. 若要详细了解新的 Az 模块和 AzureRM 兼容性,请参阅新 Azure Powershell Az 模块简介To learn more about the new Az module and AzureRM compatibility, see Introducing the new Azure PowerShell Az module. 有关 Az 模块安装说明,请参阅安装 Azure PowerShellFor Az module installation instructions, see Install Azure PowerShell.

入门Getting Started

请按照如何安装和配置 Azure PowerShell 中的说明安装 PowerShell 并在其中登录 Azure 帐户。Follow the instructions in How to install and configure Azure PowerShell to install and sign in to your Azure account in Powershell.

  • 如果想要执行以下命令而无需用户确认,请向命令追加 -Force 标志。If you would like to execute the following commands without requiring user confirmation, append the -Force flag to the command.
  • 以下所有命令都是同步执行的。All the following commands are synchronous.

Azure Cosmos 帐户Azure Cosmos Accounts

以下部分演示如何管理 Azure Cosmos 帐户,包括:The following sections demonstrate how to manage the Azure Cosmos account, including:

创建 Azure Cosmos 帐户Create an Azure Cosmos account

此命令创建一个 Azure Cosmos 数据库帐户,该帐户使用多区域、有限过期一致性策略This command creates an Azure Cosmos database account with multiple-regions, bounded-staleness consistency policy.

# Create an Azure Cosmos Account for Core (SQL) API
$resourceGroupName = "myResourceGroup"
$location = "China North 2"
$accountName = "mycosmosaccount" # must be lowercase and < 31 characters .

$locations = @(
    @{ "locationName"="China North 2"; "failoverPriority"=0 },
    @{ "locationName"="China East 2"; "failoverPriority"=1 }
)

$consistencyPolicy = @{
    "defaultConsistencyLevel"="BoundedStaleness";
    "maxIntervalInSeconds"=300;
    "maxStalenessPrefix"=100000
}

$CosmosDBProperties = @{
    "databaseAccountOfferType"="Standard";
    "locations"=$locations;
    "consistencyPolicy"=$consistencyPolicy;
    "enableMultipleWriteLocations"="false"
}

New-AzResource -ResourceType "Microsoft.DocumentDb/databaseAccounts" `
    -ApiVersion "2015-04-08" -ResourceGroupName $resourceGroupName -Location $location `
    -Name $accountName -PropertyObject $CosmosDBProperties
  • $accountName:Azure Cosmos 帐户的名称。$accountName The name for the Azure Cosmos account. 必须为小写,接受字母数字和“-”字符,长度必须为 3 到 31 个字符。Must be lowercase, accepts alphanumeric and the '-' character, and between 3 and 31 characters.
  • $location Azure Cosmos 帐户资源的位置。$location The location for the Azure Cosmos account resource.
  • $locations:数据库帐户的副本区域。$locations The replica regions for the database account. 每个数据库帐户必须有一个故障转移优先级值为 0 的写入区域。There must be one write region per database account with a failover priority value of 0.
  • $consistencyPolicy:Azure Cosmos 帐户的默认一致性级别。$consistencyPolicy The default consistency level of the Azure Cosmos account. 有关详细信息,请参阅 Azure Cosmos DB 中的一致性级别For more information, see Consistency Levels in Azure Cosmos DB.
  • $CosmosDBProperties:传递给 Cosmos DB Azure 资源管理器提供程序的、用于预配帐户的属性值。$CosmosDBProperties The property values passed to the Cosmos DB Azure Resource Manager Provider to provision the account.

可以使用 IP 防火墙以及虚拟网络服务终结点配置 Azure Cosmos 帐户。Azure Cosmos accounts can be configured with IP Firewall as well as Virtual Network service end points. 有关如何为 Azure Cosmos DB 配置 IP 防火墙的信息,请参阅配置 IP 防火墙For information on how to configure the IP Firewall for Azure Cosmos DB, see Configure IP Firewall. 若要详细了解如何为 Azure Cosmos DB 启用服务终结点,请参阅配置从虚拟网络进行访问的权限For more information on how to enable service endpoints for Azure Cosmos DB, see Configure access from virtual Networks.

列出订阅中的所有 Azure Cosmos 帐户List all Azure Cosmos accounts in a subscription

可以使用此命令列出订阅中的所有 Azure Cosmos 帐户。This command allows you to list all Azure Cosmos accounts in a subscription.

# List Azure Cosmos Accounts

Get-AzResource -ResourceType Microsoft.DocumentDb/databaseAccounts | ft

获取 Azure Cosmos 帐户的属性Get the properties of an Azure Cosmos account

使用此命令可以获取现有 Azure Cosmos 帐户的属性。This command allows you to get the properties of an existing Azure Cosmos account.

# Get the properties of an Azure Cosmos Account

$resourceGroupName = "myResourceGroup"
$accountName = "mycosmosaccount"

Get-AzResource -ResourceType "Microsoft.DocumentDb/databaseAccounts" `
    -ApiVersion "2015-04-08" -ResourceGroupName $resourceGroupName `
    -Name $accountName | Select-Object Properties

更新 Azure Cosmos 帐户Update an Azure Cosmos account

此命令可更新 Azure Cosmos 数据库帐户属性。This command allows you to update your Azure Cosmos database account properties. 可更新的属性包括:Properties that can be updated include the following:

  • 添加或删除区域Adding or removing regions
  • 更改默认的一致性策略Changing default consistency policy
  • 更改 IP 范围筛选器Changing IP Range Filter
  • 更改虚拟网络配置Changing Virtual Network configurations
  • 启用多主数据库Enabling Multi-master

备注

不能同时添加或删除区域 locations 并更改 Azure Cosmos 帐户的其他属性。You cannot simultaneously add or remove regions locations and change other properties for an Azure Cosmos account. 修改区域的操作必须作为单独的操作与任何其他对帐户资源的更改操作分开执行。Modifying regions must be performed as a separate operation than any other change to the account resource.

备注

此命令可添加和删除区域,但不可修改故障转移优先级或触发手动故障转移。This command allows you to add and remove regions but does not allow you to modify failover priorities or trigger a manual failover. 请参阅修改故障转移优先级触发手动故障转移See Modify failover priority and Trigger manual failover.

# Create an account with 2 regions
$resourceGroupName = "myResourceGroup"
$resourceType = "Microsoft.DocumentDb/databaseAccounts"
$accountName = "mycosmosaccount" # must be lower case and < 31 characters

$locations = @(
    @{ "locationName"="China North 2"; "failoverPriority"=0, "isZoneRedundant"=false },
    @{ "locationName"="China East 2"; "failoverPriority"=1, "isZoneRedundant"=false }
)
$consistencyPolicy = @{ "defaultConsistencyLevel"="Session" }
$CosmosDBProperties = @{
    "databaseAccountOfferType"="Standard";
    "locations"=$locations;
    "consistencyPolicy"=$consistencyPolicy
}
New-AzResource -ResourceType $resourceType `
    -ApiVersion "2015-04-08" -ResourceGroupName $resourceGroupName `
    -Name $accountName -PropertyObject $CosmosDBProperties

# Add a region
$account = Get-AzResource -ResourceType $resourceType `
    -ApiVersion "2015-04-08" -ResourceGroupName $resourceGroupName `
    -Name $accountName

$locations = @(
    @{ "locationName"="China North 2"; "failoverPriority"=0, "isZoneRedundant"=false },
    @{ "locationName"="China East 2"; "failoverPriority"=1, "isZoneRedundant"=false },
    @{ "locationName"="China East"; "failoverPriority"=2, "isZoneRedundant"=false }
)

$account.Properties.locations = $locations
$CosmosDBProperties = $account.Properties

Set-AzResource -ResourceType $resourceType `
    -ApiVersion "2015-04-08" -ResourceGroupName $resourceGroupName `
    -Name $accountName -PropertyObject $CosmosDBProperties

# Azure Resource Manager does not wait on the resource update
Write-Host "Confirm region added before continuing..."

# Remove a region
$account = Get-AzResource -ResourceType $resourceType `
    -ApiVersion "2015-04-08" -ResourceGroupName $resourceGroupName `
    -Name $accountName

$locations = @(
    @{ "locationName"="China North 2"; "failoverPriority"=0, "isZoneRedundant"=false },
    @{ "locationName"="China East 2"; "failoverPriority"=1, "isZoneRedundant"=false }
)

$account.Properties.locations = $locations
$CosmosDBProperties = $account.Properties

Set-AzResource -ResourceType $resourceType `
    -ApiVersion "2015-04-08" -ResourceGroupName $resourceGroupName `
    -Name $accountName -PropertyObject $CosmosDBProperties

为 Azure Cosmos 帐户启用多个写入区域Enable multiple write regions for an Azure Cosmos account

# Update an Azure Cosmos account from single to multi-master
$resourceGroupName = "myResourceGroup"
$accountName = "mycosmosaccount"
$resourceType = "Microsoft.DocumentDb/databaseAccounts"

$account = Get-AzResource -ResourceType $resourceType `
    -ApiVersion "2015-04-08" -ResourceGroupName $resourceGroupName `
    -Name $accountName

$account.Properties.enableMultipleWriteLocations = "true"
$CosmosDBProperties = $account.Properties

Set-AzResource -ResourceType $resourceType `
    -ApiVersion "2015-04-08" -ResourceGroupName $resourceGroupName `
    -Name $accountName -PropertyObject $CosmosDBProperties

删除 Azure Cosmos 帐户Delete an Azure Cosmos account

使用此命令可以删除现有的 Azure Cosmos 帐户。This command allows you to delete an existing Azure Cosmos account.

# Delete an Azure Cosmos Account
$resourceGroupName = "myResourceGroup"
$accountName = "mycosmosaccount"

Remove-AzResource -ResourceType "Microsoft.DocumentDb/databaseAccounts" `
    -ApiVersion "2015-04-08" -ResourceGroupName $resourceGroupName `
    -Name $accountName

更新 Azure Cosmos 帐户的标记Update Tags of an Azure Cosmos account

下面的示例介绍如何设置 Azure Cosmos 帐户的 Azure 资源标记The following example describes how to set Azure resource tags for an Azure Cosmos account.

备注

通过此将 -Tags 标志追加到相应的参数,可将此命令与创建或更新命令组合。This command can be combined with the create or update commands by appending the -Tags flag with the corresponding parameter.

# Update tags for an Azure Cosmos Account

$resourceGroupName = "myResourceGroup"
$accountName = "mycosmosaccount"

$tags = @{
    "dept" = "Finance";
    "environment" = "Production"
}

Set-AzResource -ResourceType "Microsoft.DocumentDb/databaseAccounts" `
    -ApiVersion "2015-04-08" -ResourceGroupName $resourceGroupName `
    -Name $accountName -Tags $tags

列出帐户密钥List Account Keys

创建 Azure Cosmos DB 帐户时,该服务会生成两个主访问密钥,用于访问 Azure Cosmos DB 帐户时的身份验证。When you create an Azure Cosmos DB account, the service generates two master access keys that can be used for authentication when the Azure Cosmos DB account is accessed. Azure Cosmos DB 提供有两个访问密钥,因此可在不中断 Azure Cosmos DB 帐户连接的情况下重新生成密钥。By providing two access keys, Azure Cosmos DB enables you to regenerate the keys with no interruption to your Azure Cosmos DB account. 还提供用于对只读操作进行身份验证的只读密钥。Read-only keys for authenticating read-only operations are also available. 有两个读写密钥(主密钥和辅助密钥)和两个只读密钥(主密钥和辅助密钥)。There are two read-write keys (primary and secondary) and two read-only keys (primary and secondary).

# List keys for an Azure Cosmos Account

$resourceGroupName = "myResourceGroup"
$accountName = "mycosmosaccount"

$keys = Invoke-AzResourceAction -Action listKeys `
    -ResourceType "Microsoft.DocumentDb/databaseAccounts" -ApiVersion "2015-04-08" `
    -ResourceGroupName $resourceGroupName -Name $accountName

Write-Host "PrimaryKey =" $keys.primaryMasterKey
Write-Host "SecondaryKey =" $keys.secondaryMasterKey

列出连接字符串List Connection Strings

对于 MongoDB 帐户,可以使用以下命令检索将 MongoDB 应用连接到数据库帐户的连接字符串。For MongoDB accounts, the connection string to connect your MongoDB app to the database account can be retrieved using the following command.

# List connection strings for an Azure Cosmos Account

$resourceGroupName = "myResourceGroup"
$accountName = "mycosmosaccount"

$keys = Invoke-AzResourceAction -Action listConnectionStrings `
    -ResourceType "Microsoft.DocumentDb/databaseAccounts" -ApiVersion "2015-04-08" `
    -ResourceGroupName $resourceGroupName -Name $accountName

Select-Object $keys

重新生成帐户密钥Regenerate Account Keys

应定期重新生成 Azure Cosmos 帐户的访问密钥,以帮助提高连接安全性。Access keys to an Azure Cosmos account should be periodically regenerated to help keep connections more secure. 系统会向帐户分配主要和辅助访问密钥。A primary and secondary access keys are assigned to the account. 这样,在重新生成其中的一个密钥时,客户端仍可以保持访问。This allows clients to maintain access while the other is regenerated. Azure Cosmos 帐户有四种类型的密钥(Primary、Secondary、PrimaryReadonly 和 SecondaryReadonly)There are four types of keys for an Azure Cosmos account (Primary, Secondary, PrimaryReadonly, and SecondaryReadonly)

# Regenerate the primary key for an Azure Cosmos Account

$resourceGroupName = "myResourceGroup"
$accountName = "mycosmosaccount"

$keyKind = @{ "keyKind"="Primary" }

$keys = Invoke-AzResourceAction -Action regenerateKey `
    -ResourceType "Microsoft.DocumentDb/databaseAccounts" -ApiVersion "2015-04-08" `
    -ResourceGroupName $resourceGroupName -Name $accountName -Parameters $keyKind

Select-Object $keys

启用自动故障转移Enable automatic failover

使 Cosmos 帐户能够在主要区域不可用时故障转移到其次要区域。Enables a Cosmos account to failover to its secondary region should the primary region become unavailable.

$resourceGroupName = "myResourceGroup"
$accountName = "mycosmosaccount"
$resourceType = "Microsoft.DocumentDb/databaseAccounts"

$account = Get-AzResource -ResourceType $resourceType `
    -ApiVersion "2015-04-08" -ResourceGroupName $resourceGroupName `
    -Name $accountName

$account.Properties.enableAutomaticFailover="true";
$CosmosDBProperties = $account.Properties;

Set-AzResource -ResourceType $resourceType `
    -ApiVersion "2015-04-08" -ResourceGroupName $resourceGroupName `
    -Name $accountName -PropertyObject $CosmosDBProperties

修改故障转移优先级Modify Failover Priority

对于配置了自动故障转移的帐户,可以更改在主要副本不可用时 Cosmos 将次要副本提升为主要副本的顺序。For accounts configured with Automatic Failover, you can change the order in which Cosmos will promote secondary replicas to primary should the primary become unavailable.

对于以下示例,假定当前的故障转移优先级为 China North 2 = 0China East 2 = 1China East = 2For the example below, assume the current failover priority, China North 2 = 0, China East 2 = 1, China East = 2.

注意

failoverPriority=0 的情况下更改 locationName 会触发 Azure Cosmos 帐户的手动故障转移。Changing locationName for failoverPriority=0 will trigger a manual failover for an Azure Cosmos account. 任何其他的优先级更改不会触发故障转移。Any other priority changes will not trigger a failover.

# Change the failover priority for an Azure Cosmos Account
# Assume existing priority is "China North 2" = 0, "China East 2" = 1, "China East" = 2

$resourceGroupName = "myResourceGroup"
$accountName = "mycosmosaccount"

$failoverRegions = @(
    @{ "locationName"="China North 2"; "failoverPriority"=0 },
    @{ "locationName"="China East"; "failoverPriority"=1 },
    @{ "locationName"="China East 2"; "failoverPriority"=2 }
)

$failoverPolicies = @{
    "failoverPolicies"= $failoverRegions
}

Invoke-AzResourceAction -Action failoverPriorityChange `
    -ResourceType "Microsoft.DocumentDb/databaseAccounts" -ApiVersion "2015-04-08" `
    -ResourceGroupName $resourceGroupName -Name $accountName -Parameters $failoverPolicies

触发手动故障转移Trigger Manual Failover

对于配置了手动故障转移的帐户,可以通过修改为 failoverPriority=0 来进行故障转移并将所有次要副本提升为主要副本。For accounts configured with Manual Failover, you can failover and promote any secondary replica to primary by modifying to failoverPriority=0. 此操作可用于启动灾难恢复演练以测试灾难恢复规划。This operation can be used to initiate a disaster recovery drill to test disaster recovery planning.

在下面的示例中,假设帐户的当前故障转移优先级为 China North 2 = 0China East 2 = 1,然后将区域互换。For the example below, assume the account has a current failover priority of China North 2 = 0 and China East 2 = 1 and flip the regions.

注意

failoverPriority=0 的情况下更改 locationName 会触发 Azure Cosmos 帐户的手动故障转移。Changing locationName for failoverPriority=0 will trigger a manual failover for an Azure Cosmos account. 任何其他优先级更改都不会触发故障转移。Any other priority change will not trigger a failover.

# Change the failover priority for an Azure Cosmos Account
# Assume existing priority is "China North 2" = 0, "China East 2" = 1, "China East" = 2

$resourceGroupName = "myResourceGroup"
$accountName = "mycosmosaccount"

$failoverRegions = @(
    @{ "locationName"="China East"; "failoverPriority"=0 },
    @{ "locationName"="China East 2"; "failoverPriority"=1 },
    @{ "locationName"="China North 2"; "failoverPriority"=2 }
)

$failoverPolicies = @{
    "failoverPolicies"= $failoverRegions
}

Invoke-AzResourceAction -Action failoverPriorityChange `
    -ResourceType "Microsoft.DocumentDb/databaseAccounts" -ApiVersion "2015-04-08" `
    -ResourceGroupName $resourceGroupName -Name $accountName -Parameters $failoverPolicies

Azure Cosmos 数据库Azure Cosmos Database

以下部分演示如何管理 Azure Cosmos 数据库,包括:The following sections demonstrate how to manage the Azure Cosmos database, including:

创建 Azure Cosmos 数据库Create an Azure Cosmos database

# Create an Azure Cosmos database
$resourceGroupName = "myResourceGroup"
$accountName = "mycosmosaccount"
$databaseName = "database1"
$resourceName = $accountName + "/sql/" + $databaseName

$DataBaseProperties = @{
    "resource"=@{"id"=$databaseName}
}

New-AzResource -ResourceType "Microsoft.DocumentDb/databaseAccounts/apis/databases" `
    -ApiVersion "2015-04-08" -ResourceGroupName $resourceGroupName `
    -Name $resourceName -PropertyObject $DataBaseProperties

创建使用共享吞吐量的 Azure Cosmos 数据库Create an Azure Cosmos database with shared throughput

$resourceGroupName = "myResourceGroup"
$accountName = "mycosmosaccount"
$databaseName = "database2"
$resourceName = $accountName + "/sql/" + $databaseName

$DataBaseProperties = @{
    "resource"=@{ "id"=$databaseName };
    "options"=@{ "Throughput"="400" }
}

New-AzResource -ResourceType "Microsoft.DocumentDb/databaseAccounts/apis/databases" `
    -ApiVersion "2015-04-08" -ResourceGroupName $resourceGroupName `
    -Name $resourceName -PropertyObject $DataBaseProperties

获取 Azure Cosmos 数据库的吞吐量Get the throughput of an Azure Cosmos database

$resourceGroupName = "myResourceGroup"
$accountName = "mycosmosaccount"
$databaseName = "database1"
$containerName = "container1"
$databaseThroughputResourceType = "Microsoft.DocumentDb/databaseAccounts/apis/databases/settings"
$databaseThroughputResourceName = $accountName + "/sql/" + $databaseName + "/throughput"

Get-AzResource -ResourceType $databaseThroughputResourceType `
    -ApiVersion "2015-04-08" -ResourceGroupName $resourceGroupName `
    -Name $databaseThroughputResourceName  | Select-Object Properties

获取帐户中的所有 Azure Cosmos 数据库Get all Azure Cosmos databases in an account

# Get all databases in an Azure Cosmos account
$resourceGroupName = "myResourceGroup"
$accountName = "mycosmosaccount"
$resourceName = $accountName + "/sql/"

Get-AzResource -ResourceType "Microsoft.DocumentDb/databaseAccounts/apis/databases" `
    -ApiVersion "2015-04-08" -ResourceGroupName $resourceGroupName `
    -Name $resourceName  | Select-Object Properties

获取单个 Azure Cosmos 数据库Get a single Azure Cosmos database

# Get a single database in an Azure Cosmos account
$resourceGroupName = "myResourceGroup"
$accountName = "mycosmosaccount"
$databaseName = "database1"
$resourceName = $accountName + "/sql/" + $databaseName

Get-AzResource -ResourceType "Microsoft.DocumentDb/databaseAccounts/apis/databases" `
    -ApiVersion "2015-04-08" -ResourceGroupName $resourceGroupName `
    -Name $resourceName | Select-Object Properties

删除 Azure Cosmos 数据库Delete an Azure Cosmos database

# Delete a database in an Azure Cosmos account
$resourceGroupName = "myResourceGroup"
$accountName = "mycosmosaccount"
$databaseName = "database1"
$resourceName = $accountName + "/sql/" + $databaseName
Remove-AzResource -ResourceType "Microsoft.DocumentDb/databaseAccounts/apis/databases" `
    -ApiVersion "2015-04-08" -ResourceGroupName $resourceGroupName -Name $resourceName

Azure Cosmos 容器Azure Cosmos Container

以下部分演示如何管理 Azure Cosmos 容器,包括:The following sections demonstrate how to manage the Azure Cosmos container, including:

创建 Azure Cosmos 容器Create an Azure Cosmos container

# Create an Azure Cosmos container with default indexes and throughput at 400 RU
$resourceGroupName = "myResourceGroup"
$accountName = "mycosmosaccount"
$databaseName = "database1"
$containerName = "container1"
$resourceName = $accountName + "/sql/" + $databaseName + "/" + $containerName

$ContainerProperties = @{
    "resource"=@{
        "id"=$containerName;
        "partitionKey"=@{
            "paths"=@("/myPartitionKey");
            "kind"="Hash"
        }
    };
    "options"=@{ "Throughput"="400" }
}

New-AzResource -ResourceType "Microsoft.DocumentDb/databaseAccounts/apis/databases/containers" `
    -ApiVersion "2015-04-08" -ResourceGroupName $resourceGroupName `
    -Name $resourceName -PropertyObject $ContainerProperties

创建具有大分区键大小的 Azure Cosmos 容器Create an Azure Cosmos container with a large partition key size

# Create an Azure Cosmos container with a large partition key value (version = 2)
$resourceGroupName = "myResourceGroup"
$accountName = "mycosmosaccount"
$databaseName = "database1"
$containerName = "container1"
$resourceName = $accountName + "/sql/" + $databaseName + "/" + $containerName

$ContainerProperties = @{
    "resource"=@{
        "id"=$containerName;
        "partitionKey"=@{
            "paths"=@("/myPartitionKey");
            "kind"="Hash";
            "version" = 2
        }
    };
    "options"=@{ "Throughput"="400" }
}

New-AzResource -ResourceType "Microsoft.DocumentDb/databaseAccounts/apis/databases/containers" `
    -ApiVersion "2015-04-08" -ResourceGroupName $resourceGroupName `
    -Name $resourceName -PropertyObject $ContainerProperties

获取 Azure Cosmos 容器的吞吐量Get the throughput of an Azure Cosmos container

$resourceGroupName = "myResourceGroup"
$accountName = "mycosmosaccount"
$databaseName = "database1"
$containerName = "container1"
$containerThroughputResourceType = "Microsoft.DocumentDb/databaseAccounts/apis/databases/containers/settings"
$containerThroughputResourceName = $accountName + "/sql/" + $databaseName + "/" + $containerName + "/throughput"

Get-AzResource -ResourceType $containerThroughputResourceType `
    -ApiVersion "2015-04-08" -ResourceGroupName $resourceGroupName `
    -Name $containerThroughputResourceName  | Select-Object Properties

创建使用共享吞吐量的 Azure Cosmos 容器Create an Azure Cosmos container with shared throughput

$resourceGroupName = "myResourceGroup"
$accountName = "mycosmosaccount"
$databaseName = "database1"
$containerName = "container2"
$resourceName = $accountName + "/sql/" + $databaseName + "/" + $containerName

$ContainerProperties = @{
    "resource"=@{
        "id"=$containerName; 
        "partitionKey"=@{
            "paths"=@("/myPartitionKey");
            "kind"="Hash"
        }
    };
    "options"=@{}
}

New-AzResource -ResourceType "Microsoft.DocumentDb/databaseAccounts/apis/databases/containers" `
    -ApiVersion "2015-04-08" -ResourceGroupName $resourceGroupName `
    -Name $resourceName -PropertyObject $ContainerProperties 

创建使用自定义索引策略的 Azure Cosmos 容器Create an Azure Cosmos container with custom index policy

# Create a container with a custom indexing policy
$resourceGroupName = "myResourceGroup"
$accountName = "mycosmosaccount"
$databaseName = "database1"
$containerName = "container3"
$resourceName = $accountName + "/sql/" + $databaseName + "/" + $containerName

$ContainerProperties = @{
    "resource"=@{
        "id"=$containerName; 
        "partitionKey"=@{
            "paths"=@("/myPartitionKey"); 
            "kind"="Hash"
        }; 
        "indexingPolicy"=@{
            "indexingMode"="Consistent";
            "includedPaths"= @(@{
                "path"="/*";
                "indexes"= @()
            });
            "excludedPaths"= @(@{
                "path"="/myPathToNotIndex/*"
            })
        }
    };
    "options"=@{ "Throughput"="400" }
}

New-AzResource -ResourceType "Microsoft.DocumentDb/databaseAccounts/apis/databases/containers" `
    -ApiVersion "2015-04-08" -ResourceGroupName $resourceGroupName `
    -Name $resourceName -PropertyObject $ContainerProperties

创建禁用索引的 Azure Cosmos 容器Create an Azure Cosmos container with indexing turned off

# Create an Azure Cosmos container with no indexing
$resourceGroupName = "myResourceGroup"
$accountName = "mycosmosaccount"
$databaseName = "database1"
$containerName = "container4"
$resourceName = $accountName + "/sql/" + $databaseName + "/" + $containerName

$ContainerProperties = @{
    "resource"=@{
        "id"=$containerName; 
        "partitionKey"=@{
            "paths"=@("/myPartitionKey");
            "kind"="Hash"
        };
        "indexingPolicy"=@{
            "indexingMode"="none"
        }
    };
    "options"=@{ "Throughput"="400" }
} 

New-AzResource -ResourceType "Microsoft.DocumentDb/databaseAccounts/apis/databases/containers" `
    -ApiVersion "2015-04-08" -ResourceGroupName $resourceGroupName `
    -Name $resourceName -PropertyObject $ContainerProperties

创建使用唯一键策略和 TTL 的 Azure Cosmos 容器Create an Azure Cosmos container with unique key policy and TTL

# Create a container with a unique key policy and TTL of one day
$resourceGroupName = "myResourceGroup"
$accountName = "mycosmosaccount"
$databaseName = "database1"
$containerName = "container5"
$resourceName = $accountName + "/sql/" + $databaseName + "/" + $containerName

$ContainerProperties = @{
    "resource"=@{
        "id"=$containerName;
        "partitionKey"=@{
            "paths"=@("/myPartitionKey");
            "kind"="Hash"
        }; 
        "indexingPolicy"=@{
            "indexingMode"="Consistent";
            "includedPaths"= @(@{
                "path"="/*";
                "indexes"= @()
            });
            "excludedPaths"= @()
        };
        "uniqueKeyPolicy"= @{
            "uniqueKeys"= @(@{
                "paths"= @(
                    "/myUniqueKey1";
                    "/myUniqueKey2"
                )
            })
        };
        "defaultTtl"= 86400;
    };
    "options"=@{ "Throughput"="400" }
}

New-AzResource -ResourceType "Microsoft.DocumentDb/databaseAccounts/apis/databases/containers" `
    -ApiVersion "2015-04-08" -ResourceGroupName $resourceGroupName `
    -Name $resourceName -PropertyObject $ContainerProperties

创建使用冲突解决策略的 Azure Cosmos 容器Create an Azure Cosmos container with conflict resolution

若要创建冲突解决策略以使用某个存储过程,请设置 "mode"="custom",并将解决策略路径设置为该存储过程的名称:"conflictResolutionPath"="myResolverStoredProcedure"To create a conflict resolution policy to use a stored procedure, set "mode"="custom" and set the resolution path as the name of the stored procedure, "conflictResolutionPath"="myResolverStoredProcedure". 若要将所有冲突写入 ConflictsFeed 并分开进行处理,请设置 "mode"="custom""conflictResolutionPath"=""To write all conflicts to the ConflictsFeed and handle separately, set "mode"="custom" and "conflictResolutionPath"=""

# Create container with last-writer-wins conflict resolution policy
$resourceGroupName = "myResourceGroup"
$accountName = "mycosmosaccount"
$databaseName = "database1"
$containerName = "container6"
$resourceName = $accountName + "/sql/" + $databaseName + "/" + $containerName

$ContainerProperties = @{
    "resource"=@{
        "id"=$containerName;
        "partitionKey"=@{
            "paths"=@("/myPartitionKey");
            "kind"="Hash"
        };
        "conflictResolutionPolicy"=@{
            "mode"="lastWriterWins";
            "conflictResolutionPath"="/myResolutionPath"
        }
    };
    "options"=@{ "Throughput"="400" }
}

New-AzResource -ResourceType "Microsoft.DocumentDb/databaseAccounts/apis/databases/containers" `
    -ApiVersion "2015-04-08" -ResourceGroupName $resourceGroupName `
    -Name $resourceName -PropertyObject $ContainerProperties

列出数据库中的所有 Azure Cosmos 容器List all Azure Cosmos containers in a database

# List all Azure Cosmos containers in a database
$resourceGroupName = "myResourceGroup"
$accountName = "mycosmosaccount"
$databaseName = "database1"
$resourceName = $accountName + "/sql/" + $databaseName

Get-AzResource -ResourceType "Microsoft.DocumentDb/databaseAccounts/apis/databases/containers" `
    -ApiVersion "2015-04-08" -ResourceGroupName $resourceGroupName `
    -Name $resourceName | Select-Object Properties

获取数据库中的单个 Azure Cosmos 容器Get a single Azure Cosmos container in a database

# Get a single Azure Cosmos container in a database
$resourceGroupName = "myResourceGroup"
$accountName = "mycosmosaccount"
$databaseName = "database1"
$containerName = "container5"
$resourceName = $accountName + "/sql/" + $databaseName + "/" + $containerName

Get-AzResource -ResourceType "Microsoft.DocumentDb/databaseAccounts/apis/databases/containers" `
    -ApiVersion "2015-04-08" -ResourceGroupName $resourceGroupName `
    -Name $resourceName | Select-Object Properties

删除 Azure Cosmos 容器Delete an Azure Cosmos container

# Delete an Azure Cosmos container
$resourceGroupName = "myResourceGroup"
$accountName = "mycosmosaccount"
$databaseName = "database1"
$containerName = "container1"
$resourceName = $accountName + "/sql/" + $databaseName + "/" + $containerName

Remove-AzResource -ResourceType "Microsoft.DocumentDb/databaseAccounts/apis/databases/containers" `
    -ApiVersion "2015-04-08" -ResourceGroupName $resourceGroupName -Name $resourceName

后续步骤Next steps