All Products
Search
Document Center

Tablestore:Read a range of data

Last Updated:Jun 04, 2026

Use the Tablestore .NET SDK to read data within a specified primary key range.

Prerequisites

Initialize the Tablestore client

Methods

public GetRangeResponse GetRange(GetRangeRequest request)

Asynchronous method:

public Task<GetRangeResponse> GetRangeAsync(GetRangeRequest request)

GetRangeRequest parameters

Parameter

Type

Description

tableName (Required)

string

The name of the table.

inclusiveStartPrimaryKey (Required)

PrimaryKey

The start primary key of the range. Includes primary key column names and values.

  • The returned data includes the start primary key.

  • The number and types of primary keys must match those of the table.

  • For a forward read, the start primary key must be less than the end primary key.

  • For a backward read, the start primary key must be greater than the end primary key.

  • ColumnValue.INF_MIN represents negative infinity and ColumnValue.INF_MAX represents positive infinity.

exclusiveEndPrimaryKey (Required)

PrimaryKey

The end primary key of the range. Includes primary key column names and values.

  • The returned data excludes the end primary key.

  • The number and types of primary keys must match those of the table.

  • ColumnValue.INF_MIN represents negative infinity and ColumnValue.INF_MAX represents positive infinity.

direction (Required)

GetRangeDirection

The read direction.

  • GetRangeDirection.Forward: Forward read.

  • GetRangeDirection.Backward: Backward read.

limit (Optional)

int

Maximum number of rows to return. Must be greater than 0. If more rows match, the response includes a NextPrimaryKey value for pagination.

columnsToGet (Optional)

HashSet<string>

Columns to read. Supports primary key columns and attribute columns.

  • If omitted, the operation returns the entire row.

  • If specified, returns only primary key columns and the specified attribute columns.

condition (Optional)

IColumnCondition

The row filter. Filter.

  • When both columnsToGet and condition are set, Tablestore filters rows by condition first, then returns only the columns in columnsToGet.

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);

Related topics