Wednesday, June 17, 2015

Asp.net, C#: Connect to Azure Storage Table and fetch/filter all the records in specific table

Azure Storage Table
Create a project(console) in Visual Studio.
Install Cloud Storage package from nugget package(steps below).
Step 1: Right click on project in solution explorer


Step 2: Click on Online Packages > Search for Azure > Install Windows Azure Storage:






Create a class for every Table in Storage. Here in following example, I am creating a class for table. Class name could be anything. But column names must be same as columns in storage table.

using Microsoft.WindowsAzure.Storage.Table;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace AzureStorage.Classes
{
    class TableName1 : TableEntity
    {
        public TableName1(string pKey, string rKey)
        {
            this.PartitionKey = pKey;
            this.RowKey = rKey;
        }

        public TableName1() { }

        public string CustomCloumn1 { get; set; }
        public int CustomCloumn2 { get; set; }
        public int CustomCloumn3 { get; set; }
        public int CustomCloumnN { get; set; }
        public DateTime TIMESTAMP { get; set; }
    }
}

PS: we need to inherit the class from TableEntity class. This class resides in following name space:
Microsoft.WindowsAzure.Storage.Table;
Now we need to hit storage table in Azure and get data from there. For this we need StorageAccountName and StorageAccountKey. And the new need to create a connection string key in app.config file. You may create a connection string via some tool or simply copy paste below key in appSettings.

  <appSettings>
    <add key="StorageConnectionString" value="DefaultEndpointsProtocol=https;AccountName=YourAzureAccountName;AccountKey=YourAzureAccountKey" />
  </appSettings>



Following code will fetch all the records in storage table TableName1. I have used a tick here. I have specified partition key not equal nitin. Which will get all the records (as there is not partition key nitin.). similarly you may choose desired operation and value to match. Partition key and row keys are default columns. In my next post I will explain how can we specific custom filter expressions,  and filter on multiple user defined columns, and filter on various types of data(like date).

static void Main(string[] args)
{
    int counter = 0;
    StringBuilder sb = new StringBuilder();

    // Retrieve the storage account from the connection string.
    CloudStorageAccount storageAccount = CloudStorageAccount.Parse(
    CloudConfigurationManager.GetSetting("StorageConnectionString"));

    // Create the table client.
    CloudTableClient tableClient = storageAccount.CreateCloudTableClient();

    // Create the CloudTable object that represents the "TableName1" table.
    CloudTable table = tableClient.GetTableReference("TableName1");

    // Construct the query operation for all customer entities where PartitionKey!="nitin".
    TableQuery<TableName1> query = new TableQuery<TableName1>().Where(TableQuery.GenerateFilterCondition("PartitionKey", QueryComparisons.NotEqual, "nitin"));

    // Print the fields for each customer.
    foreach (TableName1 entity in table.ExecuteQuery(query))
    {
        if (counter > 500) break;
        counter++;
        Console.WriteLine("{0}, {1}, {2}, {3}, {4}, {5}, {6}",
        entity.PartitionKey,
        entity.RowKey,
        entity.CustomCloumn1,
        entity.CustomCloumn2,
        entity.CustomCloumn3,
        entity.CustomCloumnN,
        entity.TIMESTAMP);
        sb.AppendFormat("{0}, {1}, {2}, {3}, {4}, {5}, {6}",
        entity.PartitionKey,
        entity.RowKey,
        entity.CustomCloumn1,
        entity.CustomCloumn2,
        entity.CustomCloumn3,
        entity.CustomCloumnN,
        entity.TIMESTAMP);
    }
    Console.WriteLine(counter);
    Console.WriteLine(sb.ToString());

    Console.WriteLine("Done... press a key to end.");
    Console.ReadKey();
}

Congrats All Done. Run and check. Let me know if you face any issue.



No comments:

Post a Comment