全部產品
Search
文件中心

Tablestore:過濾器

更新時間:May 12, 2026

Table Store支援在查詢資料時使用過濾器在服務端按指定條件進行資料過濾,本文介紹如何在 .NET SDK 中使用過濾器。

前提條件

初始化Tablestore Client

過濾器類型

Table Store的過濾器類型包括以下 2 種。

  • 單屬性列值過濾器(RelationalCondition):判斷單個屬性列的值是否符合條件。

  • 組合過濾器(CompositeCondition):將多個條件組合進行資料過濾。

單屬性列值過濾器

public class RelationalCondition : IColumnCondition

參數說明

名稱

類型

說明

Operator(必選)

CompareOperator

關係運算子,包括 EQUAL(等於)、NOT_EQUAL(不等於)、GREATER_THAN(大於)、GREATER_EQUAL(大於等於)、LESS_THAN(小於)、LESS_EQUAL(小於等於)。

ColumnName(必選)

string

判斷的屬性列名稱。

ColumnValue(必選)

ColumnValue

判斷的值。

PassIfMissing(可選)

bool

行資料不包含判斷的屬性列時,是否返回該行,預設值為 true,即返回該行資料。

LatestVersionsOnly(可選)

bool

是否只判斷查詢結果中最新的資料版本,預設值為 true,即當參考的屬性列存在多個資料版本時,只判斷最新的資料版本是否符合過濾條件。

範例程式碼

以下範例程式碼以範圍查詢為例查詢主索引值為 [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

參數說明

名稱

類型

說明

LogicOperator(必選)

LogicOperator

邏輯運算子,包括 NOT(非)、AND(與)、OR(或)。

subConditions(必選)

List<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}");
}

相關文檔