Azure Cosmos DB: Getting Started
Welcome to this introductory tutorial on Azure Cosmos DB! This guide will walk you through the essential steps to get started with Microsoft's globally distributed, multi-model database service.
What is Azure Cosmos DB?
Azure Cosmos DB is a fully managed, globally distributed database service that enables you to create highly available, low-latency applications. It supports multiple data models, including document, key-value, graph, and column-family. This tutorial will focus on the document model using the SQL API.
Prerequisites
- An active Azure subscription. If you don't have one, you can create a free account.
- A web browser (e.g., Edge, Chrome, Firefox).
Step 1: Create an Azure Cosmos DB Account
First, you need to create an Azure Cosmos DB account in the Azure portal. This account will serve as the logical container for your databases, tables, containers, and items.
- Sign in to the Azure portal.
- Click Create a resource in the top-left corner.
- Search for Azure Cosmos DB and select it.
- Click Create.
- On the Basics tab:
- Subscription: Select your Azure subscription.
- Resource group: Create a new one (e.g., `rg-cosmos-demo`) or select an existing one.
- Account Name: Enter a unique name for your Cosmos DB account (e.g., `mycosmosdbaccount12345`).
- API: Select Core (SQL).
- Location: Choose a region close to you.
- Review and click Create. Deployment may take a few minutes.
Step 2: Create a Database and Container
Once your account is deployed, you'll create a database and a container within that account. A database is a logical namespace for your data, and a container holds the actual data as a collection of items (documents).
- Navigate to your newly created Azure Cosmos DB account in the Azure portal.
- In the left-hand navigation menu, under Data Explorer, click Data Explorer.
- Click New Container.
- In the Add Container form:
- Database ID: Click Create new and enter a name for your database (e.g., `ToDoList`).
- Container ID: Enter a name for your container (e.g., `Items`).
- Partition Key: Enter a path for your partition key (e.g., `/category`). This is crucial for performance and scalability.
- Click OK.
Step 3: Add Items to Your Container
Now you can start adding items (documents) to your container. For this example, we'll add a simple JSON document representing a to-do item.
- In Data Explorer, expand your database and select your container (`Items`).
- Click New Item.
- In the JSON editor, paste the following content:
{ "id": "6a3b5b5a-5b5a-5b5a-5b5a-5b5a5b5a5b5a", "category": "work", "description": "Buy groceries", "isComplete": false, "details": { "priority": "high" } } - Click Save.
Step 4: Query Your Data
Azure Cosmos DB supports rich querying using the SQL API. Let's query for items in the 'work' category.
- With your container selected in Data Explorer, click the Explore items button or navigate to the Items tab.
- Click New SQL Query.
- Enter the following SQL query:
SELECT * FROM c WHERE c.category = "work" - Click Execute. You should see the item you just created.
Next Steps
Congratulations! You've successfully created an Azure Cosmos DB account, database, container, added an item, and queried your data. From here, you can:
- Explore different APIs (MongoDB, Cassandra, Gremlin, Table).
- Integrate with Azure Functions or other backend services.
- Learn about partitioning strategies for optimal performance.
- Implement global distribution for high availability.