Table Storage
Azure Table Storage is a NoSQL key-value store that allows you to store large amounts of structured, non-relational data. It's designed for high availability and massive scalability, making it ideal for scenarios like IoT data, user profiles, and application logs.
Unlike relational databases, Table Storage doesn't enforce a rigid schema. Each table can store entities with different properties, offering flexibility in your data modeling.
Key Concepts
Tables
A table is a collection of entities. You can think of it as a container for your data. Tables are schemaless, meaning entities within the same table can have different properties.
Entities
An entity is a record in a table, similar to a row in a relational database. An entity can have up to 1000 properties.
Properties
A property is a name-value pair within an entity. Each property has a name (string) and a value (supported data type). Property names are case-insensitive and can be up to 255 characters long.
PartitionKey and RowKey
Every entity must have two designated properties: PartitionKey and RowKey. These two properties together uniquely identify an entity within a table. Together, they form the entity's primary key.
PartitionKey: Groups entities that share the same partition. This is crucial for performance and scalability, as Azure Storage partitions data based on thePartitionKey.RowKey: Uniquely identifies an entity within a partition.
// Example Entity Structure
{
"PartitionKey": "Users",
"RowKey": "user123",
"Email": "user123@example.com",
"DisplayName": "Alice Wonderland",
"Timestamp": "2023-10-27T10:00:00Z"
}
Supported Data Types
Table Storage supports several data types for properties, including:
- String
- Boolean
- DateTime
- Double
- GUID
- Int32
- Int64
- Binary (byte array)
- DateTimeOffset
Getting Started
To start using Azure Table Storage, you'll need an Azure account and an Azure Storage account. You can interact with Table Storage using:
- Azure Portal
- Azure CLI
- Azure PowerShell
- Azure SDKs (e.g., .NET, Java, Python, Node.js)
- Azure Storage Explorer
Here's a quick example using the Azure SDK for .NET:
using Azure.Data.Tables;
// Replace with your connection string
string connectionString = "YOUR_AZURE_STORAGE_CONNECTION_STRING";
string tableName = "MySampleTable";
// Create a TableServiceClient using a connection string.
TableServiceClient tableServiceClient = new TableServiceClient(connectionString);
// Create a TableClient using the service client and table name.
TableClient tableClient = tableServiceClient.GetTableClient(tableName);
// Create the table if it doesn't exist.
tableClient.CreateIfNotExists();
// Define an entity.
var entity = new TableEntity("PartitionA", "Row1")
{
{"Email", "user1@example.com"},
{"Age", 30}
};
// Add the entity to the table.
tableClient.AddEntity(entity);
Console.WriteLine("Entity added successfully.");
Common Operations
Creating a Table
Tables are created on-demand when you first add data to them. You can explicitly create a table using the SDKs or Azure Portal.
// Example using Azure SDK
tableClient.CreateIfNotExists();
Inserting an Entity
Entities are inserted into a table using their PartitionKey and RowKey.
// Example using Azure SDK
var entity = new TableEntity("PartitionA", "Row2")
{
{"Name", "Bob"},
{"City", "New York"}
};
tableClient.AddEntity(entity);
Querying Entities
You can query entities by PartitionKey, RowKey, or with filter expressions.
// Query entities in PartitionA
var queryResults = tableClient.Query<TableEntity>(filter: $"PartitionKey eq 'PartitionA'");
foreach (var entity in queryResults)
{
Console.WriteLine($"{entity.RowKey}: {entity["Name"]}");
}
// Query for a specific entity
var singleEntity = tableClient.GetEntity<TableEntity>("PartitionA", "Row1");
if (singleEntity.HasValue)
{
Console.WriteLine($"Found: {singleEntity.Value.RowKey}");
}
Updating an Entity
You can update existing entities. If the entity doesn't exist, it will be inserted.
// Example using Azure SDK (upsert)
var entityToUpdate = new TableEntity("PartitionA", "Row1")
{
{"Email", "alice.updated@example.com"},
{"Age", 31}
};
tableClient.UpsertEntity(entityToUpdate);
Deleting an Entity
Delete an entity by providing its PartitionKey and RowKey.
// Example using Azure SDK
tableClient.DeleteEntity("PartitionA", "Row2");
Deleting a Table
You can delete an entire table and all its data.
// Example using Azure SDK
tableClient.Delete();
SDKs and Tools
Leverage the official Azure SDKs to integrate Table Storage into your applications:
For managing your storage accounts and data, consider using:
- Azure Storage Explorer (a free GUI tool)
- Azure Portal
- Azure CLI
Performance & Scalability
Table Storage offers excellent performance for large datasets. Key factors influencing performance include:
- Partitioning: Distributing data across partitions efficiently using
PartitionKeyis crucial. Aim for partitions with similar amounts of data. - Query Efficiency: Queries that filter on
PartitionKeyandRowKeyare the most efficient. - Batch Operations: Use batch transactions for performing multiple entity operations within a single request.
Table Storage scales automatically to handle massive amounts of data and traffic.
Pricing
Azure Table Storage pricing is based on:
- Data Stored: The amount of data you store in gigabytes (GB).
- Transactions: The number of read, write, and delete operations you perform.
- Data Transfer: Data transfer in and out of Azure.
For detailed pricing information, please refer to the official Azure Storage pricing page.
Tutorials
Explore these tutorials to get hands-on experience with Azure Table Storage: