Azure Cosmos DB Tutorials
Explore a comprehensive set of tutorials to get started with Azure Cosmos DB, Microsoft's globally distributed, multi-model database service. Learn how to create, manage, and scale your NoSQL databases for any application.
Getting Started
Create Your First Cosmos DB Account
A step-by-step guide to provisioning your initial Azure Cosmos DB account and understanding its core concepts.
View TutorialData Modeling with Cosmos DB
Learn best practices for designing efficient data models for your applications using Cosmos DB's various APIs.
View TutorialCore Functionality
CRUD Operations with .NET SDK
Master performing Create, Read, Update, and Delete operations on your Cosmos DB data using the official .NET SDK.
View TutorialQuerying Data with SQL API
Discover how to leverage the powerful SQL query language to retrieve and manipulate data within Cosmos DB.
View TutorialWorking with Change Feed
Understand and implement the Change Feed feature to process ongoing changes to your Cosmos DB data.
View TutorialAdvanced Topics
Global Distribution and High Availability
Configure your Cosmos DB account for global distribution and ensure resilience with multi-region writes.
View TutorialPerformance Optimization
Tips and techniques for optimizing throughput, latency, and cost for your Cosmos DB workloads.
View TutorialIntegrating with Other Azure Services
Connect Cosmos DB with Azure Functions, Azure Stream Analytics, and more for powerful data solutions.
View TutorialCode Examples
Here are some snippets demonstrating common tasks:
Creating a Document (SQL API)
import com.azure.cosmos.CosmosClient;
import com.azure.cosmos.CosmosClientBuilder;
import com.azure.cosmos.CosmosDatabase;
import com.azure.cosmos.models.CosmosItemResponse;
import com.azure.cosmos.models.PartitionKey;
import com.fasterxml.jackson.databind.node.ObjectNode;
public class CreateItem {
public static void main(String[] args) {
String endpoint = "https://YOUR-COSMOS-DB-ACCOUNT.documents.azure.com:443/";
String key = "YOUR-PRIMARY-KEY";
String databaseId = "YOUR-DATABASE-ID";
String containerId = "YOUR-CONTAINER-ID";
try (CosmosClient client = new CosmosClientBuilder()
.endpoint(endpoint)
.key(key)
.buildClient()) {
CosmosDatabase database = client.getDatabase(databaseId);
// Assuming your container has a partition key of "category"
ObjectNode newItem = null;
try {
newItem = client.getCodecContainer().createMapper().createObjectNode();
newItem.put("id", "item-123");
newItem.put("name", "Example Product");
newItem.put("category", "Electronics");
newItem.put("price", 99.99);
} catch (Exception e) {
System.err.println("Error creating JSON object: " + e.getMessage());
return;
}
CosmosItemResponse response = database.getContainer(containerId)
.createItem(newItem, new PartitionKey(newItem.get("category").asText()))
.block();
System.out.println("Created item with ID: " + response.getItem().get("id"));
System.out.println("Request charge: " + response.getRequestCharge());
} catch (Exception e) {
e.printStackTrace();
}
}
}
Querying Items (SQL API)
import com.azure.cosmos.CosmosClient;
import com.azure.cosmos.CosmosClientBuilder;
import com.azure.cosmos.CosmosContainer;
import com.azure.cosmos.CosmosDatabase;
import com.azure.cosmos.models.CosmosQueryRequestOptions;
import com.azure.cosmos.models.FeedResponse;
import java.util.Iterator;
public class QueryItems {
public static void main(String[] args) {
String endpoint = "https://YOUR-COSMOS-DB-ACCOUNT.documents.azure.com:443/";
String key = "YOUR-PRIMARY-KEY";
String databaseId = "YOUR-DATABASE-ID";
String containerId = "YOUR-CONTAINER-ID";
try (CosmosClient client = new CosmosClientBuilder()
.endpoint(endpoint)
.key(key)
.buildClient()) {
CosmosDatabase database = client.getDatabase(databaseId);
CosmosContainer container = database.getContainer(containerId);
String query = "SELECT * FROM root r WHERE r.category = 'Electronics'";
CosmosQueryRequestOptions options = new CosmosQueryRequestOptions();
// options.setPartitionKey(new PartitionKey("Electronics")); // Optional: if partitioning by category
Iterator> iterator = container.queryItems(query, options, Object.class).iterable().iterator();
while (iterator.hasNext()) {
Object item = iterator.next();
System.out.println("Found item: " + item.toString());
}
} catch (Exception e) {
e.printStackTrace();
}
}
}