Is there a way to get distinct PartionKeys from a Table

ctacke picture ctacke · Oct 12, 2012 · Viewed 10.6k times · Source

Currently I'm using the PartitionKey to differentiate devices that are storing data into Azure Table Services. I'd like to build a viewer that allows me to browse that data, but it would be nice to be able to structure it so I can view data "by device", or by PartitionKey. The viewer app won't have any knowledge of what devices exist, so it would be great if I could somehow get back a list of distinct PartionKeys in a given Table. Is this possible, or am I going to be relegated to creating a meta-data table into which I insert a new row for each device, then use that for querying?

Answer

danatcofo picture danatcofo · Feb 28, 2013

Create a single table to store your partitions. Partition the table by the table names you use and add an entry for each partition you create.

public class PartitionEntry : TableServiceEntity { }

tableServiceContext.AddObject("TablePartitions", new PartitionEntry
{
    PartitionKey = "<table name>",
    RowKey = "<partition key>",
});
tableServiceContext.BeginSaveChanges(SaveChangesOptions.ContinueOnError, null, null);

then just query this table to get a list of partitions. This is very manageable to me.

var tbl = tableServiceContext.CreateQuery<PartitionEntry>("TablePartitions");
return tbl.Where(i => i.PartitionKey == "<table name>")
          .Select(i => new { PartitionKey = i.RowKey, });

I bet this could be optimized.