Windows Azure Storage, Part 2

Azure Table Storage

In contrast to Blobs, Azure tables offer structured storage. These “tables”, however, are not to be confused with tables that might exist in the context of a relational database. In fact Azure tables are more like a typical “object database” where each table contains multiple objects, or “entities”. An entity in Azure can be up to 1 MB in size.

Entities are defined in code. For example, consider an “Employee” entity which might exist in an HR application:

/* … */
using Microsoft.WindowsAzure.StorageClient;
namespace MyDemo
{
    public class Employee  :  TableServiceEntity
    {
        public Employee(string partitionKey, string rowKey) : base(partitionKey, rowKey)
        {
        }
        public Employee() : this(“MyCompany”, Guid.NewGuid().ToString())
        {
        }
        public int EmpID { get; set; }
        public string Name { get; set; }
        public string Jobtitle { get; set; }
        public double Salary { get; set; }
    }
}

By inheriting from the TableServiceEntity class (defined in StorageClient) the Employee class gets a partitionKey and a rowKey property. These properties are used to uniquely identify each Employee. In the example above all Employees are stored on the same partition (called “MyCompany”) and the rowKey is a new Guid created for each object instance.

In addition to the definition for the entity it is required to also define a class which specifies the table that holds a collection of those entities. For example a minimal definition might look like this:

/* … */
using Microsoft.WindowsAzure;
using Microsoft.WindowsAzure.StorageClient;
namespace MyDemo
{
    public class EmployeeTable  :  TableServiceContext
    {
        public EmployeeTable(string baseAddress, StorageCredentials credentials) : base(baseAddress, credentials)
        {
        }
    }
}

Where, again, the TableServiceContext base class comes from StorageClient.

So, with those bare essentials in place we could add an entity to our table in a worker role as follows:

/* … */
using Microsoft.WindowsAzure;
using Microsoft.WindowsAzure.StorageClient;
namespace MyDemo
{
    public class WorkerRole : RoleEntryPoint
    {
        public override void Run()
        {
            MySimpleTableMethod();
        }
        private void MySimpleTableMethod()
        {
            var myStorageAccount = CloudStorageAccount.FromConfigurationSetting(“DataConnectionString”);
            myStorageAccount.CreateCloudTableClient().CreateTableIfNotExist(EmployeeTable.EmployeeTableName);
            // create a new Employee entity
            var Emp1 = new Employee();
            Emp1.EmpID = 1;
            Emp1.Name = “Kevin”;
            Emp1.Jobtitle = “Employee”;
            Emp1.Salary = 20000;

            var myEmployees = new EmployeeTable(myStorageAccount.TableEndpoint.ToString(), myStorageAccount.Credentials);
            myEmployees.AddObject( EmployeeTable.EmployeeTableName, Emp1);
            myEmployees.SaveChanges();
        }
        /* … */
    }
}

Obviously there is much more that can be done with tables. For one thing in a “real” implementation we would also add one or more methods to the EmployeeTable class which would allow querying of the table to return a collection of Employees. This example is just meant to serve as a brief, minimalistic overview of how to begin using Azure table storage. The case study exercise in our Windows Azure Course programming gets into much more detail on how, when and why to use tables!

Kevin


Type to search blog.learningtree.com

Do you mean "" ?

Sorry, no results were found for your query.

Please check your spelling and try your search again.