All Products
Search
Document Center

Tablestore:Range read

Last Updated:Apr 01, 2026

This topic describes how to read a range of rows from a Tablestore table using the Go SDK.

Prerequisites

Initialize Tablestore Client

Method

func (tableStoreClient *TableStoreClient) GetRange(request *GetRangeRequest) (*GetRangeResponse, error)

GetRangeRequest parameters

  • RangeRowQueryCriteria (required)*RangeRowQueryCriteria: Specifies the criteria for the range read.

    Parameter

    Type

    Description

    TableName (required)

    string

    The name of the table.

    StartPrimaryKey (required)

    *PrimaryKey

    The start primary key, including its column names and values.

    • The result set includes the row that corresponds to the start primary key.

    • The number and types of the primary key columns must match the schema 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.

    • Use AddPrimaryKeyColumnWithMinValue to represent the minimum possible value and AddPrimaryKeyColumnWithMaxValue to represent the maximum possible value for a primary key column.

    EndPrimaryKey (required)

    *PrimaryKey

    The end primary key, including its column names and values.

    • The result set does not include the row that corresponds to the end primary key.

    • The number and types of the primary key columns must match the schema of the table.

    • Use AddPrimaryKeyColumnWithMinValue to represent the minimum possible value and AddPrimaryKeyColumnWithMaxValue to represent the maximum possible value for a primary key column.

    Direction (optional)

    Direction

    The read direction.

    • FORWARD: Performs a forward read. This is the default.

    • BACKWARD: Performs a backward read.

    MaxVersion (optional)

    int32

    The maximum number of versions to return for each column.

    • You must specify either maximum versions or a time range.

    • If the number of data versions that match the query conditions exceeds this value, the specified number of versions is returned in reverse chronological order (from most recent to oldest).

    TimeRange (optional)

    *TimeRange

    The time range of the versions to return.

    • You must specify either maximum versions or a time range.

    • Each attribute column in a Tablestore table can have multiple versions. Setting a time range restricts the returned versions to those within that range.

    Limit (optional)

    int32

    The maximum number of rows to return, which must be greater than 0. If the result set is larger than this value, the response includes a NextStartPrimaryKey to enable pagination.

    ColumnsToGet (optional)

    []string

    The columns to read. These can be primary key columns or attribute columns.

    • If you do not specify this parameter, all columns of the row are returned.

    • If a matching row does not contain any of the specified columns, it is excluded from the results.

    Filter (optional)

    ColumnFilter

    The filter condition. For more information, see Filters.

    • If you specify both ColumnsToGet and a Filter, Tablestore first retrieves the columns specified in ColumnsToGet and then applies the filter.

    TransactionId (optional)

    *string

    The local transaction ID is used to uniquely identify a local transaction. For more information, see Local Transaction.

Sample code

The following sample code reads data from the test_table table where the primary key is greater than row1.

func GetRangeSample(client *tablestore.TableStoreClient) {
    // Build the query criteria.
    rangeRowQueryCriteria := new(tablestore.RangeRowQueryCriteria)
    rangeRowQueryCriteria.TableName = "test_table"
    // Set the start primary key for the query.
    startPK := new(tablestore.PrimaryKey)
    startPK.AddPrimaryKeyColumn("id", "row1")
    rangeRowQueryCriteria.StartPrimaryKey = startPK
    // Set the end primary key for the query. The end primary key is exclusive.
    endPK := new(tablestore.PrimaryKey)
    endPK.AddPrimaryKeyColumnWithMaxValue("id")
    rangeRowQueryCriteria.EndPrimaryKey = endPK
    // Set the maximum versions to read.
    rangeRowQueryCriteria.MaxVersion = 1

    // Call the GetRange method to query data.
    getRangeRequest := new(tablestore.GetRangeRequest)
    getRangeRequest.RangeRowQueryCriteria = rangeRowQueryCriteria
    getRangeResponse, err := client.GetRange(getRangeRequest)

    // Process the response.
    if err != nil {
        fmt.Println("Range get failed with error: ", err)
    } else {
        fmt.Printf("* RequestId: %s \n", getRangeResponse.RequestId)
        fmt.Printf("* Read CU Cost: %d \n", getRangeResponse.ConsumedCapacityUnit.Read)
        fmt.Printf("* Write CU Cost: %d \n", getRangeResponse.ConsumedCapacityUnit.Write)
        fmt.Println("* Rows Data:")
        for _, row := range getRangeResponse.Rows {
            fmt.Printf("PrimaryKey: %v; Columns: ", row.PrimaryKey.PrimaryKeys)
            for _, column := range row.Columns {
                fmt.Printf("%v ", column)
            }
            fmt.Printf("\n")
        }
    }
}

A single range read returns a maximum of 5,000 rows or 4 MB of data. If a result set exceeds this limit, the response includes a NextStartPrimaryKey. Use this key in a subsequent request to retrieve the next page of results. The following code demonstrates this pagination.

for {
    // Call the GetRange method to query data.
    getRangeRequest := new(tablestore.GetRangeRequest)
    getRangeRequest.RangeRowQueryCriteria = rangeRowQueryCriteria
    getRangeResponse, err := client.GetRange(getRangeRequest)
    // Process the response.
    if err != nil {
        fmt.Println("Range get failed with error: ", err)
        break
    } else {
        fmt.Printf("* RequestId: %s \n", getRangeResponse.RequestId)
        fmt.Printf("* Read CU Cost: %d \n", getRangeResponse.ConsumedCapacityUnit.Read)
        fmt.Printf("* Write CU Cost: %d \n", getRangeResponse.ConsumedCapacityUnit.Write)
        fmt.Printf("* Rows Count: %d \n", len(getRangeResponse.Rows))
        fmt.Println("* Rows Data:")
        for _, row := range getRangeResponse.Rows {
            fmt.Printf("PrimaryKey: %v; Columns: ", row.PrimaryKey.PrimaryKeys)
            for _, column := range row.Columns {
                fmt.Printf("%v ", column)
            }
            fmt.Printf("\n")
        }
    }
    // Set the start primary key for the next iteration.
    if getRangeResponse.NextStartPrimaryKey != nil {
        startPK := new(tablestore.PrimaryKey)
        startPK.AddPrimaryKeyColumn("id", getRangeResponse.NextStartPrimaryKey.PrimaryKeys[0].Value)
        rangeRowQueryCriteria.StartPrimaryKey = startPK
    } else {
        break
    }
}

You can also configure the following settings in your query.

  • Set the read direction.

    // Set the direction to backward.
    rangeRowQueryCriteria.Direction = tablestore.BACKWARD
    // Set the start primary key. For a backward read, the start primary key must be greater than the end primary key.
    startPK := new(tablestore.PrimaryKey)
    startPK.AddPrimaryKeyColumnWithMaxValue("id")
    rangeRowQueryCriteria.StartPrimaryKey = startPK
    // Set the end primary key. The end primary key is exclusive.
    endPK := new(tablestore.PrimaryKey)
    endPK.AddPrimaryKeyColumn("id", "row1")
    rangeRowQueryCriteria.EndPrimaryKey = endPK
  • Set a time range to read only versions from that period.

    // Set the time range for the query to the last 24 hours.
    timeRange := new(tablestore.TimeRange)
    timeRange.Start = int64(time.Now().Unix() * 1000 - 86400 * 1000) 
    timeRange.End = int64(time.Now().Unix() * 1000) 
    rangeRowQueryCriteria.TimeRange = timeRange;
  • Specify which attribute columns to return.

    rangeRowQueryCriteria.AddColumnToGet("col1")
  • Limit the number of rows returned in a single request.

    rangeRowQueryCriteria.Limit = 10

Related topics

Batch read