Range read

更新时间: 2026-06-22 15:06:46

Read a range of rows from a Tablestore table by using the Go SDK.

Prerequisites

Initialize Tablestore Client

Method signature

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

GetRangeRequest parameters

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

    Parameter

    Type

    Description

    TableName (required)

    string

    The name of the table.

    StartPrimaryKey (required)

    *PrimaryKey

    The start primary key, including column names and values.

    • The result set includes the row matching 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 column names and values.

    • The result set does not include the row matching 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

    Scan direction.

    • FORWARD: Forward read. This is the default.

    • BACKWARD: Backward read.

    MaxVersion (optional)

    int32

    Maximum number of versions to return per column.

    • Specify either maximum versions or a time range.

    • If more versions match, only the newest versions up to this count are returned.

    TimeRange (optional)

    *TimeRange

    Time range of versions to return.

    • Specify either maximum versions or a time range.

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

    Limit (optional)

    int32

    Maximum number of rows per response. Must be greater than 0. When more rows match, the response includes NextStartPrimaryKey for the next page.

    ColumnsToGet (optional)

    []string

    Columns to read. You can specify primary key columns, attribute columns, or both.

    • If you omit this parameter, all columns of the row are returned.

    • If ColumnsToGet is set and a row contains none of the specified columns, null is returned for that row.

    Filter (optional)

    ColumnFilter

    Column filter expression. For details, see Filters.

    • When both ColumnsToGet and Filter are set, Tablestore first narrows rows using ColumnsToGet, then applies Filter.

    TransactionId (optional)

    *string

    Local transaction ID. For more information, see Local Transaction.

Sample code

The following example reads rows 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 number of versions to read per column.
    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")
        }
    }
}

Each GetRange response returns at most 5,000 rows or 4 MB of data. When the result exceeds that limit, the response includes a NextStartPrimaryKey for the next read. Use the following loop to iterate until all rows are read.

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 apply the following optional 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 return only versions within that window.

    // 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;
  • Return only the specified attribute columns.

    rangeRowQueryCriteria.AddColumnToGet("col1")
  • Limit the number of rows returned per request.

    rangeRowQueryCriteria.Limit = 10

References

Batch read data

上一篇: Delete a single row 下一篇: Batch read data
阿里云首页 表格存储 相关技术圈