This topic describes how to read a range of data from Tablestore by using the .NET SDK.
Prerequisites
Methods
public GetRangeResponse GetRange(GetRangeRequest request)Asynchronous method:
public Task<GetRangeResponse> GetRangeAsync(GetRangeRequest request)Code examples
The following code example shows how to read data from the test_table table where the primary key value 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 can return a maximum of 5,000 rows or 4 MB of data. If more data matches the query range, the response includes a NextPrimaryKey value. To retrieve the remaining data, use this value as the inclusiveStartPrimaryKey in your next GetRange request. The following code shows how to use a loop to iterate 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;
}
}You can also configure your query using the following settings.
Set the data 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 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);Specify the attribute columns to read.
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);Set the maximum number of rows to return.
int limit = 10; // Call the GetRange method to read rows. GetRangeRequest getRangeRequest = new GetRangeRequest("test_table", GetRangeDirection.Forward, inclusiveStartPrimaryKey, exclusiveEndPrimaryKey, null, limit);