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
Method
func (tableStoreClient *TableStoreClient) GetRow(request *GetRowRequest) (*GetRowResponse, error)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
TimeRangeparameter. 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