Azure Table Storage

Using Azure Table Storage

Introduction

Table Storage is a key-value store that uses a partition key to help with scale out distribution of data and a row key for unique access to a particular entry within a partition. Together, these keys are used to uniquely identify a record in the account.

Accessing the account

First, we will need to add the following to our configuration file (app.config if it’s a console application, web.config if it’s a web application):

Then, we will access our account with the following code:

Creating a Table

To create a Table, we can use the CreateIfNotExists function, as shown in the sample below:

Inserting records

To insert a record we will first need a class that inherits from TableEntity:

Then, we create a new OrderEntity, fill the values, and execute an Insert operation on the Table:

Inserting multiple records

We can also insert multiple records in a batch transaction, with the following limitations:

  • All operations must take place on the same partition.
  • The batch can include up to 100 operations.
  • The batch payload cannot be greater than 4 MB.

Updating records

To update a record, we first get the record, then we can create an InsertOrReplace operation:

Deleting a record

To delete a record, we must first retrieve the record (as shown in the previous code sample) and the create a Delete operation:

Querying Azure Table Storage

In the first versions of the Azure Storage library (versions 1.x), the experience was much like that of an Entity Repository, in the sense that we had a Context and lots of LINQ calls. Although it was easy to understand, it was really difficult to determine exactly when the query was done to the service. And it was pretty upsetting to be limited only to “Where”, “First” and “Select” LINQ functions.

Since version 2.0, we have a TableQuery object available, which gives us more control over our query, and we can better understand what’s happening.

Querying for an entity

To query for an entity, we will need the partition key and the row key.

Applying Filters

Table Storage supports OData, which allows us to make some basic querying: filter, top, select, orderby and a few others. The Storage Client library exposes then through the TableQuery class.

This allows us, for example, to get all records in a partition:

There are two important elements here: GenerateFilterCondition and QueryComparisons.

There are some variations to the GenerateFilterCondition function, according to the data type, being this one the one used for strings.

GenerateFilterConditionForGuid
GenerateFilterConditionForLong
GenerateFilterConditionForDouble
GenerateFilterConditionForInt
GenerateFilterConditionForDate
GenerateFilterConditionForBool
GenerateFilterConditionForBinary

And the available options for QueryComparisons are:

QueryComparisons.Equal
QueryComparisons.NotEqual
QueryComparisons.GreaterThan
QueryComparisons.LessThan
QueryComparisons.GreaterThanOrEqual
QueryComparisons.LessThanOrEqual

If we need to combine different filters, we use the TableQuery.CombineFilters() function:

With the operators being:

TableOperators.And
TableOperators.Or
TableOperators.Not

Selects

The Select function allows us to query only for the fields we need. This function can be really useful in the cases when we need to save network bandwidth, like when we are building a WinRT app.

For example, if we want to get only the names of our customers:

Take

If we need to limit the amount of results, we just use the Take function:

Combining all

Of course, we can combine all these functions together:

Strongly typed queries

All the previous example functions return dynamic values, but we can also make queries that return the class we need, by using TableQuery<OrderEntity>.

Happy coding!

@gjbellmann

Advertisements

Leave a comment

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s