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

时间:2015-07-28 16:44:10

标签: azure azure-storage

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?

2 个答案:

答案 0 :(得分:5)

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.

答案 1 :(得分:1)

你不能在键上做像contains()这样的事情。但由于它支持CompareTo("")方法,因此您需要稍微修改表格设计。

维护多个分区键而不是单个键。你可以简单地推动KJV'行键的一部分到分区键。然后从C1-V1开始,C1-V2作为行键。

然后,如果你想要

  1. KJV的所有条目 - 查询分区键' KJV'
  2. 所有' C1' KJV的条目 - 查询分区键' KJV'和行键以' C1 - '
  3. 开头
  4. C1的所有条目 - 查询以' C1 - '
  5. 开头的行键

    或者表格中的设计更改,您需要遍历您的主要产品,例如KJV'并构建多个查询,每个查询以' KJV-C1 - '开头,然后联合所有查询以获得最终结果。

    请注意,表存储不允许所有LINQ操作,有时您需要设计表键以记住大多数查询。