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:
- Global Distribution: Replicate your data across multiple Azure regions for high availability and low latency.
- Multi-Model: Supports various data models including document, key-value, graph, and column-family through different APIs (SQL API, MongoDB API, Cassandra API, Gremlin API, Table API).
- Scalability: Independently scale throughput and storage to meet your application's demands.
- Throughput (RU/s): Request Units per second (RU/s) are a logical representation of database throughput.
- Consistency Levels: Choose from five well-defined consistency levels to balance availability and performance.
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:
- Sign in to the Azure portal.
- In the Azure portal search bar, enter "Azure Cosmos DB" and select it.
- Click Create.
- On the Create an Azure Cosmos DB account page, select your desired API (e.g., Core (SQL) API for document data).
- Fill in the required fields, including subscription, resource group, account name, and location.
- Configure capacity mode (Autoscale or Manual) and provisioned throughput. For getting started, manual is often simpler.
- 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
- Navigate to your Azure Cosmos DB account resource.
- In the left-hand menu, select Data Explorer.
- Click New Container.
- 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
/idor a custom property like/userId. - Configure throughput for the container or inherit from the database.
- 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:
- Data Modeling Best Practices
- Using the Azure Cosmos DB SDKs for your preferred programming language.
- Understanding and configuring different APIs.
- Exploring advanced features like Change Feed and Stored Procedures.