This topic explains how to use the .NET SDK to iteratively read data from Tablestore.
Prerequisites
Method
public IEnumerable<Row> GetRangeIterator(GetIteratorRequest request)Sample code
The following code iteratively reads data from the test_table table, starting from the row with the primary key value "row1".
try
{
// Set the start primary key.
PrimaryKey inclusiveStartPrimaryKey = new PrimaryKey()
{
{ "id", new ColumnValue("row1") }
};
// Set the end primary key. The result does not include the end primary key.
PrimaryKey exclusiveEndPrimaryKey = new PrimaryKey()
{
{ "id", ColumnValue.INF_MAX }
};
// Create a CapacityUnit object to track the capacity units consumed during iteration.
CapacityUnit capacityUnit = new CapacityUnit(0, 0);
// Create the request for the iterator.
GetIteratorRequest getIteratorRequest = new GetIteratorRequest("test_table", GetRangeDirection.Forward, inclusiveStartPrimaryKey, exclusiveEndPrimaryKey, capacityUnit);
// Call GetRangeIterator to get the iterator.
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}");
}The following examples show how to set different parameters for iterative reads.
Set the read direction.
// Set the start primary key. For a backward read, the start primary key must be greater than the end primary key. PrimaryKey inclusiveStartPrimaryKey = new PrimaryKey() { { "id", ColumnValue.INF_MAX } }; // Set the end primary key. The result does not include the end primary key. PrimaryKey exclusiveEndPrimaryKey = new PrimaryKey() { { "id", new ColumnValue("row1") } }; // Create the request for the iterator and set the direction to backward. GetIteratorRequest getIteratorRequest = new GetIteratorRequest("test_table", GetRangeDirection.Backward, inclusiveStartPrimaryKey, exclusiveEndPrimaryKey, capacityUnit);Set the maximum number of rows to read.
int limit = 20; // Create the request for the iterator. GetIteratorRequest getIteratorRequest = new GetIteratorRequest("test_table", GetRangeDirection.Forward, inclusiveStartPrimaryKey, exclusiveEndPrimaryKey, capacityUnit, null, limit);Specify the attribute columns to read.
HashSet<string> columnsToGet = new HashSet<string> { "col2" }; // Create the request for the iterator. GetIteratorRequest getIteratorRequest = new GetIteratorRequest("test_table", GetRangeDirection.Forward, inclusiveStartPrimaryKey, exclusiveEndPrimaryKey, capacityUnit, columnsToGet);