How to Use Azure Table Storage
Azure Table storage is a NoSQL key-attribute store that you can use to store large amounts of structured, non-relational data. It's a cost-effective way to store data that requires flexible schemas. This guide will walk you through the fundamental operations for using Azure Table storage.
Prerequisites
- An Azure Subscription.
- An Azure Storage Account.
- Install the Azure SDK for your preferred language (e.g., .NET, Python, Java, Node.js).
Creating a Table
Before you can store data, you need to create a table. Tables in Azure Table storage are schemaless, meaning you don't define column types upfront. Each entity within a table can have a different set of properties.
Using the Azure SDK (.NET Example)
using Azure.Data.Tables;
string connectionString = "YOUR_AZURE_STORAGE_CONNECTION_STRING";
string tableName = "MyProducts";
TableServiceClient tableServiceClient = new TableServiceClient(connectionString);
TableClient tableClient = await tableServiceClient.CreateTableIfNotExistsAsync(tableName);
Console.WriteLine($"Table '{tableName}' created or already exists.");
Adding an Entity to a Table
An entity is a record in a table, similar to a row in a relational database. Each entity must have two properties: PartitionKey and RowKey. These two properties together uniquely identify an entity.
PartitionKey: Groups entities that share a common partition. All entities in the same partition are stored together.RowKey: Uniquely identifies an entity within a partition.
Using the Azure SDK (.NET Example)
using Azure;
using Azure.Data.Tables;
// Assume tableClient is already created as shown above
var productEntity = new TableEntity("Electronics", "ProductID123")
{
["Name"] = "Laptop",
["Price"] = 1200.50m,
["Category"] = "Computers",
["Stock"] = 50,
["IsAvailable"] = true
};
await tableClient.AddEntityAsync(productEntity);
Console.WriteLine("Entity added successfully.");
Querying Entities
You can retrieve entities from a table using various query methods. You can filter by partition key, row key, or other properties.
Retrieving a Single Entity
using Azure;
using Azure.Data.Tables;
// Assume tableClient is already created
Response<TableEntity> response = await tableClient.GetEntityAsync<TableEntity>("Electronics", "ProductID123");
TableEntity entity = response.Value;
Console.WriteLine($"Name: {entity["Name"]}, Price: {entity["Price"]}");
Retrieving Multiple Entities (Query)
using Azure;
using Azure.Data.Tables;
// Assume tableClient is already created
string filter = "Category eq 'Computers'";
var queryResults = tableClient.QueryAsync<TableEntity>(filter: filter);
await foreach (var page in queryResults.AsPages())
{
foreach (TableEntity entity in page.Values)
{
Console.WriteLine($"PartitionKey: {entity.PartitionKey}, RowKey: {entity.RowKey}, Name: {entity["Name"]}");
}
}
Updating an Entity
You can update existing entities. If the entity does not exist, it can be inserted.
Using the Azure SDK (.NET Example)
using Azure;
using Azure.Data.Tables;
// Assume tableClient is already created and entity exists
var updatedEntity = new TableEntity("Electronics", "ProductID123")
{
["Price"] = 1150.75m,
["Stock"] = 45
};
await tableClient.UpsertEntityAsync(updatedEntity, TableUpdateMode.Replace); // Use Replace to overwrite or Merge to update specific properties
Console.WriteLine("Entity updated successfully.");
Deleting an Entity
You can delete an entity by providing its partition key and row key.
Using the Azure SDK (.NET Example)
using Azure;
using Azure.Data.Tables;
// Assume tableClient is already created
await tableClient.DeleteEntityAsync("Electronics", "ProductID123");
Console.WriteLine("Entity deleted successfully.");
Deleting a Table
You can delete an entire table. This action is irreversible.
Using the Azure SDK (.NET Example)
using Azure;
using Azure.Data.Tables;
// Assume tableClient is already created
await tableServiceClient.DeleteTableAsync(tableName);
Console.WriteLine($"Table '{tableName}' deleted successfully.");
PartitionKey can significantly improve query performance and reduce costs, especially for large datasets.