Azure provides a whole host of options to store your data. The most common one that everyone knows and loves is Azure SQL. But there are other options available such as table storage or blob storage. For now we are just going to focus on the table storage.

What is Table Storage?

Table storage is the Azure NoSQL offering. For those not familiar with NoSQL it is not the same as a traditional relational database. There is no relationship between different elements. I’ve heard it described as kind of like excel. None of the rows are really related to each other in any hard and fast manner, and each row can look very different from every other row. I think that is an excellent explanation.

Why Table Storage?

Simplicity. Sometimes you just need an easy way to store some data, and a full-fledged RDMS is just too much. You can put whatever you want in Azure table storage without any worry that it doesn’t really fit in a table.

How to use it

The first step would to create you storage container in Azure management portal. By selecting New -> Data Services -> Storage -> Quick Create and then enter the name for you storage account. Below you can see I created an account named ‘tothecloud’

Add new storage account

We will need one other thing from the Azure management studio and that is an access key. Think of your storage account name (tothecloud) and the access key as username and password to get at your storage account. Once your storage account is created and you click on the account to go to the dashboard, at the bottom you should see a button to manage access keys. If you click on that button you will get a screen that looks like the following

Manage Access Keys

Normally you would not share these with the world, but by the time you read this I will have deleted and or regenerated the keys. Both of these keys are available to use can be used interchangeably. They give you two in case the first one gets compromised you can substitute the new one, and then regenerate the compromised one. They really don’t recommend regenerating both at the same time so that there is no down time.

And that is pretty much all you have to do from the portal. From here we are going to be switching to our C# code.
First let’s look how to put a record in table storage and then we’ll look at how to get things out.

Adding to table storage

  StorageCredentials creds = new StorageCredentials("tothecloud", "<your storage account key>");
  CloudStorageAccount account  = new CloudStorageAccount(creds, false);

  CloudTableClient tableClient = account.CreateCloudTableClient();
 
  CloudTable table = tableClient.GetTableReference("people");
  table.CreateIfNotExists();
 
  Person p = new Person {
     PersonId = 1,
     Name = "tom",
     Age = 28,
     PartitionKey = "people",
     RowKey = "1"
  };
 
   TableOperation insertOp = TableOperation.Insert(p);
   table.Execute(insertOp);

In this example we are adding to our table storage account a Person object. Our person object isn’t anything fancy except that it implements the ITableEntity interface. Which requires, among other things, PartitionKey and RowKey which are needed by Table storage to find the object you put in there. This must be a unique pair. You cannot have duplicates or Azure Table storage will throw an error. Now let’s take a quick walk through the code.

We start off by declaring some credentials using our storage account name, and our account key that we copied earlier. We then take those creds and create a new CloudStorageAccount, the second parameter denotes whether or not we want to use https. We then, using our account, create a cloud table client, and then get a reference to our table named people. If the table doesn’t exist we go ahead create it. After defining our person object, we create a table operation for the insert and execute it against the table. Very simple. I didn’t define any schema at all in my table storage container.

Now that we have something in there, how do we get it out?

Selecting From Table Storage

   StorageCredentials creds = new StorageCredentials("tothecloud", "<your storage account key>");
   CloudStorageAccount account = new CloudStorageAccount(creds, false);
   CloudTableClient tableClient = account.CreateCloudTableClient();

   CloudTable table = tableClient.GetTableReference("people");
   table.CreateIfNotExists();

   TableQuery<Person> query = new TableQuery<Person>().Where(TableQuery.GenerateFilterCondition("PartitionKey", QueryComparisons.Equal, "people")); 

   IEnumerable<Person> results = table.ExecuteQuery(query);

Just as in the first example we get our credentials, account, and table. After that things get a little interesting. We are creating a TableQuery based on person, and you can use whatever property of person that you want. In this case I want to get everything from the people table. So I’m taking everything where the PartitionKey equals people. After I have my query, I run that against my table and get back an enumerable list of People. From there you can do work with the set as you normally would.

Limitations

As with any solution Azure table storage does have some limitations. This isn’t a full fledged relational database. You can relate items in your container, however there is no hard link. So if you need real foreign keys, then you may want to look at SQL Azure. Along that same line there are no indexes, so some queries can take some time to complete when you have a lot of data in a partition.

Another limitation is that the objects that you are uploading into Azure Table storage have to be flat. You can have an object of simple types, but you cannot have complex types as a property of your object. So in my example above if I had another class named Address. I could not use that inside my person object. So everything has to be flat, which is a bit different from other NoSQL options.

So just like any technology make sure you weigh your pros and cons.

Summary

  • Azure Table Storage is the Azure NoSQL option for cloud storage
  • Azure Table storage is a fast and easy way to get storage for your application
  • There are no foreign keys or indexes in table storage
  • All objects must be flat
  • You can get all the Azure dlls from nuget
  • You can sign up for a Windows Azure account by clicking the link on the right!

There are other APIs available for a lot of other languages other than .Net out there that will allow you to take advantage of Azure table storage and all it has to offer. Many of these APIs simply wrap the REST table storage interface.

This post is also posted at TCSC.com