Utilize o Tablestore SDK for Go para consultar dados por meio da execução de instruções SQL.
Pré-requisitos
Um cliente deve estar inicializado. Para mais informações, consulte Inicializar um cliente do Tablestore.
Uma tabela de mapeamento deve existir para a tabela que você deseja consultar. Para mais detalhes, veja Crie uma tabela de mapeamento.
Parâmetros
|
Parâmetro |
Descrição |
|
query |
A instrução SQL a ser executada. |
Exemplos
O exemplo abaixo executa uma instrução SELECT na tabela test_table e retorna até 20 linhas. A resposta inclui o tipo de instrução, o esquema do conjunto de resultados e os dados das linhas.
func queryData(client *tablestore.TableStoreClient) {
// Build the SQL request.
request := &tablestore.SQLQueryRequest{Query: "select pk, long_value, double_value, string_value, bool_value from test_table limit 20"}
// Execute the SQL request.
response, err := client.SQLQuery(request)
if err != nil {
panic(err)
}
// Print the statement type.
fmt.Printf("response type: %v\n", response.StmtType.String())
// Print the result set schema.
columns := response.ResultSet.Columns()
fmt.Printf("response table meta: [")
for i := 0; i < len(columns); i++ {
fmt.Printf("%v:%v ", columns[i].Name, columns[i].Type.String())
}
// Iterate over the result set rows.
resultSet := response.ResultSet
fmt.Println("]\nresponse resultset:")
for resultSet.HasNext() {
row := resultSet.Next()
stringValue, _ := row.GetString(0)
fmt.Printf("%v, ", stringValue)
stringValue, _ = row.GetStringByName("pk")
fmt.Printf("%v, ", stringValue)
longValue, _ := row.GetFloat64(1)
fmt.Printf("%v, ", longValue)
longValue, _ = row.GetFloat64ByName("long_value")
fmt.Printf("%v, ", longValue)
floatValue, _ := row.GetFloat64(2)
fmt.Printf("%v, ", floatValue)
floatValue, _ = row.GetFloat64ByName("double_value")
fmt.Printf("%v, ", floatValue)
stringValue, _ = row.GetString(3)
fmt.Printf("%v, ", stringValue)
stringValue, _ = row.GetStringByName("string_value")
fmt.Printf("%v, ", stringValue)
boolValue, _ := row.GetBool(4)
fmt.Printf("%v, ", boolValue)
boolValue, _ = row.GetBoolByName("bool_value")
fmt.Printf("%v\n", boolValue)
}
}
Exemplo de resposta:
response type: SQL_SELECT
response table meta: [pk:STRING long_value:INTEGER double_value:DOUBLE string_value:STRING bool_value:BOOLEAN]
response resultset:
binary_null, binary_null, 0, 0, 1, 1, a, a, false, false
bool_null, bool_null, 0, 0, 1, 1, a, a, false, false
double_null, double_null, 0, 0, 1, 1, a, a, false, false
long_null, long_null, 0, 0, 1, 1, a, a, false, false
string_null, string_null, 0, 0, 1, 1, , , false, false