快速入门:从 Azure 门户创建 Azure Cosmos 帐户、数据库、容器和项Quickstart: Create an Azure Cosmos account, database, container, and items from the Azure portal

Azure Cosmos DB 是世纪互联提供的多区域分布式多模型数据库服务。Azure Cosmos DB is 21Vianet's multiple-regionally distributed multi-model database service. 可以使用 Azure Cosmos DB 快速创建和查询键/值数据库、文档数据库和图形数据库,所有这些资源都可受益于 Azure Cosmos DB 核心的多区域分布和水平缩放功能。You can use Azure Cosmos DB to quickly create and query key/value databases, document databases, and graph databases, all of which benefit from the multiple-region distribution and horizontal scale capabilities at the core of Azure Cosmos DB.

本快速入门演示如何使用 Azure 门户创建 Azure Cosmos DB SQL API 帐户、创建文档数据库和容器,并将数据添加到容器。This quickstart demonstrates how to use the Azure portal to create an Azure Cosmos DB SQL API account, create a document database, and container, and add data to the container.

先决条件Prerequisites

Azure 订阅,或免费的 Azure Cosmos DB 试用帐户An Azure subscription or free Azure Cosmos DB trial account

创建 Azure Cosmos DB 帐户Create an Azure Cosmos DB account

转到 Azure 门户以创建 Azure Cosmos DB 帐户。Go to the Azure portal to create an Azure Cosmos DB account. 搜索“Azure Cosmos DB”,然后选择它。Search for and select Azure Cosmos DB.

Azure 门户“数据库”窗格

  1. 选择 添加Select Add.

  2. 在“创建 Azure Cosmos DB 帐户”页上,输入新 Azure Cosmos 帐户的基本设置。On the Create Azure Cosmos DB Account page, enter the basic settings for the new Azure Cosmos account.

    设置Setting Value 说明Description
    订阅Subscription 订阅名称Subscription name 选择要用于此 Azure Cosmos 帐户的 Azure 订阅。Select the Azure subscription that you want to use for this Azure Cosmos account.
    资源组Resource Group 资源组名称Resource group name 选择一个资源组,或者选择“新建”,然后输入新资源组的唯一名称。Select a resource group, or select Create new, then enter a unique name for the new resource group.
    帐户名Account Name 唯一的名称A unique name 输入标识此 Azure Cosmos 帐户的名称。Enter a name to identify your Azure Cosmos account. 由于 documents.azure.cn 将追加到所提供的名称以创建 URI,因此,请使用唯一的名称。Because documents.azure.cn is appended to the name that you provide to create your URI, use a unique name.

    名称只能包含小写字母、数字和连字符 (-)。The name can only contain lowercase letters, numbers, and the hyphen (-) character. 它的长度必须介于 3 到 31 个字符之间。It must be between 3-31 characters in length.
    APIAPI 要创建的帐户的类型The type of account to create 选择“Core (SQL)”,以便使用 SQL 语法创建文档数据库并进行查询。Select Core (SQL) to create a document database and query by using SQL syntax.

    API 确定要创建的帐户的类型。The API determines the type of account to create. Azure Cosmos DB 提供五种 API:适用于文档数据的 Core (SQL) 和 MongoDB、适用于图形数据的 Gremlin、Azure 表和 Cassandra。Azure Cosmos DB provides five APIs: Core (SQL) and MongoDB for document data, Gremlin for graph data, Azure Table, and Cassandra. 目前,你必须为每种 API 创建单独的帐户。Currently, you must create a separate account for each API.

    详细了解 SQL APILearn more about the SQL API.
    容量模式Capacity mode 预配吞吐量或无服务器Provisioned throughput or Serverless 选择“预配吞吐量”以在预配吞吐量模式下创建帐户。Select Provisioned throughput to create an account in provisioned throughput mode. 选择“无服务器”以在无服务器模式下创建帐户。Select Serverless to create an account in serverless mode.

    注意:无服务器当前仅适用于核心 (SQL) API 帐户。Note: Serverless is currently available for Core (SQL) API accounts only.
    应用免费层折扣Apply Free Tier Discount 应用或不应用Apply or Do not apply 使用 Azure Cosmos DB 免费层,你将在帐户中获得每秒的前 400 RU 免费的吞吐量和 5 GB 的免费存储。With Azure Cosmos DB free tier, you will get the first 400 RU/s and 5 GB of storage for free in an account. 了解免费层的详细信息。Learn more about free tier.
    位置Location 离用户最近的区域The region closest to your users 选择用于托管 Azure Cosmos DB 帐户的地理位置。Select a geographic location to host your Azure Cosmos DB account. 使用离用户最近的位置,使他们能够以最快的速度访问数据。Use the location that is closest to your users to give them the fastest access to the data.
    帐户类型Account Type 生产或非生产Production or Non-Production 如果帐户将用于生产工作负荷,请选择“生产”。Select Production if the account will be used for a production workload. 如果帐户将用于非生产环境(例如开发、测试、QA 或过渡),请选择“非生产”。Select Non-Production if the account will be used for non-production, e.g. development, testing, QA, or staging. 这是一个 Azure 资源标记设置,用于调整门户体验,但不会影响基础 Azure Cosmos DB 帐户。This is an Azure resource tag setting that tunes the Portal experience but does not affect the underlying Azure Cosmos DB account. 可以随时更改此值。You can change this value anytime.
    异地冗余Geo-Redundancy 启用或禁用Enable or Disable 通过将你的区域与某个配对区域进行配对来启用或禁用帐户的多区域分发。Enable or disable multiple-region distribution on your account by pairing your region with a pair region. 稍后可以将更多区域添加到帐户。You can add more regions to your account later.
    多区域写入Multi-region Writes 启用或禁用Enable or Disable 借助多区域写入功能,可以利用全中国的数据库和容器的预配吞吐量。Multi-region writes capability allows you to take advantage of the provisioned throughput for your databases and containers across China.

    备注

    每个 Azure 订阅最多可以有一个免费层 Azure Cosmos DB 帐户,并且你必须在创建帐户时选择加入使用。You can have up to one free tier Azure Cosmos DB account per Azure subscription and must opt-in when creating the account. 如果看不到用于应用免费层折扣的选项,这意味着订阅中的另一个帐户已启用免费层。If you do not see the option to apply the free tier discount, this means another account in the subscription has already been enabled with free tier.

    备注

    如果选择“无服务器”作为“容量模式”,则以下选项不可用 :The following options are not available if you select Serverless as the Capacity mode:

    • 应用免费层折扣Apply Free Tier Discount
    • 异地冗余Geo-redundancy
    • 多区域写入Multi-region Writes

    Azure 门户“数据库”窗格

  3. 选择“查看 + 创建”。Select Review + create. 可以跳过“网络”和“标记”部分 。You can skip the Network and Tags sections.

  4. 检查帐户设置,然后选择“创建”。Review the account settings, and then select Create. 创建帐户需要几分钟时间。It takes a few minutes to create the account. 等待门户页显示“你的部署已完成”消息。Wait for the portal page to display Your deployment is complete.

    Azure 门户“数据库”窗格

  5. 选择“转到资源”,转到 Azure Cosmos DB 帐户页。Select Go to resource to go to the Azure Cosmos DB account page.

    Azure 门户“数据库”窗格

添加数据库和容器Add a database and a container

可以使用 Azure 门户中的数据资源管理器来创建数据库和容器。You can use the Data Explorer in the Azure portal to create a database and container.

  1. 在 Azure Cosmos DB 帐户页上的左侧导航栏中选择“数据资源管理器”,然后选择“新建容器”。 Select Data Explorer from the left navigation on your Azure Cosmos DB account page, and then select New Container.

    可能需要向右滚动才能看到“添加容器”窗口。You may need to scroll right to see the Add Container window.

    Azure 门户“数据库”窗格

  2. 在“添加容器”窗格中,输入新容器的设置。In the Add container pane, enter the settings for the new container.

    设置Setting 建议的值Suggested value 说明Description
    数据库 IDDatabase ID ToDoListToDoList 输入 ToDoList 作为新数据库的名称。Enter ToDoList as the name for the new database. 数据库名称必须包含 1 到 255 个字符,不能包含 /, \\, #, ? 或尾随空格。Database names must contain from 1 through 255 characters, and they cannot contain /, \\, #, ?, or a trailing space. 选中“预配数据库吞吐量”选项,这样就可以在数据库中的所有容器之间共享预配给该数据库的吞吐量。Check the Provision database throughput option, it allows you to share the throughput provisioned to the database across all the containers within the database. 此选项还有助于节省成本。This option also helps with cost savings.
    吞吐量Throughput 400400 将吞吐量保留为每秒 400 个请求单位 (RU/s)。Leave the throughput at 400 request units per second (RU/s). 如果想要减少延迟,以后可以增加吞吐量。If you want to reduce latency, you can scale up the throughput later.

    注意:在无服务器帐户中创建新容器时,此设置不可用。Note: This setting is not available when creating a new container in a serverless account.
    容器 IDContainer ID ItemsItems 输入 Items 作为新容器的名称。Enter Items as the name for your new container. 容器 ID 与数据库名称的字符要求相同。Container IDs have the same character requirements as database names.
    分区键Partition key /category/category 本文中所述的示例使用 /category 作为分区键。The sample described in this article uses /category as the partition key.

    对于本示例,请不要添加“唯一键”。Don't add Unique keys for this example. 使用唯一键可将数据完整性层添加到数据库,因为它能确保每个分区键的一个或多个值的唯一性。Unique keys let you add a layer of data integrity to the database by ensuring the uniqueness of one or more values per partition key. 有关详细信息,请参阅 Azure Cosmos DB 中的唯一键For more information, see Unique keys in Azure Cosmos DB.

  3. 选择“确定”。Select OK. 数据资源管理器将显示新建的数据库和容器。The Data Explorer displays the new database and the container that you created.

将数据添加到数据库Add data to your database

使用数据资源管理器将数据添加到新的数据库。Add data to your new database using Data Explorer.

  1. 在“数据资源管理器”中展开“ToDoList”数据库,然后展开“项”容器。In Data Explorer, expand the ToDoList database, and expand the Items container. 接下来,依次选择“项”、“新建项”。Next, select Items, and then select New Item.

    Azure 门户“数据库”窗格

  2. 在“文档”窗格的右侧,将以下结构添加到文档:Add the following structure to the document on the right side of the Documents pane:

    {
        "id": "1",
        "category": "personal",
        "name": "groceries",
        "description": "Pick up apples and strawberries.",
        "isComplete": false
    }
    
  3. 选择“保存”。Select Save.

    Azure 门户“数据库”窗格

  4. 再次选择“新建文档”,创建并保存具有唯一 id 的另一个文档,以及所需的其他任何属性和值。Select New Document again, and create and save another document with a unique id, and any other properties and values you want. 文档可以采用任何结构,因为 Azure Cosmos DB 不会对数据施加任何架构。Your documents can have any structure, because Azure Cosmos DB doesn't impose any schema on your data.

查询数据Query your data

可以在数据资源管理器中使用查询来检索和筛选数据。You can use queries in Data Explorer to retrieve and filter your data.

  1. 在数据资源管理器的“项”选项卡顶部,查看默认查询 SELECT * FROM cAt the top of the Items tab in Data Explorer, review the default query SELECT * FROM c. 此查询检索并显示容器中按 ID 排序的所有文档。This query retrieves and displays all documents from the container ordered by ID.

    数据资源管理器中的默认查询是“SELECT * FROM c”

  2. 若要更改查询,请选择“编辑筛选器”,将默认查询替换为 ORDER BY c._ts DESC,然后选择“应用筛选器”。To change the query, select Edit Filter, replace the default query with ORDER BY c._ts DESC, and then select Apply Filter.

    添加“ORDER BY c._ts DESC”并单击“应用筛选器”,更改默认查询

    此修改后的查询根据文档的时间戳按降序显示文档,所以现在最先列出的是第二个文档。The modified query displays the documents in descending order based on their time stamp, so now your second document is listed first.

    将查询更改为 ORDER BY c._ts DESC,然后单击“应用筛选器”

如果熟悉 SQL 语法,可以在查询谓词框中输入任何受支持的 SQL 查询If you're familiar with SQL syntax, you can enter any supported SQL queries in the query predicate box. 还可以使用数据资源管理器创建存储过程、UDF 和触发器以执行服务器端业务逻辑。You can also use Data Explorer to create stored procedures, UDFs, and triggers for server-side business logic.

数据资源管理器可以通过 Azure 门户轻松访问 API 中提供的所有内置编程数据访问功能。Data Explorer provides easy Azure portal access to all of the built-in programmatic data access features available in the APIs. 也可通过门户缩放吞吐量、获取密钥和连接字符串,以及查看 Azure Cosmos DB 帐户的指标和 SLA。You also use the portal to scale throughput, get keys and connection strings, and review metrics and SLAs for your Azure Cosmos DB account.

清理资源Clean up resources

执行完应用和 Azure Cosmos DB 帐户的操作以后,可以删除所创建的 Azure 资源,以免产生更多费用。When you're done with your app and Azure Cosmos DB account, you can delete the Azure resources you created so you don't incur more charges. 若要删除资源,请执行以下操作:To delete the resources:

  1. 在 Azure 门户的“搜索”栏中,搜索并选择“资源组” 。In the Azure portal Search bar, search for and select Resource groups.

  2. 从列表中选择为本快速入门创建的资源组。From the list, select the resource group you created for this quickstart.

    选择要删除的资源组

  3. 在资源组“概览”页上,选择“删除资源组” 。On the resource group Overview page, select Delete resource group.

    删除资源组

  4. 在下一窗口中输入要删除的资源组的名称,然后选择“删除” 。In the next window, enter the name of the resource group to delete, and then select Delete.

如果希望仅删除数据库并在将来使用 Azure Cosmos 帐户,则可以通过以下步骤删除数据库:If you wish to delete just the database and use the Azure Cosmos account in future, you can delete the database with the following steps:

  • 转到 Azure Cosmos 帐户。Got to your Azure Cosmos account.
  • 打开数据资源管理器,右键单击要删除的数据库,然后选择“删除数据库”。Open Data Explorer, right click on the database that you want to delete and select Delete Database.
  • 输入数据库 ID/数据库名称以确认删除操作。Enter the Database ID/database name to confirm the delete operation.

后续步骤Next steps

本快速入门介绍了如何创建 Azure Cosmos DB 帐户,以及如何使用数据资源管理器创建数据库和容器。In this quickstart, you learned how to create an Azure Cosmos DB account, create a database and container using the Data Explorer. 现在可以将其他数据导入 Azure Cosmos DB 帐户了。You can now import additional data to your Azure Cosmos DB account.