Query data in a Lastpoint index by using Tablestore SDK for Go.
Usage notes
The Lastpoint index feature requires Tablestore SDK for Go V1.7.15 or later. Upgrade your SDK before using this feature.
Prerequisites
Before you begin, ensure that you have:
A Lastpoint index created for a time series table. For more information, see Create a Lastpoint index
Query data from a Lastpoint index
A Lastpoint index stores the most recent data point for each time series. Use the GetRange operation to scan all entries and retrieve the latest values without scanning the entire time series table.
The following example reads all rows from a Lastpoint index. Set MaxVersion to 1 because a Lastpoint index applies last-point semantics — each primary key retains only the most recent write, so only one version exists per row.
func GetRange(client *tablestore.TableStoreClient, lastpointName string) {
getRangeRequest := &tablestore.GetRangeRequest{}
rangeRowQueryCriteria := &tablestore.RangeRowQueryCriteria{}
rangeRowQueryCriteria.TableName = lastpointName
startPK := new(tablestore.PrimaryKey)
startPK.AddPrimaryKeyColumnWithMinValue("_#h")
startPK.AddPrimaryKeyColumnWithMinValue("_m_name")
startPK.AddPrimaryKeyColumnWithMinValue("_data_source")
startPK.AddPrimaryKeyColumnWithMinValue("_tags")
endPK := new(tablestore.PrimaryKey)
endPK.AddPrimaryKeyColumnWithMaxValue("_#h")
endPK.AddPrimaryKeyColumnWithMaxValue("_m_name")
endPK.AddPrimaryKeyColumnWithMaxValue("_data_source")
endPK.AddPrimaryKeyColumnWithMaxValue("_tags")
rangeRowQueryCriteria.StartPrimaryKey = startPK
rangeRowQueryCriteria.EndPrimaryKey = endPK
rangeRowQueryCriteria.Direction = tablestore.FORWARD
// Set MaxVersion to 1. A Lastpoint index uses last-point semantics,
// so each primary key retains only the most recent write.
rangeRowQueryCriteria.MaxVersion = 1
getRangeRequest.RangeRowQueryCriteria = rangeRowQueryCriteria
getRangeResp, err := client.GetRange(getRangeRequest)
fmt.Println("get range result is ", getRangeResp)
for {
if err != nil {
fmt.Println("get range failed with error:", err)
}
for _, row := range getRangeResp.Rows {
fmt.Println("range get row with key", row.PrimaryKey.PrimaryKeys[0].Value, row.PrimaryKey.PrimaryKeys[1].Value, row.PrimaryKey.PrimaryKeys[2].Value)
}
if getRangeResp.NextStartPrimaryKey == nil {
break
} else {
fmt.Println("next pk is :", getRangeResp.NextStartPrimaryKey.PrimaryKeys[0].Value, getRangeResp.NextStartPrimaryKey.PrimaryKeys[1].Value, getRangeResp.NextStartPrimaryKey.PrimaryKeys[2].Value)
getRangeRequest.RangeRowQueryCriteria.StartPrimaryKey = getRangeResp.NextStartPrimaryKey
getRangeResp, err = client.GetRange(getRangeRequest)
}
fmt.Println("continue to query rows")
}
fmt.Println("range get row finished")
}
What's next
To read data from a time series table using other Tablestore SDK for Go methods, see Read data.
To run faster queries against the Lastpoint index — such as Boolean query, full-text search, prefix query, and fuzzy query — create a search index on the Lastpoint index. For more information, see Retrieve a Lastpoint index.