创建 Azure Cosmos 容器Create an Azure Cosmos container

本文介绍如何使用 Azure 门户、Azure CLI、PowerShell 或受支持 SDK 以不同方式来创建 Azure Cosmos 容器(集合、表或图形)。This article explains the different ways to create an Azure Cosmos container (collection, table, or graph) using Azure portal, Azure CLI, PowerShell, or supported SDKs. 本文演示如何创建容器、指定分区键和预配吞吐量。This article demonstrates how to create a container, specify the partition key, and provision throughput.

备注

创建容器时,请确保不创建名称相同但大小写不同的两个容器。When creating containers, make sure you don't create two containers with the same name but different casing. 这是因为 Azure 平台的某些部分不区分大小写,这可能会对具有此类名称的容器导致遥测和操作混乱/冲突。That's because some parts of the Azure platform are not case-sensitive, and this can result in confusion/collision of telemetry and actions on containers with such names.

使用 Azure 门户创建容器Create a container using Azure portal

SQL APISQL API

  1. 登录到 Azure 门户Sign in to the Azure portal.

  2. 创建新的 Azure Cosmos 帐户或选择现有的帐户。Create a new Azure Cosmos account, or select an existing account.

  3. 打开“数据资源管理器”窗格,然后选择“新建容器” 。Open the Data Explorer pane, and select New Container. 接下来,请提供以下详细信息:Next, provide the following details:

    • 表明要创建新数据库还是使用现有数据库。Indicate whether you are creating a new database or using an existing one.
    • 输入容器 ID。Enter a container ID.
    • 输入分区键。Enter a partition key.
    • 输入要进行预配的吞吐量(例如,1000 RU)。Enter a throughput to be provisioned (for example, 1000 RUs).
    • 选择“确定” 。Select OK.

    “数据资源管理器”窗格的屏幕截图,其中突出显示了“新建容器”

用于 MongoDB 的 Azure Cosmos DB APIAzure Cosmos DB API for MongoDB

  1. 登录到 Azure 门户Sign in to the Azure portal.

  2. 创建新的 Azure Cosmos 帐户或选择现有的帐户。Create a new Azure Cosmos account, or select an existing account.

  3. 打开“数据资源管理器”窗格,然后选择“新建容器” 。Open the Data Explorer pane, and select New Container. 接下来,请提供以下详细信息:Next, provide the following details:

    • 表明要创建新数据库还是使用现有数据库。Indicate whether you are creating a new database or using an existing one.
    • 输入容器 ID。Enter a container ID.
    • 输入分片键。Enter a shard key.
    • 输入要进行预配的吞吐量(例如,1000 RU)。Enter a throughput to be provisioned (for example, 1000 RUs).
    • 选择“确定” 。Select OK.

    Azure Cosmos DB API for MongoDB“添加容器”对话框的屏幕截图

Cassandra APICassandra API

  1. 登录到 Azure 门户Sign in to the Azure portal.

  2. 创建新的 Azure Cosmos 帐户或选择现有的帐户。Create a new Azure Cosmos account, or select an existing account.

  3. 打开“数据资源管理器”窗格,然后选择“新建表” 。Open the Data Explorer pane, and select New Table. 接下来,请提供以下详细信息:Next, provide the following details:

    • 表明要创建新密钥空间还是使用现有密钥空间。Indicate whether you are creating a new keyspace, or using an existing one.
    • 输入表名称。Enter a table name.
    • 输入属性并指定一个主键。Enter the properties and specify a primary key.
    • 输入要进行预配的吞吐量(例如,1000 RU)。Enter a throughput to be provisioned (for example, 1000 RUs).
    • 选择“确定” 。Select OK.

    Cassandra API 的屏幕截图,突出显示“添加表”对话框

    备注

    Cassandra API 的主键用作分区键。For Cassandra API, the primary key is used as the partition key.

Gremlin APIGremlin API

  1. 登录到 Azure 门户Sign in to the Azure portal.

  2. 创建新的 Azure Cosmos 帐户或选择现有的帐户。Create a new Azure Cosmos account, or select an existing account.

  3. 打开“数据资源管理器”窗格,然后选择“新建图形” 。Open the Data Explorer pane, and select New Graph. 接下来,请提供以下详细信息:Next, provide the following details:

    • 表明要创建新数据库还是使用现有数据库。Indicate whether you are creating a new database, or using an existing one.
    • 输入图形 ID。Enter a Graph ID.
    • 对于“无限制”存储容量。Select Unlimited storage capacity.
    • 输入顶点的分区键。Enter a partition key for vertices.
    • 输入要进行预配的吞吐量(例如,1000 RU)。Enter a throughput to be provisioned (for example, 1000 RUs).
    • 选择“确定” 。Select OK.

    Gremlin API 的屏幕截图,突出显示“添加图形”对话框

表 APITable API

  1. 登录到 Azure 门户Sign in to the Azure portal.

  2. 创建新的 Azure Cosmos 帐户或选择现有的帐户。Create a new Azure Cosmos account, or select an existing account.

  3. 打开“数据资源管理器”窗格,然后选择“新建表” 。Open the Data Explorer pane, and select New Table. 接下来,请提供以下详细信息:Next, provide the following details:

    • 输入表 ID。Enter a Table ID.
    • 输入要进行预配的吞吐量(例如,1000 RU)。Enter a throughput to be provisioned (for example, 1000 RUs).
    • 选择“确定” 。Select OK.

    表 API 的屏幕截图,突出显示“添加表”对话框

    备注

    就表 API 来说,每次添加新行时,都会指定分区键。For Table API, the partition key is specified each time you add a new row.

使用 Azure CLI 创建容器Create a container using Azure CLI

下面的链接说明如何使用 Azure CLI 为 Azure Cosmos DB 创建容器资源。The links below show how to create container resources for Azure Cosmos DB using Azure CLI.

有关所有 Azure Cosmos DB API 的所有 Azure CLI 示例的列表,请参阅 Azure Cosmos DB 的 Azure CLI 示例For a listing of all Azure CLI samples across all Azure Cosmos DB APIs see, Azure CLI samples for Azure Cosmos DB.

使用 PowerShell 创建容器Create a container using PowerShell

下面的链接说明如何使用 PowerShell 为 Azure Cosmos DB 创建容器资源。The links below show how to create container resources for Azure Cosmos DB using PowerShell.

有关所有 Azure Cosmos DB API 的所有 PowerShell 示例的列表,请参阅 PowerShell 示例For a listing of all PowerShell samples across all Azure Cosmos DB APIs see, PowerShell Samples

使用 .NET SDK 创建容器Create a container using .NET SDK

如果创建集合时遇到超时异常,请执行读取操作来验证是否已成功创建集合。If you encounter timeout exception when creating a collection, do a read operation to validate if the collection was created successfully. 成功完成集合创建操作之前,读取操作将引发异常。The read operation throws an exception until the collection create operation is successful. 有关创建操作所支持的状态代码列表,请参阅 Azure Cosmos DB 的 HTTP 状态代码一文。For the list of status codes supported by the create operation see the HTTP Status Codes for Azure Cosmos DB article.

SQL API 和 Gremlin APISQL API and Gremlin API

// Create a container with a partition key and provision 1000 RU/s throughput.
DocumentCollection myCollection = new DocumentCollection();
myCollection.Id = "myContainerName";
myCollection.PartitionKey.Paths.Add("/myPartitionKey");

await client.CreateDocumentCollectionAsync(
    UriFactory.CreateDatabaseUri("myDatabaseName"),
    myCollection,
    new RequestOptions { OfferThroughput = 1000 });

用于 MongoDB 的 Azure Cosmos DB APIAzure Cosmos DB API for MongoDB

// Create a collection with a partition key by using Mongo Shell:
db.runCommand( { shardCollection: "myDatabase.myCollection", key: { myShardKey: "hashed" } } )

备注

MongoDB 网络协议不能理解请求单位的概念。MongoDB wire protocol does not understand the concept of Request Units. 要创建一个新集合,并在其上提供吞吐量,请使用 Azure 门户或用于 SQL API 的 Cosmos DB SDK。To create a new collection with provisioned throughput on it, use the Azure portal or Cosmos DB SDKs for SQL API.

Cassandra APICassandra API

// Create a Cassandra table with a partition/primary key and provision 1000 RU/s throughput.
session.Execute(CREATE TABLE myKeySpace.myTable(
    user_id int PRIMARY KEY,
    firstName text,
    lastName text) WITH cosmosdb_provisioned_throughput=1000);

后续步骤Next steps