このトピックでは、.NET SDK を使用して Tablestore からデータを反復的に読み取る方法について説明します。
前提条件
メソッド
public IEnumerable<Row> GetRangeIterator(GetIteratorRequest request)サンプルコード
次のコードは、プライマリキー値が "row1" の行から、test_table テーブルのデータを反復的に読み取ります。
try
{
// 開始プライマリキーを設定します。
PrimaryKey inclusiveStartPrimaryKey = new PrimaryKey()
{
{ "id", new ColumnValue("row1") }
};
// 終了プライマリキーを設定します。結果には終了プライマリキーは含まれません。
PrimaryKey exclusiveEndPrimaryKey = new PrimaryKey()
{
{ "id", ColumnValue.INF_MAX }
};
// 反復処理中に消費されたキャパシティーユニットを追跡する CapacityUnit オブジェクトを作成します。
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);