maxnelso
maxnelso

Reputation: 85

Azure Table Storage Query For Unique Partition Key

My problem is as follows, I have an Azure Table Storage that is set up like this:

I want to make a query such that given a number X, I want to return the highest number Row Key that is less than X for each Partition Key. That is kind of confusingly worded, so here is an example:

Partition Key, Row Key

"hello", 7

"hello", 9

"hello", 12

"other", 6

If I queried with the number 10, I want to return a list like [("hello", 9), ("other", 6)]. Note that I want only unique partition keys and the highest number that is less than 10.

I can do this for a particular name with something like (leaving some stuff out for brevity):

    string orderFilter = TableQuery.GenerateFilterConditionForLong("RowKey", "le", 10);
    string nameFilter = TableQuery.GenerateFilterCondition("PartitionKey", "eq", "hello");

    TableQuery query = new TableQuery().Where(TableQuery.CombineFilters(orderFilter, "and", nameFilter));

    IEnumerable<NameEntity> names = await nameTable.ExecuteQuerySegmentedAsync<NameEntity>(query, entityResolver, continuationToken);

    // Get newest name
    NameEntity newestName = names.Last();

But I can't seem to figure out how to do this with all the different Partition Keys. Is this possible in a single query? Thanks for any help!

Upvotes: 3

Views: 3113

Answers (2)

David Makogon
David Makogon

Reputation: 71028

There is no way to enumerate partition keys (without doing a table scan), so you can either:

  • do a full table scan, keeping track of various partition keys you find along the way, along with highest-value row key
  • keep track of your partition keys as you write them to table storage (e.g. create another table with just your partition keys).

With the latter, the other answer suggests writing the max-value for that partition key, at the same time. Certainly viable. But you'll need to remember that you need to maintain this bit of metadata.

Upvotes: 3

Isaac Abraham
Isaac Abraham

Reputation: 3502

Pretty sure that you can't do this - Azure Tables just aren't set up to do this sort of things AFAIK. An alternative solution would be to create a second table with partition key based on the number and one row for each max-value-per-partition (in other words, eagerly calculate this), or something like that.

Upvotes: 1

Related Questions