All Products
Search
Document Center

Tablestore:Query data

Last Updated:Jun 14, 2024

After you create a mapping table for a table or search index, you can execute the SELECT statement to query and analyze data by using the mapping table.

Note

For more information about the SELECT statement, see Query data.

Prerequisites

Parameters

Parameter

Description

query

The SQL statement. Configure the parameter based on the required feature.

Examples

The following sample code provides an example on how to execute the select pk, long_value, double_value, string_value, bool_value from test_table limit 20 statement to query data in the table named test_table and return up to 20 rows of data. The system returns the request type, the schema of the returned results, and the returned results of the query statement.

func queryData(client *tablestore.TableStoreClient) {
    // Create an SQL request. 
    request := &tablestore.SQLQueryRequest{Query: "select pk, long_value, double_value, string_value, bool_value from test_table limit 20"}

    // Obtain the response to the SQL request. 
    response, err := client.SQLQuery(request)
    if err != nil {
        panic(err)
    }

    // Obtain the SQL request type. 
    fmt.Printf("response type: %v\n", response.StmtType.String())

    // Obtain the schema of the returned results of the SQL request. 
    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())
    }

    // Obtain the returned results of the SQL request. 
    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)
    }
}

Sample response:

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

FAQ

References

  • If you want to accelerate data queries and computing by executing SQL statements, you can create a secondary index or a search index. For more information, see Index selection policy and Computing pushdown.

  • If an attribute column is added to or removed from a data table, you can execute the ALTER TABLE statement to modify the mapping table that is created for the data table. For more information, see Update attribute columns of mapping tables.

  • If you want to query the description of a table, you can execute the DESCRIBE statement. For more information, see Query information about a table.

  • If you no longer require a mapping table that is created for a table or a search index, you can execute the DROP MAPPING TABLE statement to delete the mapping table. For more information, see Delete mapping tables.

  • If you want to view the index information about a table, you can execute the SHOW INDEX statement. For more information, see Query index information about tables.

  • If you want to query the names of mapping tables in the current database, you can execute the SHOW TABLES statement. For more information, see Query the names of mapping tables.

  • You can also use computing engines, such as MaxCompute, Spark, Hive, HadoopMR, Function Compute, Flink, and PrestoDB, to compute and analyze data in tables. For more information, see Overview.

  • If your business requires multi-dimensional queries and data analysis, you can create a search index and specify the required attributes as the fields of the search index. Then, you can use the search index to query and analyze data. For example, you can use a search index to perform queries based on non-primary key columns, Boolean queries, and fuzzy queries. You can also use a search index to obtain the maximum and minimum values, collect statistics about the number of rows, and group query results. For more information, see Search indexes.