Getting Started with Azure Cosmos DB
Welcome to the essential guide for beginning your journey with Azure Cosmos DB, a globally distributed, multi-model database service. This article will walk you through the fundamental concepts and steps needed to get your first Cosmos DB database up and running.
What is Azure Cosmos DB?
Azure Cosmos DB is Microsoft's proprietary globally distributed, multi-model database service. It is the first service to offer multi-master writes globally, and guarantees data processing times within milliseconds. Cosmos DB supports various data models, including document, key-value, graph, and column-family.
Key Concepts
- Account: The top-level resource for Cosmos DB. All other resources are created under an account.
- Database: A container for resources like collections and users.
- Container: The unit of scalability for throughput and storage. It holds items and their schema. Common container types include SQL (document) collections, MongoDB collections, Cassandra tables, etc.
- Item: The fundamental unit of data in Cosmos DB, stored within a container. For SQL API containers, items are JSON documents.
- Partition Key: A property within an item that determines its logical partition. Proper partition key selection is crucial for performance and scalability.
- Throughput: Measured in Request Units (RUs) per second, it defines the amount of resources (CPU, memory, IOPS) allocated to your container or database.
Steps to Get Started
1. Create an Azure Cosmos DB Account
You can create a Cosmos DB account through the Azure portal, Azure CLI, or Azure PowerShell.
Using Azure Portal:
- Sign in to the Azure portal.
- Click Create a resource.
- Search for Azure Cosmos DB and select it.
- Click Create.
- Fill in the required details (Subscription, Resource group, Account name, API type, Location). For this guide, we'll select the Core (SQL) API.
- Configure capacity (provisioned throughput or serverless).
- Review and create the account.
2. Create a Database and Container
Once your account is deployed, you'll need to create a database and a container within it.
- Navigate to your Cosmos DB account in the Azure portal.
- In the left-hand navigation, select Data Explorer.
- Click New Container.
- Enter a Database ID (e.g.,
ToDoList) and a Container ID (e.g.,Items). - Crucially, select a Partition Key path. For example,
/categoryor/idif each item has a unique identifier. - Configure the throughput for your container (e.g., 400 RU/s).
- Click OK.
3. Add Data
You can add data using the Data Explorer in the Azure portal or by writing code.
Using Data Explorer:
- In Data Explorer, navigate to your container (e.g.,
Items). - Click New Item.
- Paste or write your JSON document. For example:
{ "id": "f04b46a3-0000-0000-0000-000000000001", "category": "personal", "name": "Groceries", "description": "Buy milk and eggs", "isComplete": false } - Click Save.
id field, which is mandatory for SQL API containers.
4. Query Data
Querying data is done using SQL-like syntax through the Data Explorer or your application code.
Example Query in Data Explorer:
SELECT * FROM c WHERE c.category = "personal"
Next Steps
- Explore the different APIs available in Cosmos DB (SQL, MongoDB, Cassandra, Gremlin, Table).
- Learn about optimal partition key strategies for your workload.
- Integrate Cosmos DB into your applications using the SDKs for your preferred language (.NET, Java, Node.js, Python, etc.).
- Understand pricing and cost optimization strategies.