Introduction to Azure Cosmos DB Table API
The Azure Cosmos DB Table API is a NoSQL key-value store that is part of the Azure Cosmos DB family of services. It provides a highly scalable, globally distributed, and low-latency data storage solution for applications that require a simple, flexible schema.
The Table API is designed for applications that need to store large amounts of structured, non-relational data. It's particularly well-suited for scenarios such as:
- Storing user profiles and preferences.
- Managing game state and leaderboards.
- Cataloging product information.
- Tracking IoT device data.
The Table API is compatible with the Azure Table storage service, allowing you to migrate existing applications with minimal changes. It offers enhanced features over standard Table storage, including global distribution, multiple consistency models, and dedicated throughput.
Getting Started with Table API
To start using the Table API, you'll need an Azure Cosmos DB account. You can create one through the Azure portal.
- Create an Azure Cosmos DB account with the Table API.
- Select the desired region for your account.
- Obtain your connection string from the "Keys" section of your Cosmos DB account in the Azure portal.
You can then use one of the available SDKs to interact with your data.
Example using Azure Cosmos DB SDK for .NET:
using Azure.Data.Tables;
string connectionString = "";
string tableName = "MyTable";
TableServiceClient tableServiceClient = new TableServiceClient(connectionString);
TableClient tableClient = await tableServiceClient.CreateTableIfNotExistsAsync(tableName);
// Add an entity
var entity = new { PartitionKey = "User", RowKey = "Alice", Email = "alice@example.com", Age = 30 };
await tableClient.UpsertEntityAsync(entity);
// Query entities
var queryResults = tableClient.Query<TableEntity>(filter: "PartitionKey eq \"User\"");
foreach (var item in queryResults)
{
Console.WriteLine($"User: {item.RowKey}, Email: {item.GetString("Email")}, Age: {item.GetInt32("Age")}");
}
Data Modeling in Table API
Data in the Table API is organized into tables, which are collections of entities. Each entity is a set of properties. Every entity must have a PartitionKey
and a RowKey
, which together form the primary key and uniquely identify an entity within a table.
- PartitionKey: Used to group entities. Entities with the same PartitionKey are stored together, which can improve query performance.
- RowKey: Used to uniquely identify an entity within a partition.
Properties can be of various data types, including string, integer, boolean, GUID, datetime, and double.
Example Entity:
Property Name | Data Type | Description |
---|---|---|
PartitionKey | String | e.g., "Customers" |
RowKey | String | e.g., "C1001" |
Name | String | e.g., "John Doe" |
String | e.g., "john.doe@example.com" | |
OrderCount | Int32 | e.g., 5 |
Querying Data
The Table API supports OData-style querying for efficient data retrieval. You can filter, project, and sort entities.
Filtering:
Use the $filter
query option to specify conditions.
GET /MyTable()?$filter=PartitionKey eq 'Customers' and OrderCount gt 10
Projection:
Use the $select
query option to retrieve only specific properties.
GET /MyTable()?$filter=PartitionKey eq 'Customers'&$select=RowKey,Email
Sorting:
Use the $orderby
query option to sort results.
GET /MyTable()?$filter=PartitionKey eq 'Customers'&$orderby=Name desc
Common Operations
- Create Table: Creates a new table.
- Delete Table: Deletes an existing table.
- Insert Entity: Adds a new entity to a table.
- Update Entity: Modifies an existing entity.
- Delete Entity: Removes an entity.
- Query Entities: Retrieves entities based on specified criteria.
SDKs
Azure Cosmos DB provides SDKs for various programming languages, including:
- .NET
- Java
- Python
- Node.js
- Go
- and more.
These SDKs simplify the process of interacting with the Table API, handling connection management, authentication, and request serialization.
REST API
You can also interact with the Table API directly using its REST API. This is useful for integration with systems that do not have dedicated SDKs or for performing management operations.
The base URI for the Table API is:
https://<your-cosmosdb-account-name>.table.cosmos.azure.com
All requests must be authenticated using Shared Key authentication or Azure Active Directory.
Pricing
Azure Cosmos DB pricing is based on throughput (Request Units per second) and storage consumed. The Table API offers different pricing models, including:
- Provisioned Throughput: Guaranteed performance for your application.
- Autoscale Throughput: Automatically scales throughput based on demand.
- Serverless: Pay-per-request model, ideal for intermittent workloads.
For detailed information, refer to the Azure Cosmos DB pricing page.