Azure Table Storage Using Azure Storage Explorer

In this blog, you will learn about Azure Table storage using Azure Storage Explorer.

Why This Blog?

 
Microsoft provides Azure Storage Explorer and Azure Storage Emulator tools for developers who want to test and use storage accounts and services without getting a paid Azure subscription. In this article, I will discuss how to use Azure Table Storage using Development Storage Account, which is free.
 

Installation Process

  
Download Microsoft Azure Storage Explorer from the below URL
https://azure.microsoft.com/en-in/features/storage-explorer/
 
Download Microsoft Azure Storage Emulator - v5.8 (please download v5.8 version only)
https://azure.microsoft.com/en-in/downloads/
 
Key Point - Microsoft Azure Storage Explorer and Microsoft Azure Storage Emulator both should be active or running.
 

Start Explorer and Emulator

 
First, run your Explorer and Emulator. Please see the below snapshot; this will be your explorer. 
 

What is Azure Table Storage?

 
Azure Table storage stores large amounts of structured data. The service is a NoSQL datastore which accepts authenticated calls from inside and outside the Azure cloud. Azure tables are ideal for storing structured, non-relational data.
 

How To Create Azure Table storage?

 
Two Ways:
  1. In Azure Storage Explorer, Right-click on Table and select create table option, in text box enter your table name. Please refer to the below figure.

    Key Note:- table name should be in lowercase.

  2. We can create through program instruction also.
For Programing, What Things Are Required? 
 
The below namespace is required so please run the below NuGet command in your solution.
  1. using Microsoft.WindowsAzure.Storage;  
  2. using Microsoft.WindowsAzure.Storage.Table;  
Click On Tools => Hover On => Nuget Package Manager and select package manager console
 
Execute the below command.
  1. Install-Package WindowsAzure.Storage -Version 9.3.3  
This will be the default credentials and we are storing at the web config file.
 
DefaultEndpointsProtocol=http;
AccountName=devstoreaccount1;
AccountKey=Eby8vdM02xNOcqFlqUwJPLlmEtlCDXJ1OUzFT50uSRZ6IFsuFq2UVErCz4I6tq/K1SZFPTOtr/KBHBeksoGMGw==
 
Please add the below key in webconfig, connection string section.
  1. <connectionStrings>  
  2.    <add name="azurestorageconfigurationkey" connectionString="DefaultEndpointsProtocol=http;AccountName=devstoreaccount1;AccountKey=Eby8vdM02xNOcqFlqUwJPLlmEtlCDXJ1OUzFT50uSRZ6IFsuFq2UVErCz4I6tq/K1SZFPTOtr/KBHBeksoGMGw==" providerName="System.Data.SqlClient"/>  
  3. </connectionStrings>  
Add the necessary using statements,
  1. using Microsoft.WindowsAzure.Storage;  
  2. using Microsoft.WindowsAzure.Storage.Table;  
  3. using System.Threading.Tasks;  

Create Table

 
Get a CloudStorageAccount object that represents your storage account information.
 
Use the following code, using the name of your storage account and the account key, which you can find in the storage connection string in the webconfig file. 
  1. CloudStorageAccount storageAccount = CloudStorageAccount.DevelopmentStorageAccount;  
  2. string connectionString = ConfigurationManager.ConnectionStrings["azurestorageconfigurationkey"].ToString();  
Get a CloudTableClient object to reference the table objects in your storage account.
  1. CloudTableClient tableClient = storageAccount.CreateCloudTableClient();  
  2. CloudTable studentTable = tableClient.GetTableReference("studenttable");  
To create the Azure table, create an async method and within it, call CreateIfNotExistsAsync(),
  1. await studentTable.CreateIfNotExistsAsync();  
Please find the bellow figure for code implimentation.
 

Add An Entity(data) To A Table

 
To add an entity to a table, you create a class that defines the properties of your entity. The following code defines an entity class called StudentEntity that uses the student's "registration number" as the row key and colleage_code_as_partitionkeyvalue as the partition key.
 
Code Snippet For StudentEntity  Class 
  1. Public class StudentEntity: TableEntity {  
  2.     public StudentEntity(string RegistrationNumber, string colleage_code_as_partitionkeyvalue) {  
  3.         this.partitionkey = colleage_code_as_partitionkeyvalue);  
  4.     this.Rowkey = RegistrationNumber;  
  5. }  
  6. public StudentEntity() {}  
  7. public string StudentName {  
  8.     get;  
  9.     set;  
  10. }  
  11. public string Colleage {  
  12.     get;  
  13.     set;  
  14. }  
  15. public string Branch {  
  16.     get;  
  17.     set;  
  18. }  
  19. }  
Before adding an entity to the table let's discuss two important topics. 
 

PartitionKey

 
In Table Storage, you have to decide on the PartitionKey yourself.
 
In essence, you are responsible for the throughput you’ll get on your system.
 
If you put every entity in the same partition (by using the same partition key), you’ll be limited to the size of the storage machines for the amount of storage you can use. Plus, you’ll be constraining the maximal throughput as there’s lots of entities in the same partition.
 
Should you set the PartitionKey to the same value for every entity stored? No. You’ll end up with scaling issues at some point.
 

RowKey

 
The RowKey property stores string values that uniquely identify entities within each partition. The PartitionKey and the RowKey together form the primary key for the entity.
 
We can insert entities two ways,
  1. single entity insert
  2. batch of entity insert
Single Entity Insert
 
 
TableOperation - It is class and it Represents a single table operation.
Insert - Creates a new table operation that inserts the given entity into a table.
ExecuteAsync - Initiates an asynchronous operation that executes an asynchronous table operation.
 
Please refer to the below figure, whatever we inserted into entity is stored under the student table.
 
Batch Of Entity Insert 
 
TableBatchOperation
 
Represents a batch operation on a table. Batch operation is a collection of table operations that are executed by the Storage Service REST API as a single atomic operation, by invoking an Entity Group Transaction.
 
A batch operation may contain up to 100 individual table operations, with the requirement that each operation entity must have the same partition key. A batch with a retrieve operation cannot contain any other operations. Note that the total payload of a batch operation is limited to 4MB.
 
Insert the specified entity into a table.
 
Please refer to the below figure for output. In the below figure observe that entities are stored properly.
 
 
Get All Of The Entities Using Partition Key
 
To query a table for all of the entities in a partition, use a TableQuery object. The following code example specifies a filter for entities where '4UB' is the partition key. This example prints the fields of each entity in the query results to the console.
 
Get A Single Entity
 
You can write a query to get a single, specific entity. The following code uses a TableOperation object to specify a student registration number '4ub12cs505'. The method returns just one entity, rather than a collection, and the returned value in TableResult. A result is a StudentEntity object.
 
Specifying both partition and row keys in a query is the fastest way to retrieve a single entity from the Table service.
 
Delete An Entity
 
You can delete an entity after you find it. The following code looks for and deletes a student entity register number "4ub12cs502".
 

Summary


In this blog, we have discussed the below things,
  • Installation of Azure storage explorer and Azure storage emulator.
  • Create an Azure storage table.
  • Add an entity(data) to a table
  • Get a single entity
  • Get all of the entities using partition key
  • Delete an entity
Code Snippet
  1. //Create Storage Account:-        
  2. CloudStorageAccount storageAccount = CloudStorageAccount.DevelopmentStorageAccount;  
  3. string connectionString = ConfigurationManager.ConnectionStrings["azurestorageconfigurationkey"].ToString();  
  4. CloudTableClient tableClient = storageAccount.CreateCloudTableClient();  
  5. CloudTable studentTable = tableClient.GetTableReference("studenttable");  
  6. await studentTable.CreateIfNotExistsAsync();  
  7. //Create a new student entity, single object or row insertion to the table.         
  8. StudentEntity studentobject = new StudentEntity("4ub12cs505""4UB");  
  9. studentobject.Branch = "Computer Science";  
  10. studentobject.Colleage = "University BDT College";  
  11. studentobject.StudentName = "Kiran";  
  12. TableOperation insertOperation = TableOperation.Insert(studentobject);  
  13. await studentTable.ExecuteAsync(insertOperation);  
  14. //Create the batch operation     
  15. TableBatchOperation batchOperation = new TableBatchOperation();  
  16. StudentEntity stdentdata1 = new StudentEntity("4ub12cs502""4UB");  
  17. stdentdata1.Branch = "Computer Science";  
  18. stdentdata1.Colleage = "University BDT College";  
  19. stdentdata1.StudentName = "Madan Sorab";  
  20. StudentEntity stdentdata2 = new StudentEntity("4ub12cv402""4UB");  
  21. stdentdata2.Branch = "Civil";  
  22. stdentdata2.Colleage = "University BDT College";  
  23. stdentdata2.StudentName = "Mahesh";  
  24. batchOperation.Insert(stdentdata1);  
  25. batchOperation.Insert(stdentdata2);  
  26. await studentTable.ExecuteBatchAsync(batchOperation);  
  27. //Construct the query operation for all student entities where //PartitionKey="4UB". :-        
  28. TableQuery < StudentEntity > query = new TableQuery < StudentEntity > ().  
  29. Where(TableQuery.GenerateFilterCondition("PartitionKey", QueryComparisons.Equal, "4UB"));  
  30. TableContinuationToken token = null;  
  31. do {  
  32.     TableQuerySegment < StudentEntity > resultSegment = await studentTable.ExecuteQuerySegmentedAsync(query, token);  
  33.     token = resultSegment.ContinuationToken;  
  34.     foreach(StudentEntity entity in resultSegment.Results) {  
  35.         Console.WriteLine("{0}, {1}\t{2}\t{3}", entity.PartitionKey, entity.RowKey, entity.StudentName, entity.Branch);  
  36.     }  
  37. }  
  38. while (token != null);  
  39. //Create a retrieve operation that takes a student entity.      
  40. TableOperation retrieveOperation = TableOperation.Retrieve < StudentEntity > ("4UB""4ub12cs505");  
  41. TableResult retrievedResult = await studentTable.ExecuteAsync(retrieveOperation);  
  42. StudentEntity dataresult = new StudentEntity();  
  43. if (retrievedResult.Result != null) {  
  44.     dataresult = (StudentEntity) retrievedResult.Result;  
  45. else {  
  46.     dataresult = null;  
  47. }  
  48. //Delete Entity:      
  49. TableOperation retrieveOperation1 = TableOperation.Retrieve < StudentEntity > ("4UB""4ub12cs502");  
  50. TableResult retrievedResult1 = await studentTable.ExecuteAsync(retrieveOperation1);  
  51. StudentEntity deleteEntity = (StudentEntity) retrievedResult1.Result;  
  52. if (deleteEntity != null) {  
  53.     TableOperation deleteOperation = TableOperation.Delete(deleteEntity);  
  54.     await studentTable.ExecuteAsync(deleteOperation);  
  55. }   
I hope it's helpful. Eat-> Code->Sleep->Repeat.