Posts Tagged 'Azure storage tables'

Windows Azure Training Series: Creating an Azure Storage Account

In this blog post, I assume you already have an Azure subscription and a user account set up. If you do not, check out Windows Azure Training Series:  Understanding Subscriptions and Users. You might also like to read Windows Azure Training Series: Understanding Azure Storage for an overview of Azure storage.

Creating a new Azure storage account

To use Azure storage, you need to create a storage account. To do this, first go to the Windows Azure Platform Manage Portal and log in. Then, click on the New Storage Account button at the top left corner of the portal as shown below.

You will be prompted to enter a unique URL (or account name) for this storage account. Azure storage is always accessed using a URL based on this name, plus the type of storage you’re accessing, plus the “core.windows.net” domain. So, using the name shown below, accessing blob storage would be done using the URL, http://myuniqueaccountname.blob.core.windows.net/. To access table storage you would use the URL, http://myuniqueaccountname.table.core.windows.net/.

After entering your URL, choose a region where your storage account will be located and then click create.

Azure Storage account properties

After the account is created you can view its properties in the Properties grid on the right side of the management portal. The most important property is the primary access key. To access it, click on the View button.

The window shown below will open; it allows you to view and copy the access keys to the clipboard. You will need to add the access key to the configuration file of your Azure application.

In Visual Studio, open the ServiceDefinition.csdef file in your Azure service project, and add the configuration setting shown below.

Also, open the ServiceConfiguration.cscfg file, and set the value of that setting as shown below. Of course, you’ll use your own account name and account key.

Finally, you’ll need to add the following code to your WebRole.cs file.

Tune in next time and we’ll take a look at the code required to write to blob storage.

Doug Rehnstrom

To learn more about Windows Azure, check out Learning Tree’s course, Windows Azure Platform Introduction: Programming Cloud-Based Applications.

Windows Azure Training Series: Understanding Azure Storage

Windows Azure Storage Choices

There are a couple common ways of storing data when using Microsoft Windows Azure. One is SQL Azure, which is a cloud-based version of Microsoft SQL Server. The other is Azure storage. SQL Azure will be familiar to those who already understand relational databases. It may also make moving an application to cloud easier, if that application already uses SQL Server.

Azure storage has some advantages as well. First it is inexpensive. Azure storage costs about 15 cents per gigabytes per month, compared to $10 per gigabyte per month for SQL Azure. It can also be very large. Depending on your instance size, it can be up to 2 terabytes. It is also cross-platform, and accessed using standard internet requests.

Types of Windows Azure Storage

There are four types of Azure storage: blob storage, table storage, queue storage and Azure drives.

Blob storage is used to store binary data. This could be pictures, videos, or any other binary data.

Table storage is used to store structured data. It is similar to a database, but not relational. Table storage is a convenient way of saving business entities in an object-oriented program. In many ways it is simpler than relational storage.

Queue storage provides a simple messaging system that allows different Azure roles to communicate. For example, a user may request a report to be run using an application running in a Web role. That request could be sent to an Azure queue. Later a worker role can process the request, and then email the completed report to the user.

Azure drives allow storage to be access using standard NTFS APIs. This could be particularly useful if you have an application that already writes to a hard disk and you want to migrate it to the cloud.

Accessing Azure Storage

Azure storage can be access using a REST-based API via HTTP. This means storage can be used from any application, whether it is written in .NET, Java, Python, JavaScript or something else.

If you’re using .NET though, accessing storage is made easier using the Azure SDK. If you don’t already have it, go to this link, http://www.microsoft.com/windowsazure/windowsazure/, and then click on the “Get tools and SDK” button. You might also like to read this article, Windows Azure Training Series – Setting up a Development Environment for Free.

Once you have the SDK installed, set a reference to Microsoft.WindowsAzure.StorageClient.dll, and you’re ready to go.

In later posts, we’ll take a look at some code to write to Azure storage. In the meantime, you might like to read the prior posts in this series.

Windows Azure Training Series – Understanding Subscriptions and Users

Windows Azure Training Series – Setting up a Development Environment for Free

Windows Azure Training Series – Creating Your First Azure Project

Windows Azure Training Series – Understanding Azure Roles

Windows Azure Training Series – Deploying a Windows Azure Application

You might also like to come to Learning Tree course 2602, Windows Azure Platform Introduction: Programming Cloud-Based Applications.

Doug Rehnstrom

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



Learning Tree Logo

Cloud Computing Training

Learning Tree offers over 210 IT training and Management courses, including Cloud Computing training.

Enter your e-mail address to follow this blog and receive notifications of new posts by e-mail.

Join 51 other followers

Follow Learning Tree on Twitter

Archives

Do you need a customized Cloud training solution delivered at your facility?

Last year Learning Tree held nearly 2,500 on-site training events worldwide. To find out more about hosting one at your location, click here for a free consultation.
Live, online training
.NET Blog

Follow

Get every new post delivered to your Inbox.

Join 51 other followers

%d bloggers like this: