Azure Cosmos DB Tutorial
Introduction to Azure Cosmos DB
Azure Cosmos DB is a globally distributed, multi-model database service designed to provide high availability, low latency, and scalability. It supports multiple data models such as document, key-value, graph, and column-family. This tutorial will guide you through the basics of Azure Cosmos DB, from setting up your account to performing CRUD operations.
Setting Up Azure Cosmos DB
To get started with Azure Cosmos DB, you need an Azure account. If you don't have one, you can sign up for a free account on the Azure website.
Step 1: Create a Cosmos DB Account
1. Navigate to the Azure portal (portal.azure.com).
2. Click on "Create a resource" and search for "Azure Cosmos DB".
3. Click on "Create" and fill in the required information:
- Subscription: Select your Azure subscription.
- Resource Group: Create a new resource group or select an existing one.
- Account Name: Enter a unique name for your Cosmos DB account.
- API: Select the API you want to use (e.g., Core (SQL), MongoDB, Cassandra, Azure Table, Gremlin).
- Location: Choose a region for your database.
4. Click on "Review + create" and then "Create" to provision your Cosmos DB account.
Creating a Database and Container
Step 2: Create a Database
1. In the Azure portal, navigate to your Cosmos DB account.
2. Click on "Data Explorer" and then "New Database".
3. Enter a name for your database and click "OK".
Step 3: Create a Container
1. In the Data Explorer, click on "New Container".
2. Provide the following information:
- Database ID: Select the database you created.
- Container ID: Enter a name for your container.
- Partition Key: Define a partition key (e.g., /id).
3. Click "OK" to create the container.
Performing CRUD Operations
Create Document
You can create a new document in the container using the Data Explorer or SDKs. Here is an example using the SQL API in the Data Explorer:
1. Click on "Items" under your container.
2. Click "New Item" and enter the JSON data for the document:
{ "id": "1", "name": "John Doe", "email": "john.doe@example.com" }
3. Click "Save" to create the document.
Read Document
You can read documents using queries in the Data Explorer or SDKs. For example, to read all documents:
[ { "id": "1", "name": "John Doe", "email": "john.doe@example.com" } ]
Update Document
To update a document, you can use the Data Explorer or SDKs. Here is an example of updating a document using the Data Explorer:
1. Click on the document you want to update.
2. Modify the JSON data:
{ "id": "1", "name": "John Doe", "email": "john.updated@example.com" }
3. Click "Save" to update the document.
Delete Document
To delete a document, you can use the Data Explorer or SDKs. Here is an example of deleting a document using the Data Explorer:
1. Click on the document you want to delete.
2. Click "Delete" and confirm the deletion.
Using SDKs
Azure Cosmos DB provides SDKs for various programming languages such as .NET, Java, Python, and Node.js. Here is an example of using the .NET SDK to perform CRUD operations:
Install the SDK
1. Open your project in Visual Studio.
2. Install the Azure Cosmos DB SDK:
Install-Package Microsoft.Azure.Cosmos
Using the SDK
Here is an example code snippet to perform CRUD operations using the .NET SDK:
using Microsoft.Azure.Cosmos; using System; using System.Threading.Tasks; public class Program { private static readonly string EndpointUrl = "https://your-account.documents.azure.com:443/"; private static readonly string PrimaryKey = "your-primary-key"; private CosmosClient cosmosClient; private Database database; private Container container; public static async Task Main(string[] args) { Program p = new Program(); await p.GetStartedAsync(); } private async Task GetStartedAsync() { this.cosmosClient = new CosmosClient(EndpointUrl, PrimaryKey); await this.CreateDatabaseAsync(); await this.CreateContainerAsync(); await this.AddItemsToContainerAsync(); } private async Task CreateDatabaseAsync() { this.database = await this.cosmosClient.CreateDatabaseIfNotExistsAsync("DemoDatabase"); Console.WriteLine("Created Database: {0}\n", this.database.Id); } private async Task CreateContainerAsync() { this.container = await this.database.CreateContainerIfNotExistsAsync("DemoContainer", "/id"); Console.WriteLine("Created Container: {0}\n", this.container.Id); } private async Task AddItemsToContainerAsync() { dynamic testItem = new { id = "1", name = "John Doe", email = "john.doe@example.com" }; ItemResponseresponse = await this.container.CreateItemAsync(testItem, new PartitionKey(testItem.id)); Console.WriteLine("Created Item: {0}\n", response.Resource); } }