Table Store支援在查詢資料時使用過濾器在服務端按指定條件進行資料過濾,本文介紹如何在 .NET SDK 中使用過濾器。
前提條件
過濾器類型
Table Store的過濾器類型包括以下 2 種。
單屬性列值過濾器(RelationalCondition):判斷單個屬性列的值是否符合條件。
組合過濾器(CompositeCondition):將多個條件組合進行資料過濾。
單屬性列值過濾器
public class RelationalCondition : IColumnCondition範例程式碼
以下範例程式碼以範圍查詢為例查詢主索引值為 [row1, row3) 的行資料,並在查詢後進行過濾,返回 col1 屬性列的值等於 val1 的行資料。
try
{
// 設定查詢起始主鍵
PrimaryKey inclusiveStartPrimaryKey = new PrimaryKey()
{
{ "id", new ColumnValue("row1") }
};
// 設定查詢結束主鍵(返回結果不包含結束主鍵)
PrimaryKey exclusiveEndPrimaryKey = new PrimaryKey()
{
{ "id", new ColumnValue("row3") }
};
// 構造過濾器,條件為 col1 == "val1"
RelationalCondition relationalCondition = new RelationalCondition("col1", CompareOperator.EQUAL, new ColumnValue("val1"));
// 調用 GetRange 方法讀取行資料
GetRangeRequest getRangeRequest = new GetRangeRequest("test_table", GetRangeDirection.Forward, inclusiveStartPrimaryKey, exclusiveEndPrimaryKey, null, null, relationalCondition);
GetRangeResponse getRangeResponse = client.GetRange(getRangeRequest);
// 返回結果處理
Console.WriteLine($"RequestId: {getRangeResponse.RequestID}");
Console.WriteLine($"Read CU Cost: {getRangeResponse.ConsumedCapacityUnit.Read}");
Console.WriteLine($"Write CU Cost: {getRangeResponse.ConsumedCapacityUnit.Write}");
Console.WriteLine("Row Data: ");
foreach (Row row in getRangeResponse.RowDataList)
{
Console.WriteLine(row);
}
}
catch (Exception ex)
{
Console.WriteLine($"Get Range failed, exception: {ex.Message}");
}設定行資料不包含判斷的屬性列時,不返回該行。
relationalCondition.PassIfMissing = false;
組合過濾器
最多支援 32 個條件的組合。
public class CompositeCondition : IColumnCondition範例程式碼
以下範例程式碼以範圍查詢為例查詢主索引值為 [row1, row3) 的行資料,並使用組合過濾器進行資料過濾。
try
{
// 設定查詢起始主鍵
PrimaryKey inclusiveStartPrimaryKey = new PrimaryKey()
{
{ "id", new ColumnValue("row1") }
};
// 設定查詢結束主鍵(返回結果不包含結束主鍵)
PrimaryKey exclusiveEndPrimaryKey = new PrimaryKey()
{
{ "id", new ColumnValue("row3") }
};
// 構造單屬性列值過濾器1,條件為 col1 == "val1"
RelationalCondition relationalCondition1 = new RelationalCondition("col1", CompareOperator.EQUAL, new ColumnValue("val1"));
// 構造單屬性列值過濾器2,條件為 col2 == "val2"
RelationalCondition relationalCondition2 = new RelationalCondition("col2", CompareOperator.EQUAL, new ColumnValue("val2"));
// 構造組合過濾器1,條件為 col1 == "val1" or col2 == "val2"
CompositeCondition compositeCondition1 = new CompositeCondition(LogicOperator.OR);
compositeCondition1.AddCondition(relationalCondition1);
compositeCondition1.AddCondition(relationalCondition2);
// 構造單屬性列值過濾器3,條件為 col3 == "val3"
RelationalCondition relationalCondition3 = new RelationalCondition("col3", CompareOperator.EQUAL, new ColumnValue("val3"));
// 構造組合過濾器2,條件為 組合過濾器1 and 單屬性列值過濾器3,即 (col1 == "val1" or col2 == "val2") and col3 == "val3"
CompositeCondition compositeCondition2 = new CompositeCondition(LogicOperator.AND);
compositeCondition2.AddCondition(compositeCondition1);
compositeCondition2.AddCondition(relationalCondition3);
// 調用 GetRange 方法讀取行資料
GetRangeRequest getRangeRequest = new GetRangeRequest("test_table", GetRangeDirection.Forward, inclusiveStartPrimaryKey, exclusiveEndPrimaryKey, null, null, compositeCondition2);
GetRangeResponse getRangeResponse = client.GetRange(getRangeRequest);
// 返回結果處理
Console.WriteLine($"RequestId: {getRangeResponse.RequestID}");
Console.WriteLine($"Read CU Cost: {getRangeResponse.ConsumedCapacityUnit.Read}");
Console.WriteLine($"Write CU Cost: {getRangeResponse.ConsumedCapacityUnit.Write}");
Console.WriteLine("Row Data: ");
foreach (Row row in getRangeResponse.RowDataList)
{
Console.WriteLine(row);
}
}
catch (Exception ex)
{
Console.WriteLine($"Get Range failed, exception: {ex.Message}");
}