Quantcast
Channel: Brian Pedersen's Sitecore and .NET Blog
Viewing all articles
Browse latest Browse all 286

C# Azure Table Storage QueryAsync, Paging and Filtering

$
0
0

Azure Table Storage is a NoSQL key-value store that can store massive amounts of data in a semi-structured way. Data is stored using a PartitionKey/RowKey keyset and a list of values. So far so good, but how do we query the danm thing?

First things first, have a look at my table storage, a list of famous celebrities:

Azure Table Storage

STEP 1: THE NUGET PACKAGES

You need the following NuGet package to access a Table Storage:

STEP 2: MAP THE TABLE STORAGE TO A MODEL CLASS:

Querying Table Storages are done by creating a model that implements the ITableEntity interface. The interface ensures that the ETag and Timestamp is present in the class:

using Azure;
using Azure.Data.Tables;

namespace MyCode
{
  public class FamousCelebritiesModel : ITableEntity
  {
    public string PartitionKey { get; set; }
    public string RowKey { get; set; }
    public int CelebrityIndex { get; set; }
    public string RealName { get; set; }
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public string Url { get; set; }
    public ETag ETag { get; set; } = default!;
    public DateTimeOffset? Timestamp { get; set; } = default!;
  }
}

STEP 3: CONNECT TO THE TABLE STORAGE

Connection is easy:

using Azure;
using Azure.Data.Tables;

namespace MyCode
{
  public class FamousCelebritiesRepository
  {
    private TableServiceClient _tableServiceClient;
    private TableClient _tableClient;

    public FamousCelebritiesRepository()
    {
      _tableServiceClient = new TableServiceClient("[your connection string]");
      _tableClient = _tableServiceClient.GetTableClient(tableName: "FamousCelebrities");
    }
	
	// more functions goes here
	// more functions goes here
	// more functions goes here
	// more functions goes here
	// more functions goes here

  }
}

QUERY 1: QUERYASYNC TO GET ALL ITEMS FROM THE TABLE STORAGE

The QueryAsync looks different from what we are used to, as the “await” operator is on the foreach loop:

public async Task<IEnumerable<FamousCelebritiesModel>> GetAllAsync()
{
  IList<FamousCelebritiesModel> modelList = new List<FamousCelebritiesModel>();
  var celebs = _tableClient.QueryAsync<FamousCelebritiesModel>(filter: "", maxPerPage: 10);
  await foreach (var celeb in celebs)
  {
    modelList.Add(celeb);
  }
  return modelList;
}

QUERY 2: QUERYASYNC WITH FILTER

You can apply simple LINQ filters to a Table Storage. This example filters by PartitionKey:

public async Task<IEnumerable<FamousCelebritiesModel>> GetByKnownAsync(string partitionKey)
{   
  IList<FamousCelebritiesModel> modelList = new List<FamousCelebritiesModel>();
  var celebs = _tableClient.QueryAsync<FamousCelebritiesModel>(x => x.PartitionKey == partitionKey, maxPerPage: 10);
  await foreach (var celeb in celebs)
  {
    modelList.Add(celeb);
  }
  return modelList;
}

QUERY 3: PAGINATION

Table Storage with millions of rows must be queried using pagination. Table Storage uses a “continuationtoken” to tell which page is next. Start with an empty token (null) to get the first page, and then use the continuationtoken from that query to get the next page:

public async Task<Tuple<string, IEnumerable<IFamousCelebritiesModel>>?> GetAllAsync(string continuationToken)
{
  IList<FamousCelebritiesModel> modelList = new List<FamousCelebritiesModel>();
  var celebs = _tableClient.QueryAsync<FamousCelebritiesModel>(filter: "", maxPerPage: 10);

  await foreach (var page in celebs.AsPages(continuationToken))
  {
    return Tuple.Create<string, IEnumerable<IFamousCelebritiesModel>>(page.ContinuationToken, page.Values);
  }
  return null;
}

Don’t know how to use a Tuple?

// Get first page:
var page = await GetAllAsync(null);
string continuationToken = page.Item1;
foreach (var item in page.Item2)  
{
  // take items from the list, for example:
  // item.RealName
}

// Get next page:
page = await GetAllAsync(continuationToken);
continuationToken = page.Item1;
foreach (var item in page.Item2)  
{
  // take items from the list, for example:
  // item.RealName
}

That’s it. You are now a Azure Table Storage Expert. Happy coding.

MORE TO READ:


Viewing all articles
Browse latest Browse all 286

Latest Images

Trending Articles



Latest Images