Read a single row

更新时间: 2026-06-22 14:13:09

Read a single row from a Tablestore table with the Go SDK.

Important

Provide a complete primary key for every read, 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: Criteria for a single-row read, with the following subparameters.

    Parameter

    Type

    Description

    TableName (Required)

    string

    Name of the table.

    PrimaryKey (Required)

    *PrimaryKey

    Primary key of the target row, with a name and value for each primary key column.

    • Primary key columns may use STRING, INTEGER, or BINARY.

    • Primary key column count and types must match the table schema.

    MaxVersion (Optional)

    int32

    Maximum number of cell versions to return per column.

    • Specify either this parameter or TimeRange.

    • If more versions exist than the limit allows, only the latest versions are returned in descending timestamp order.

    TimeRange (Optional)

    *TimeRange

    Time range that selects which cell versions to return.

    • Specify either this parameter or MaxVersion.

    • Attribute columns can store multiple versions. Only versions within the specified range are returned.

    ColumnsToGet (Optional)

    []string

    Names of columns to return. Primary key columns and attribute columns are both allowed.

    • If omitted, the response includes the entire row.

    • If the row exists but has none of the requested columns, the response returns the primary key only.

    Filter (Optional)

    ColumnFilter

    Filter condition. See Filter.

    • With both ColumnsToGet and Filter, the Filter runs on the row first. When the row matches, the response returns only columns specified in ColumnsToGet.

    TransactionId (Optional)

    *string

    Local transaction ID. See Local transaction.

Sample code

This example reads one 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 limit results to a version time range, set TimeRange. Only versions in that 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;
  • Return only specific attribute columns.

    criteria.AddColumnToGet("col2")

Related topics

上一篇: Write a single row 下一篇: Update a single row
阿里云首页 表格存储 相关技术圈