全部產品
Search
文件中心

Tablestore:迭代讀取資料

更新時間:May 12, 2026

本文介紹如何通過 .NET SDK 迭代讀取Table Store中的資料。

前提條件

初始化Tablestore Client

方法說明

public IEnumerable<Row> GetRangeIterator(GetIteratorRequest request)

GetIteratorRequest參數說明

名稱

類型

說明

tableName(必選)

string

資料表名稱。

inclusiveStartPrimaryKey(必選)

PrimaryKey

起始主鍵資訊,包括主鍵列名稱和主索引值。

  • 返回資料包含起始主鍵。

  • 主鍵個數和類型必須與資料表的主鍵保持一致。

  • 正向讀取資料時,起始主鍵必須小於結束主鍵。

  • 反向讀取資料時,起始主鍵必須大於結束主鍵。

  • ColumnValue.INF_MIN 表示無限小,ColumnValue.INF_MAX 表示無限大。

exclusiveEndPrimaryKey(必選)

PrimaryKey

結束主鍵資訊,包括主鍵列名稱和主索引值。

  • 返回資料不包含結束主鍵。

  • 主鍵個數和類型必須與資料表的主鍵保持一致。

  • ColumnValue.INF_MIN 表示無限小,ColumnValue.INF_MAX 表示無限大。

direction(必選)

GetRangeDirection

讀取方向。

  • GetRangeDirection.Forward:正向讀取資料。

  • GetRangeDirection.Backward:反向讀取資料。

consumedCapacityUnitCounter(必選)

CapacityUnit

操作消耗的服務能力單元。

limit(可選)

int

最大迭代讀取行數,必須大於 0。

columnsToGet(可選)

HashSet<string>

指定讀取的資料列,可以是主鍵列或屬性列。

  • 不設定columnsToGet時,返回整行資料。

  • 設定columnsToGet時,如果讀取的行資料不包含任何指定的資料列,將返回 null。

condition(可選)

IColumnCondition

過濾條件,詳情請參見過濾器

  • 如果同時設定columnsToGet和condition,先按columnsToGet篩選合格資料行,再按condition條件過濾資料。

範例程式碼

以下範例程式碼使用迭代方式讀取 test_table 表中從主索引值 row1 開始的資料。

try
{
    // 設定查詢起始主鍵
    PrimaryKey inclusiveStartPrimaryKey = new PrimaryKey()
    {
        { "id", new ColumnValue("row1") }
    };
    // 設定查詢結束主鍵(返回結果不包含結束主鍵)
    PrimaryKey exclusiveEndPrimaryKey = new PrimaryKey()
    {
        { "id", ColumnValue.INF_MAX }
    };

    // 構造CapacityUnit(記錄迭代過程中消耗的CU值)
    CapacityUnit capacityUnit = new CapacityUnit(0, 0);

    // 構造迭代查詢條件
    GetIteratorRequest getIteratorRequest = new GetIteratorRequest("test_table", GetRangeDirection.Forward, inclusiveStartPrimaryKey, exclusiveEndPrimaryKey, capacityUnit);

    // 調用 GetRangeIterator 擷取迭代器
    IEnumerable<Row> iterator = client.GetRangeIterator(getIteratorRequest);
    foreach (var row in iterator)
    {
        Console.WriteLine(row);
    }
}
catch (Exception ex)
{
    Console.WriteLine($"Get range iterator failed, exception: {ex.Message}");
}

您可以在迭代讀取資料時參考以下範例程式碼進行參數設定。

  • 設定資料讀取方向。

    // 設定查詢起始主鍵(反向讀取資料時,起始主鍵要大於結束主鍵)
    PrimaryKey inclusiveStartPrimaryKey = new PrimaryKey()
    {
        { "id", ColumnValue.INF_MAX }
    };
    // 設定查詢結束主鍵(返回結果不包含結束主鍵)
    PrimaryKey exclusiveEndPrimaryKey = new PrimaryKey()
    {
        { "id", new ColumnValue("row1") }
    };
    
    // 構造迭代查詢條件(設定反向讀取資料)
    GetIteratorRequest getIteratorRequest = new GetIteratorRequest("test_table", GetRangeDirection.Backward, inclusiveStartPrimaryKey, exclusiveEndPrimaryKey, capacityUnit);
  • 設定最大迭代讀取行數。

    int limit = 20;
    
    // 構造迭代查詢條件
    GetIteratorRequest getIteratorRequest = new GetIteratorRequest("test_table", GetRangeDirection.Forward, inclusiveStartPrimaryKey, exclusiveEndPrimaryKey, capacityUnit, null, limit);
  • 指定讀取的屬性列。

    HashSet<string> columnsToGet = new HashSet<string> { "col2" };
    
    // 構造迭代查詢條件
    GetIteratorRequest getIteratorRequest = new GetIteratorRequest("test_table", GetRangeDirection.Forward, inclusiveStartPrimaryKey, exclusiveEndPrimaryKey, capacityUnit, columnsToGet);