Randy Minder
Randy Minder

Reputation: 48522

Azure Table Storage - Retrieving all entities matching a partial row key

I am just learning Azure Table Storage and I'm able to save and retrieve entities without any problem. However, I'd like to do the following. Say I have row keys (all with the same partition key) that look as follows:

KJV-C1-V1
KJV-C1-V2
KJV-C1-V3
KJV-C2-V1
KJV-C2-V2
KJV-C2-V3

I'd like to be able to perform two types of queries in .NET C#:

  1. Retrieve all entities with row keys that start with 'KJV-C1'.
  2. Retrieve all entities with row keys that contain '-C1-' in the key

Preferrably I'd like to be able to do this without reading all entities in the partition and filtering the ones that don't match the pattern I'm looking for. Is this possible with Azure Table Storage queries?

Upvotes: 0

Views: 2920

Answers (2)

Mahesh Jasti
Mahesh Jasti

Reputation: 572

You cannot do something like contains() over keys. But as it supports CompareTo("") method, you need to slightly modify your table design.

Maintain multiple partition keys instead of single. You can simple push 'KJV' part of your row key to partition key. Then start with C1-V1, C1-V2 as your row keys.

Then, if you want

  1. All entries of KJV - Query for partition key 'KJV'
  2. All 'C1' entries of KJV - Query for partition key 'KJV' and row key starting with 'C1-'
  3. All entries for C1 - Query for row key starting with 'C1-'

OR with out design change in table, you need to loop through your major products like 'KJV' and build multiple queries with each starting with 'KJV-C1-', then union all of them to get final result.

Please mind that table storage does not allow all LINQ operations and sometimes you need to design the table keys keeping your majority of queries in mind.

Upvotes: 1

Gaurav Mantri
Gaurav Mantri

Reputation: 136366

Retrieve all entities with row keys that start with 'KJV-C1'.

This is possible. Sample OData query:

PartitionKey eq 'your partition key' and (RowKey ge 'KJV-C1' and RowKey lt 'KJV-C2')

Retrieve all entities with row keys that contain '-C1-' in the key

This unfortunately is not possible. You would have to fetch all entities and filter the data on the client side.

Upvotes: 5

Related Questions