All Products
Search
Document Center

Tablestore:Read a single row

Last Updated:Apr 01, 2026

This topic shows how to use the Go SDK to read a single row from a Tablestore table.

Important

When you read data, you must provide a complete primary key, including the value for any auto-increment primary key column.

Prerequisites

Initialize a Tablestore client

Method

func (tableStoreClient *TableStoreClient) GetRow(request *GetRowRequest) (*GetRowResponse, error)

GetRowRequest parameters

  • SingleRowQueryCriteria (Required)*SingleRowQueryCriteria: The criteria for reading a single row. This parameter contains the following subparameters.

    Parameter

    Type

    Description

    TableName (Required)

    string

    The table name.

    PrimaryKey (Required)

    *PrimaryKey

    The primary key of the row to read. It must include the name and value for each primary key column.

    • The supported data types for primary key columns are STRING, INTEGER, and BINARY.

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

    MaxVersion (Optional)

    int32

    The maximum number of versions to return.

    • You must specify either this parameter or TimeRange.

    • If the number of matching versions exceeds this limit, the service returns the latest versions up to the specified limit, sorted by timestamp in descending order.

    TimeRange (Optional)

    *TimeRange

    The time range of versions to read.

    • You must specify either this parameter or MaxVersion.

    • Each attribute column in a Tablestore table can have multiple versions. If you specify a time range, only versions created within that range are returned.

    ColumnsToGet (Optional)

    []string

    The names of the columns to read. You can specify both primary key columns and attribute columns.

    • If you omit this parameter, the service returns the entire row.

    • If a row exists but contains none of the specified columns, the response includes the primary key but no attribute columns.

    Filter (Optional)

    ColumnFilter

    The filter condition. For more information, see Filter.

    • If you specify both ColumnsToGet and Filter, the Filter is applied to the row first. If the row passes the filter, the response includes only the columns specified in ColumnsToGet.

    TransactionId (Optional)

    *string

    The ID of the local transaction. For more information, see Local transaction.

Sample code

This example shows how to read a single row whose primary key value is "row1".

func GetRowSample(client *tablestore.TableStoreClient) {
    // Construct the primary key.
    getPk := new(tablestore.PrimaryKey)
    getPk.AddPrimaryKeyColumn("id", "row1")

    // Define the query criteria.
    criteria := new(tablestore.SingleRowQueryCriteria)
    criteria.TableName = "test_table"
    criteria.PrimaryKey = getPk
    criteria.MaxVersion = 1

    // Call the GetRow method to read the row.
    getRowRequest := new(tablestore.GetRowRequest)
    getRowRequest.SingleRowQueryCriteria = criteria
    response, err := client.GetRow(getRowRequest)
    if err != nil {
        fmt.Println("Get row failed with error: ", err)
    } else {
        fmt.Printf("RequestId: %s \n", response.RequestId)
        fmt.Printf("Read CU Cost: %d \n", response.ConsumedCapacityUnit.Read)
        fmt.Printf("Write CU Cost: %d \n", response.ConsumedCapacityUnit.Write)
        fmt.Printf("Row Data: %v ", response.PrimaryKey)
        for _, Column := range response.Columns {
            fmt.Printf("%v ", Column)
        }
    }
}
  • To read data from a specific version range, set the TimeRange parameter. Only versions within this range are returned.

    // Set the time range 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) 
    criteria.TimeRange = timeRange;
  • Read specific attribute columns.

    criteria.AddColumnToGet("col2")

Related topics

  • Read data in a range

  • Read data in batches