Use the Tablestore .NET SDK to read data within a specified primary key range.
Prerequisites
Methods
public GetRangeResponse GetRange(GetRangeRequest request)
Asynchronous method:
public Task<GetRangeResponse> GetRangeAsync(GetRangeRequest request)
Code examples
Read all rows from test_table where the primary key is greater than row1:
try
{
// Set the start primary key.
PrimaryKey inclusiveStartPrimaryKey = new PrimaryKey()
{
{ "id", new ColumnValue("row1") }
};
// Set the end primary key. The result set excludes the end primary key.
PrimaryKey exclusiveEndPrimaryKey = new PrimaryKey()
{
{ "id", ColumnValue.INF_MAX }
};
// Call the GetRange method to read rows.
GetRangeRequest getRangeRequest = new GetRangeRequest("test_table", GetRangeDirection.Forward, inclusiveStartPrimaryKey, exclusiveEndPrimaryKey);
GetRangeResponse getRangeResponse = client.GetRange(getRangeRequest);
// Process the response.
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}");
}
A single GetRange request returns up to 5,000 rows or 4 MB of data. If more data matches, the response includes a NextPrimaryKey value. Use it as the inclusiveStartPrimaryKey in subsequent requests to paginate. The following example loops through all matching rows:
while (true)
{
// Call the GetRange method to read rows.
GetRangeRequest getRangeRequest = new GetRangeRequest("test_table", GetRangeDirection.Forward, inclusiveStartPrimaryKey, exclusiveEndPrimaryKey);
GetRangeResponse getRangeResponse = client.GetRange(getRangeRequest);
// Process the response.
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);
}
if (getRangeResponse.NextPrimaryKey != null)
{
inclusiveStartPrimaryKey = getRangeResponse.NextPrimaryKey;
}
else
{
break;
}
}
Additional query options:
-
Read backward.
// 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 set excludes the end primary key. PrimaryKey exclusiveEndPrimaryKey = new PrimaryKey() { { "id", new ColumnValue("row1") } }; // Call the GetRange method to read rows backward. GetRangeRequest getRangeRequest = new GetRangeRequest("test_table", GetRangeDirection.Backward, inclusiveStartPrimaryKey, exclusiveEndPrimaryKey); -
Return specific columns.
HashSet<string> columnsToGet = new HashSet<string> { "col2" }; // Call the GetRange method to read rows. GetRangeRequest getRangeRequest = new GetRangeRequest("test_table", GetRangeDirection.Forward, inclusiveStartPrimaryKey, exclusiveEndPrimaryKey, columnsToGet, null); -
Limit row count.
int limit = 10; // Call the GetRange method to read rows. GetRangeRequest getRangeRequest = new GetRangeRequest("test_table", GetRangeDirection.Forward, inclusiveStartPrimaryKey, exclusiveEndPrimaryKey, null, limit);