快速入門:從 Azure 入口網站建立 Azure Cosmos 帳戶、資料庫、容器和項目Quickstart: Create an Azure Cosmos account, database, container, and items from the Azure portal

適用於: SQL API

Azure Cosmos DB 是 Microsoft 的全域分散式多模型資料庫服務。Azure Cosmos DB is Microsoft's globally 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 global 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.com 會附加到您所提供的名稱以建立 URI,請使用唯一名稱。Because documents.azure.com 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.
    申請 Azure Cosmos DB 免費階層折扣Apply Azure Cosmos DB 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.
    LocationLocation 最接近使用者的區域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 global 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 the globe.
    可用性區域Availability Zones 啟用或停用Enable or Disable 可用性區域可協助您進一步改善應用程式的可用性和復原能力。Availability Zones help you further improve availability and resiliency of your application.

注意

每個 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 Cosmos DB 的新帳戶頁面

  1. 選取 [檢閱 + 建立]。Select Review + create. 您可以略過 [網路] 和 [標記] 區段。You can skip the Network and Tags sections.

  2. 檢閱帳戶設定,然後選取 [建立]。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 入口網站的 [通知] 窗格

  3. 選取 [移至資源] 以移至 Azure Cosmos DB 帳戶頁面。Select Go to resource to go to the Azure Cosmos DB account page.

    Azure Cosmos DB 帳戶頁面

新增資料庫和容器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
    資料庫識別碼Database 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/秒)。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.
    容器識別碼Container ID 項目Items 輸入 Items 作為新容器的名稱。Enter Items as the name for your new container. 容器識別碼與資料庫名稱具有相同的字元需求。Container IDs have the same character requirements as database names.
    分割區索引鍵Partition key /類別/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 資料庫,然後展開 Items 容器。In Data Explorer, expand the ToDoList database, and expand the Items container. 接下來,選取 [Items],然後選取 [新增項目]。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.

    將 json 資料複製在 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. 此查詢會依照識別碼順序擷取並顯示容器中的所有文件。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.
  • 輸入資料庫識別碼/資料庫名稱,以確認刪除作業。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.