Azure Storage Tables API
The Azure Storage Tables API enables you to store and query large amounts of structured, non-relational data. It's a NoSQL key-attribute store. Data in a table is a collection of entities, and each entity is a collection of properties. Tables do not enforce a schema, so different entities within the same table can have different sets of properties.
Introduction
Azure Table Storage is a service that stores NoSQL key-attribute data. It's a good choice for storing flexible datasets like user data for web applications, address books, device information, or any other type of metadata that your service requires.
Key concepts:
- Account: An Azure Storage account provides a unique namespace in Azure for your storage data.
- Table: A collection of entities. Tables are schema-less.
- Entity: A record within a table, similar to a row in a database. An entity can have up to 1000 properties.
- Property: A name-value pair within an entity. Property names are strings, and values can be of various primitive data types (e.g., String, Int32, Int64, DateTime, Boolean, GUID, Double, Decimal, Binary).
- PartitionKey and RowKey: Together, these two properties form the clustered index for an entity. They uniquely identify each entity within a table.
Core Operations
The Tables API supports a set of REST operations to manage tables and entities.
Table Operations
These operations allow you to manage tables within your storage account.
| Operation | HTTP Method | Description | URI |
|---|---|---|---|
| Create Table | POST | Creates a new table. | /Tables |
| List Tables | GET | Lists all tables in the storage account. | /Tables |
| Delete Table | DELETE | Deletes a table. | /Tables('TableName') |
| Query Tables | GET | Queries for tables based on a filter. | /Tables?$filter=TableName eq 'MyTable' |
Entity Operations
These operations allow you to manage entities within a specific table.
| Operation | HTTP Method | Description | URI |
|---|---|---|---|
| Insert Entity | POST | Inserts a new entity into a table. | /Entities('TableName') |
| Update Entity | PUT | Updates an existing entity. | /Entities('TableName')(PartitionKey='PK',RowKey='RK') |
| Merge Entity | MERGE | Merges properties of an existing entity. | /Entities('TableName')(PartitionKey='PK',RowKey='RK') |
| Delete Entity | DELETE | Deletes an entity. | /Entities('TableName')(PartitionKey='PK',RowKey='RK') |
| Query Entities | GET | Queries for entities within a table. | /Entities('TableName') |
Entities and Properties
An entity is defined by its properties. The PartitionKey and RowKey are mandatory and form the entity's primary key. Properties can be of various primitive types.
{
"PartitionKey": "Customers",
"RowKey": "ABC123",
"Name": "John Doe",
"Email": "john.doe@example.com",
"Age": 30,
"IsActive": true,
"RegistrationDate": "2023-10-27T10:00:00Z"
}
Querying Entities
You can query entities using OData syntax. Filters can be applied to select specific entities based on property values.
Example Query: Retrieve all active customers in the 'Customers' partition.
GET /Tables('Customers')/Entities?$filter=PartitionKey eq 'Customers' and IsActive eq true
Accept: application/json
DataServiceVersion: 3.0;NetTr:1
MaxDataServiceVersion: 3.0;NetTr:1
Supported Query Operators:
- Comparison:
eq,ne,gt,ge,lt,le - Logical:
and,or,not - Arithmetic:
add,sub,mul,div - String Functions:
substringof,length,concat,tolower,toupper,trim,replace - Date/Time Functions:
year,month,day,hour,minute,second,date,time - Arithmetic Functions:
round,floor,ceiling - Collection Functions:
any,all
Authentication
Access to Azure Storage Tables is secured using Shared Key authorization or Shared Access Signatures (SAS). You can also integrate with Azure Active Directory for token-based authentication.
Batch Operations
Batch operations allow you to send multiple entity operations (insert, update, delete) in a single HTTP request, improving efficiency.
A batch request uses the application/http content type and includes a batch boundary. Each operation within the batch is an independent HTTP request.
Transactions
Azure Table Storage supports entity group transactions. A transaction involves a set of entities that must all succeed or all fail. All entities in a transaction must share the same PartitionKey.