本文介紹如何通過 .NET SDK 迭代讀取Table Store中的資料。
前提條件
方法說明
public IEnumerable<Row> GetRangeIterator(GetIteratorRequest request)範例程式碼
以下範例程式碼使用迭代方式讀取 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);