Consulte dados em um índice Lastpoint usando o Tablestore SDK for Go.
Observações de uso
O recurso de índice Lastpoint exige o Tablestore SDK for Go V1.7.15 ou posterior. Atualize o SDK antes de usar esse recurso.
Pré-requisitos
Antes de começar, verifique se você tem:
Um índice Lastpoint criado para uma tabela de séries temporais. Para mais informações, consulte Criar um índice Lastpoint.
Consultar dados de um índice Lastpoint
Um índice Lastpoint armazena o ponto de dados mais recente de cada série temporal. Use a operação GetRange para percorrer todas as entradas e recuperar os valores mais recentes sem varrer toda a tabela de séries temporais.
O exemplo a seguir lê todas as linhas de um índice Lastpoint. Defina MaxVersion como 1, pois o índice Lastpoint aplica a semântica de último ponto. Cada chave primária retém apenas a gravação mais recente; portanto, existe apenas uma versão por linha.
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")
}
Próximos passos
Para ler dados de uma tabela de séries temporais com outros métodos do Tablestore SDK for Go, consulte Ler dados.
Crie um índice de busca no índice Lastpoint para executar consultas mais rápidas, como consulta booleana, busca de texto completo, consulta de prefixo e consulta difusa. Para mais informações, consulte Recuperar um índice Lastpoint.