How to Use Azure Table Storage

On this page

Introduction to Azure Table Storage

Azure Table Storage is a NoSQL key-attribute store that can store a large amount of structured, non-relational data. It's ideal for applications that require a flexible schema and rapid development. Table Storage is cost-effective and offers high availability.

Key concepts in Table Storage include:

Prerequisites

Before you begin, ensure you have the following:

  1. An Azure subscription. If you don't have one, create a free account.
  2. A Storage Account. You can create one in the Azure portal.
  3. The Azure SDK for your preferred language (e.g., .NET, Java, Python, Node.js). For this example, we'll use conceptual code snippets.

Creating a Table

Tables are created implicitly when you first store data in them. You don't need to explicitly create a table before adding entities. However, you can check for its existence and create it if it doesn't exist using the SDK.

Example (Conceptual C# SDK)


using Azure.Data.Tables;

// Replace with your actual connection string
string connectionString = "YOUR_STORAGE_ACCOUNT_CONNECTION_STRING";
string tableName = "MySampleTable";

TableServiceClient tableServiceClient = new TableServiceClient(connectionString);
TableClient tableClient = tableServiceClient.GetTableClient(tableName);

// The table will be created automatically when the first entity is added,
// but you can also explicitly create it.
tableClient.CreateIfNotExists();
Console.WriteLine($"Table '{tableName}' created or already exists.");
                

Working with Entities

Entities are the core data units in Table Storage. They are represented as objects or dictionaries containing properties.

Inserting Entities

To insert an entity, you define its properties, including the PartitionKey and RowKey, and then add it to the table.

Example (Conceptual Python SDK)


from azure.data.tables import TableClient

connection_string = "YOUR_STORAGE_ACCOUNT_CONNECTION_STRING"
table_name = "MySampleTable"

table_client = TableClient.from_connection_string(conn_str=connection_string, table_name=table_name)

entity = {
    "PartitionKey": "Users",
    "RowKey": "user123",
    "Name": "Alice Smith",
    "Email": "alice.smith@example.com",
    "Age": 30,
    "IsActive": True
}

table_client.upsert_entity(entity) # upsert_entity creates if not exists, updates if exists
print("Entity inserted or updated.")
                

Querying Entities

You can query entities by specifying filters on properties. Queries are most efficient when they include filters on the PartitionKey.

Example (Conceptual Java SDK)


import com.azure.data.tables.TableClient;
import com.azure.data.tables.TableServiceClient;
import com.azure.data.tables.TableServiceClientBuilder;
import com.azure.data.tables.models.TableEntity;
import com.azure.data.tables.models.TableQueryException;

String connectionString = "YOUR_STORAGE_ACCOUNT_CONNECTION_STRING";
String tableName = "MySampleTable";

TableServiceClient tableServiceClient = new TableServiceClientBuilder()
    .connectionString(connectionString)
    .buildClient();
TableClient tableClient = tableServiceClient.getTableClient(tableName);

// Query for all entities in the "Users" partition
System.out.println("Querying all users:");
for (TableEntity entity : tableClient.listEntities("PartitionKey eq 'Users'")) {
    System.out.println("  " + entity.getProperty("Name") + " (" + entity.getProperty("Email") + ")");
}

// Query for a specific user
System.out.println("\nQuerying for user123:");
try {
    TableEntity specificUser = tableClient.getEntity("Users", "user123");
    System.out.println("  Found: " + specificUser.getProperty("Name"));
} catch (TableQueryException e) {
    System.out.println("  User not found.");
}
                

Updating Entities

You can update existing entities. The upsertEntity operation is often used, which will create the entity if it doesn't exist or update it if it does.

Example (Conceptual Node.js SDK)


const { TableClient } = require("@azure/data-tables");

const connectionString = "YOUR_STORAGE_ACCOUNT_CONNECTION_STRING";
const tableName = "MySampleTable";

const tableClient = TableClient.fromConnectionString(connectionString, tableName);

const updatedEntity = {
    partitionKey: "Users",
    rowKey: "user123",
    Name: "Alice B. Smith", // Updated name
    Age: 31 // Updated age
};

tableClient.updateEntity(updatedEntity, "Merge"); // "Merge" updates properties, "Replace" replaces the whole entity
console.log("Entity updated.");
                

Deleting Entities

Entities can be deleted using their PartitionKey and RowKey.

Example (Conceptual C# SDK)


using Azure.Data.Tables;

// ... (connection and table client setup as before)

string partitionKey = "Users";
string rowKey = "user123";

tableClient.DeleteEntity(partitionKey, rowKey);
Console.WriteLine($"Entity with PartitionKey='{partitionKey}' and RowKey='{rowKey}' deleted.");
                

Understanding Partitions and Rows

The choice of PartitionKey and RowKey significantly impacts performance and scalability. Entities with the same PartitionKey are grouped together, enabling efficient range queries within a partition. The RowKey provides a unique identifier within that partition.

Best Practices:

Note: A single partition has a size limit of 100 GB. Queries that span multiple partitions are less efficient than queries within a single partition.

Conclusion

Azure Table Storage provides a scalable, cost-effective solution for storing large amounts of structured, non-relational data. By understanding the concepts of tables, entities, partitions, and rows, and by applying best practices in designing your keys, you can effectively leverage Table Storage for your applications.

For more advanced scenarios, explore features like: