Azure Data Services: NoSQL Table Storage (Part 4)

Hello and welcome to this tutorial series of Azure Data Services. Through the course of this tutorial, I will take you through what Azure has to offer in Data Services. I will start with the basics of relational databases, dig into the goodness of non-relational offerings and end with what is new and what is exciting in Azure Data Services

Part 1: An Introduction to Azure Data Services
Azure Data Services: SQL in the Cloud (Part 2)
Azure Data Services: Blobbing did you say.. What is that? (Part 3)


Azure Table Storage is a non-relational NoSQL data store that allows for flexible datasets that are not constrained by schemas or models. It is built for massive scale. The data sets are split in partitions which supports load balancing across different nodes. The data is easily and very efficiently accessed using the combination of a partition and the row key, which allows a very fast lookup.

So lets just dig into terminology. Again, this falls under Storage Containers. So we have storage containers having one or more tables. Tables have one or more entities. Here entity can be thought of as rows. Each row can have different number of columns which resonates with the fact that this store has no fixed pre defined schema or template. These entities are partitioned across different partitions which are spread across multiple nodes using the partition key. Each entity is identified by the combination of the partition and the row keys, the composite key.

There are several ways we can access or query the data on these tables. This primarily uses a combination of Partition Key and Row Key as the index. SDKs for Storage Access across various languages such as .NET, Node, Java, PHP, Ruby and Python are provided. In general, the standard CRUD (Create, Read, Update and Delete) transactions are supported. It is also important to understand that like a Dictionary, each entity acts like a key-value pair. This implies that when a new entity is added, a new key is added. Likewise, if we re-use an existing key, the entity is replaced with the new property.

The HTTP queries can be queried using an HTTP endpoint and ODATA protocol (REST).

   1: //Base URL
   2: https://[account]
   4: //GET entity using Partition and Row Key
   5: [GET]/[table]([PartitionKey],[RowKey])
   7: //Query tables for entities that match an expression
   8: [GET]/[table]()?$filter=[query expression]
  10: //Delete an entry
  11: [DEL]/[table]([PartitionKey],[RowKey])
  13: //Insert or Replace
  14: [PUT]/[table]([PartitionKey],[RowKey])

So now let us look at how we access tables through Visual Studio:

Step 1: We need to install and add the references for Azure

   1: //Installing the dependencies using the following commands in the Package Manager Console
   2: install-package -version 5.6.0
   3: install-package -version
   5: //After successfully installing the references, add the following Using statements in the program.cs of the Console App
   6: using Microsoft.WindowsAzure.Storage;
   7: using Microsoft.WindowsAzure.Storage.Table;

Step 2: Lets get started with creating a Development Storage Account, which implies that here we are using the Azure Storage Development platform rather than the actual storage account in Azure.

   1: static void Main(string[] args)
   2:         {
   3:             CloudTableClient tableClient = CloudStorageAccount.DevelopmentStorageAccount.CreateCloudTableClient();
   4:             CloudTable tableClientRef = tableClient.GetTableReference("Roster");
   5:             tableClientRef.CreateIfNotExists();
   6:         }

Step 3: Create another class which will define the structure of the data model. This defines the structure of the data to give some idea of the kind of data that we ll be entering in our application.

   1: public class Employee : TableEntity
   2:     {
   3:         public int yearsAtCompany { get; set; }
   5:         public override string ToString()
   6:         {
   7:             return RowKey + "\t\t" + yearsAtCompany + "]";
   8:         }
  10:     }

Step 4: Back to our initial class, let us add some data to our table. Note, in the third entity I have not added one column property and that is completely fine.

   1: Employee first = new Employee { PartitionKey = "1", RowKey = "Jim", yearsAtCompany=2 };
   2: Employee second = new Employee { PartitionKey = "1", RowKey = "Tim", yearsAtCompany = 3 };
   3: Employee third = new Employee { PartitionKey = "1", RowKey = "Fin" };

Step 5: Lets conduct our insert operation on this set of data. There are 2 methods of doing this as shown. One using single Execute operation or the other using BatchExecute operation.

   1: //Single Execute Statement
   2: TableOperation InsertOperation = TableOperation.InsertOrReplace(first);
   3: tableClientRef.Execute(InsertOperation);
   5: //Batch Execution Statement
   6: TableBatchOperation batchOperation = new TableBatchOperation();
   7: batchOperation.InsertOrReplace(second);
   8: batchOperation.InsertOrReplace(third);
   9: tableClientRef.ExecuteBatch(batchOperation);

Step 6: Now, before we run the code, let us look into the Server Explorer to see if the table exists and you will find that there are no tables currently.


Step 7: Lets execute the program and then we will see that a table has been created:



That's it! It is as simple as that to use Azure Table Storage. We started by talking about its advantages in terms of scalability and high performance indexing. We then dived into the access and operations of the data store. In the next section, I will be discussing Storage Queues and how it acts like an effective messaging system. I will also cover certain elements of the Service Bus which has a similar function but different use case. Stay tuned and hit me @AdarshaDatta with your experience on Azure.


Comments (0)

Skip to main content