What is Azure Cosmos DB?
Azure Cosmos DB is a globally distributed, multi-model database service that enables you to create and interact with massively scalable, high-performance data solutions. It offers a choice of APIs, including SQL (Core API), MongoDB, Cassandra, Gremlin, and Table, providing flexibility to choose the best API for your application.
Key features of Azure Cosmos DB include:
- Global Distribution: Distribute your data across any number of Azure regions worldwide.
- Elastic Scalability: Scale throughput and storage independently and elastically.
- Guaranteed Low Latency: Offer sub-millisecond latency for reads and writes globally.
- Multi-Model: Supports various data models and APIs.
- High Availability: Built-in, automatic failover for mission-critical applications.
Key Concepts
Understanding these core concepts is crucial for working with Azure Cosmos DB:
-
Account: Your Cosmos DB resource in Azure. It can contain multiple databases.
Azure Portal -> Create a resource -> Azure Cosmos DB - Database: A logical namespace for your data. A Cosmos DB account can have multiple databases.
- Container: The unit of physical storage and throughput for your data. Containers can store data of different types, but typically store items of the same kind. A container is defined by a name and a partition key.
- Item: The actual data stored within a container. In the SQL API, items are JSON documents.
- Partition Key: A property within your items that determines how your data is distributed across logical partitions. Choosing a good partition key is vital for performance and scalability.
- Request Units (RUs): A measure of throughput. One RU represents the amount of computational work needed to perform a database operation, such as a read or write.
Getting Started with Cosmos DB (SQL API)
Let's walk through the basic steps to create and interact with a Cosmos DB container using the SQL API.
1. Create an Azure Cosmos DB Account
You can create an account via the Azure portal, Azure CLI, or Azure PowerShell.
az cosmosdb create --name <your-cosmosdb-account-name> --resource-group <your-resource-group> --kind GlobalDocumentDB --locations regionName="West US" isZoneRedundant="false" regionName="East US" isZoneRedundant="false"
2. Create a Database and Container
Once the account is created, you can create a database and a container. We'll use a sample JSON document structure for demonstration.
Illustrative view of creating a container in the Azure portal.
Container Configuration:
- Database:
TasksDB - Container:
Items - Partition Key:
/category
Example JSON item:
{
"id": "6b43035e-51b2-44e6-900a-c1a267f57739",
"name": "Cosmos DB Tutorial Item",
"description": "This is a sample item for the tutorial.",
"isComplete": false,
"category": "tutorial",
"tags": ["cosmosdb", "azure", "tutorial"]
}
3. Querying Data
You can query your data using the SQL API, which uses a SQL-like syntax. Here's an example to retrieve items where the category is 'tutorial':
SELECT * FROM c WHERE c.category = "tutorial"
You can execute these queries directly in the Azure portal's Data Explorer, or programmatically using Azure SDKs.
What's Next?
This introduction covered the basics of Azure Cosmos DB. To dive deeper, consider exploring:
- Using different APIs (MongoDB, Cassandra, Gremlin)
- Implementing optimal partition key strategies
- Leveraging Cosmos DB for serverless applications with Azure Functions
- Exploring advanced features like Change Feed and Time-to-Live (TTL)
Refer to the official Azure Cosmos DB documentation for comprehensive details and advanced tutorials.