Azure Storage Table Batch Insert Operation

Introduction

 
This blog explains the process to bulk insert entities into Azure Storage Table using batch operations.
 
In this article, we will be using the Azure function (Timer Triggered), which you can schedule as per your own requirements.
 
In this article, we are mainly focusing on the batch job (i.e we will be using batch jobs to bulk insert the entities into Azure Table.)
 
In order to process this, I've created a mock LIST in the program with 250 items (This is only for demo purposes, but the overall logic will be the same for larger data/Entities.)
 

Environment

 
Storage Account

Create an Azure Storage account in your Azure subscription.
 
Follow the steps mentioned in Microsoft docs.
 
For example, I've used the Azure Timer function here. You can implement the same as per your project need, the rest of the logical part will be same.
 
You need to install below NuGet packages for the solution:
  • Windows.Azure.Storage
  • Newtonsoft.JSON
Azure Storage Table
 
All entities in a single batch operation must have all the same PartitionKey. Being the lazy programmer that I am, I only wanted to think about it once and I wanted it to work for anything I threw at it, so I came up with this. If you named your table "TestTable" and you used the tool below to generate the code for your project, this is what the batch insert operation would look like.
 
Batch Operation
 
A 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 operating 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.
 
Step 4.1 - Create EntityClass
 
Create an entity class as shown below, here "ContactNumber" is my custom field, the same field has been added in the Azure table as Column.
  1. public class UserEntity : TableEntity  
  2.         {  
  3.             public UserEntity() { }  
  4.             public UserEntity(string department, string UserName)  
  5.             {  
  6.                 PartitionKey = department;  
  7.                 RowKey = UserName;  
  8.             }  
  9.             public string ContactNumber { getset; }  
  10.         }  
Step 4.2 - Authenticate Azure Table
 
This function helps to authenticate your Azure Storage Table, mention your Azure Storage AccountName and AccountKey. 
 
Refer to the below image:
Azure Storage Table Batch Insert Operation
 
Also, replace the target table name with your TableName. 
  1. private static CloudTable AuthTable()  
  2.        {  
  3.            string accountName = "<<YOUR ACCOUNT STORAGE NAME>>";  
  4.            string accountKey = "<<SORAGE ACCOUNT KEY>>";  
  5.            string TargetTableName = "TestTable";  
  6.   
  7.            try  
  8.            {  
  9.                StorageCredentials storageCreds = new StorageCredentials(accountName, accountKey);  
  10.                CloudStorageAccount cloudStoageAccount = new CloudStorageAccount(storageCreds, useHttps: true);  
  11.   
  12.                CloudTableClient cloudTableClient = cloudStoageAccount.CreateCloudTableClient();  
  13.   
  14.                CloudTable cloudTable = cloudTableClient.GetTableReference(TargetTableName);  
  15.   
  16.                return cloudTable;  
  17.            }  
  18.            catch  
  19.            {  
  20.                return null;  
  21.            }  
  22.        }  
Step 4.3 - Create a Mockdata list (You can replace this list with your actual data)
 
This is mock data and we are using List<string> to store our mock data, you can replace this with your business data
  1. public static List<string> GetMockContactnumbers()  
  2.         {  
  3.             Random rand = new Random();  
  4.             List<string> contactList = new List<string>();  
  5.   
  6.             for (int i = 0; i < 250; i++)  
  7.             {  
  8.                 //rand = new Random();  
  9.                 //contactList = new List<string>();  
  10.   
  11.                 int randomNumber = rand.Next(8000000, 9999999);  
  12.   
  13.                 contactList.Add(randomNumber.ToString());  
  14.             }  
  15.   
  16.             return contactList;  
  17.         }  
Step 4.4 - Create Batches for Batch job

Create batches of your data, preferably batches of 100
 
The azure table will not allow you to batch more than 100 items. 
  1. public static IEnumerable<IEnumerable<T>> Batch<T>(this IEnumerable<T> collection, int batchSize)  
  2.         {  
  3.             var nextbatch = new List<T>(batchSize);  
  4.             foreach (T item in collection)  
  5.             {  
  6.                 nextbatch.Add(item);  
  7.                 if (nextbatch.Count == batchSize)  
  8.                 {  
  9.                     yield return nextbatch;  
  10.                     nextbatch = new List<T>(batchSize);  
  11.                 }  
  12.             }  
  13.   
  14.             if (nextbatch.Count > 0)  
  15.             {  
  16.                 yield return nextbatch;  
  17.             }  
  18.         }  
Step 4.5 - Iterate through each batch insert operation
 
Iterate through each batch and each item inside the batch
  1. //Creating Batches of 100 items in order to insert them into AzureStorage  
  2.               var batches = contactList.Batch(100);  
  3.   
  4.               //Iterating through each batch  
  5.               foreach (var batch in batches)  
  6.               {  
  7.                   batchOperationObj = new TableBatchOperation();  
  8.   
  9.                   //Iterating through each batch entities  
  10.                   foreach (var item in batch)  
  11.                   {  
  12.                       userEntityObj = new UserEntity("HR Department""rowKey");  
  13.                       {  
  14.                           UserIncrementCounter++;  
  15.                           userEntityObj.PartitionKey = "HR Department";  
  16.                           userEntityObj.RowKey = "User" + UserIncrementCounter.ToString();  
  17.   
  18.                           userEntityObj.ContactNumber = item;  
  19.   
  20.                       }  
  21.                       batchOperationObj.InsertOrReplace(userEntityObj);  
  22.                   }  
  23.   
  24.                   cloudTable.ExecuteBatch(batchOperationObj);  
  25.               }  
All set, after the execution of the code, you can check your table Azure Table Storage Explorer, as shown below.
 
Azure Storage Table Batch Insert Operation
 

Summary

 
In this article, we've seen how to bulk insert entities into an Azure Table. Regardless of the data, you can use List as well as an array to create batches using the same method. 

Click here for view above solution on GitHub.


Similar Articles