Getting Started with Azure Cosmos DB

Welcome to the introductory guide for Azure Cosmos DB! This tutorial will walk you through the fundamental steps to set up and start using Azure Cosmos DB, a globally distributed, multi-model database service. Whether you're building a new application or migrating an existing one, Cosmos DB offers flexibility and scale for your data needs.

1. Understand Azure Cosmos DB Core Concepts

Before diving into the setup, familiarize yourself with these key concepts:

2. Create an Azure Cosmos DB Account

The first step is to provision an Azure Cosmos DB account in the Azure portal. Follow these steps:

  1. Sign in to the Azure portal.
  2. In the Azure portal search bar, enter "Azure Cosmos DB" and select it.
  3. Click Create.
  4. On the Create an Azure Cosmos DB account page, select your desired API (e.g., Core (SQL) API for document data).
  5. Fill in the required fields, including subscription, resource group, account name, and location.
  6. Configure capacity mode (Autoscale or Manual) and provisioned throughput. For getting started, manual is often simpler.
  7. Review and create the account.

Tip

For quick testing, you can use the "Free Tier" option which provides a limited amount of free throughput and storage for the lifetime of the account.

3. Create a Database and Container

Once your Cosmos DB account is deployed, you need to create a database and a container (equivalent to a table or collection) to store your data.

3.1. Using the Azure Portal

  1. Navigate to your Azure Cosmos DB account resource.
  2. In the left-hand menu, select Data Explorer.
  3. Click New Container.
  4. Provide a Database ID, Container ID, and a Partition key. The partition key is crucial for performance and scalability. For document databases, a common choice is /id or a custom property like /userId.
  5. Configure throughput for the container or inherit from the database.
  6. Click OK.

3.2. Using Azure CLI (Example)

You can also use Azure CLI to automate resource creation:


az cosmosdb create --name mycosmosdbaccount --resource-group myresourcegroup --kind GlobalDocumentDB
az cosmosdb sql database create --account-name mycosmosdbaccount --resource-group myresourcegroup --name mydatabase
az cosmosdb sql container create --account-name mycosmosdbaccount --resource-group myresourcegroup --database-name mydatabase --name mycontainer --partition-key-path "/id"
        

4. Connect to Your Cosmos DB Account

To interact with your data, your applications will need to connect to your Cosmos DB account using connection strings or SDKs.

You can find your primary connection string in the Azure portal under your Cosmos DB account's Keys section.

Important

Treat your primary connection key as a password. Do not embed it directly in client-side code.

5. Add and Query Data

With your container set up, you can start adding data and performing queries.

5.1. Adding Data (SQL API Example)

Here's a simple JSON document you might add to a container:


{
    "id": "a1b2c3d4-e5f6-7890-1234-567890abcdef",
    "name": "Azure Cosmos DB",
    "category": "Database Service",
    "tags": ["NoSQL", "Globally Distributed", "PaaS"],
    "description": "A fully managed, globally distributed, multi-model database service."
}
        

5.2. Querying Data (SQL API Example)

You can query your data using SQL-like syntax directly in Data Explorer or via SDKs:


SELECT * FROM c WHERE c.category = "Database Service"
        

Next Steps

Congratulations! You've successfully set up and started interacting with Azure Cosmos DB. To further enhance your understanding and application development, consider exploring:

« Previous: Architecture Next: Data Modeling »