dcsimg
 

An Introduction to Azure Table Storage

Wednesday Jun 19th 2019 by Tapas Pal
An Introduction to Azure Table Storage

Follow along with the code provided and become more proficient with using Azure Storage Tables.

Introduction

Windows Azure Storage Tables are very popular, non-relational, Key-Value-pair, storage systems suitable for storing massive amounts of unstructured data. Developers use Table storage to store structured NoSQL data in the Cloud, providing a Key/attribute store with a schemaless design. The advantage of using table storage is fast and cost-effective for many types of applications. It can store a large amount of structured data. In addition, table storage can store flexible datasets, such as user data for a Web application or any other device information or any other types of metadata, which your service requires. Developers also can use Table Storage to store and query huge set/s of structured, non-relational data.

Creating New Table Storage

To create a new Azure Table service, you need to have a Windows Azure account. Developers can get a free trial account or, if you have a MSDN subscription, you can sign up for your Windows Azure. Once you have an Azure account, you then can create a storage account to be used to store Tables.

To create a new storage account, you have to log in to the Windows Azure management portal. Next, in the portal, you can quickly create a Storage Account by clicking on the large NEW icon at the bottom left hand of the portal, as shown in Figure 1.

Create New Azure Resource
Figure 1: Create New Azure Resource

Next, from Azure Marketplace select storage, then select storage account; you can see this in Figure 2.

New Azure Storage
Figure 2: New Azure Storage

Fill in the storage account name and location. For other settings, leave as default, and then click the review + create button.

After successful deployment, you will see the deployment success screen shown in Figure 3.

New Azure Storage Deployed
Figure 3: New Azure Storage Deployed

Each request you made against Microsoft Azure Storage must be authorized. One of the methods for authorizing access to a storage account is by using a connection string.

A connection string includes the authentication information required for your application to access data in an Azure storage account at run time. To get the connection string open storage account, from settings click on Access Keys. You can see this in Figure 4.

Azure Storage Connection String
Figure 4: Azure Storage Connection String

How to Connect Storage Tables from .NET

You should use the Microsoft Azure CosmosDB Table library in .NET to access Azure Storage tables. To get started, you need to refer to the Microsoft.Azure.CosmosDB.Table namespace in your .NET project. To demonstrate, I have added the following code to define an entity class that uses the student's first name as the row key and last name as the partition key. Together, an entity's partition and row key uniquely identify it in the table.

public class StudentEntity : TableEntity
{
   public StudentEntity(string StudentlastName, string
      StudentlastName)
   {
      this.PartitionKey = StudentlastName; this.RowKey =
         StudentlastName;
   }
   public StudentEntity() { }
   public string StudentEmail { get; set; }
   public string StudentPhoneNumber { get; set; }
}

Next, the Entities defined in the previous step can be queried faster than entities with different partition keys.

The following code example shows the creation of the CloudTable object and then of a StudentEntity object.

StudentEntity objStudentEntity = new StudentEntity("Tapas", "Pal")
{
   Email = "tapas.pal@gmail.com",
   PhoneNumber = "6662832930293"

};

TableOperation insertOperation =
   TableOperation.Insert(objStudentEntity);
table.Execute(insertOperation);

To query a table for all entities in a partition, you can use the TableQuery object. The following code example specifies a filter for entities with MVC code.

public ActionResult Index()
{
   var account = CloudStorageAccount.Parse
      (ConfigurationManager.AppSettings
         ["tapasazureStorageAccount"]);
   var client = account.CreateCloudTableClient();

   var table = client.GetTableReference("Student");

   table.CreateIfNotExists();

   StudentEntity objStudentEntity = new
      StudentEntity("Tapas", "Pal")
   {
      Email = "tapas.pal@gmail.com",
      PhoneNumber = "6662832930293"

   };

   TableOperation insertOperation =
      TableOperation.Insert(objStudentEntity);
   table.Execute(insertOperation);

   return View();
}

public ActionResult Students()
{

   var condition = TableQuery.GenerateFilterCondition
      ("PartitionKey", QueryComparisons.Equal, "Tapas");
   var query = new TableQuery<StudentEntity>()
      .Where(condition);

   var account = CloudStorageAccount.Parse
      (ConfigurationManager.AppSettings
         ["tapasazureStorageAccount"]);
   var client = account.CreateCloudTableClient();
   var table = client.GetTableReference("Student");

   var lst = table.ExecuteQuery(query);

   return View(lst);
}

Conclusion

Azure Table Storage is a highly scalable, Key-Value pair NoSQL storage system. Knowing how table storage works will help you determine if it is a good fit for your particular requirements. That is all for today. Continue reading!

Home
Mobile Site | Full Site