Read a single row
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
Method
func (tableStoreClient *TableStoreClient) GetRow(request *GetRowRequest) (*GetRowResponse, error)
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")