All Products
Search
Document Center

Tablestore:Query data

Last Updated:Aug 29, 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

Usage notes

Tablestore SDK for .NET V5.0.0 or later supports SQL queries. To perform SQL queries by using Tablestore SDK for .NET, make sure that your SDK version is 5.0.0 or later. We recommend that you use the latest SDK. For more information, see Version history of Tablestore SDK for .NET.

Examples

The following sample code provides an example on how to execute the select pk0,pk1,col0,col1,date_col,geo_col 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.

/// <summary>
/// Query data. 
/// </summary>
/// <param name="otsClient"></param>
public static void QueryData(OTSClient otsClient)
{
    SQLQueryRequest sqlQueryRequest = new SQLQueryRequest("select pk0,pk1,col0,col1,date_col,geo_col from test_table limit 20");

    SQLQueryResponse sqlQueryResponse = otsClient.SQLQuery(sqlQueryRequest);

    SQLTableMeta sqlTableMeta = sqlQueryResponse.GetSQLResultSet().GetSQLTableMeta();
    Console.WriteLine(JsonConvert.SerializeObject(sqlTableMeta.GetSchema()));

    ISQLResultSet resultSet = sqlQueryResponse.GetSQLResultSet();
    while (resultSet.HasNext())
    {
        ISQLRow row = resultSet.Next();
        Console.WriteLine(row.GetString("pk0") + " , " + row.GetLong("pk1") + " , " + row.GetString("col0") + " , " +
                          row.GetLong("col1") + " , " + row.GetString("date_col") + " , " + row.GetString("geo_col"));
    }
}

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 a table.

  • If you want to list the names of mapping tables in the current database, you can execute the SHOW TABLES statement. For more information, see List the names of 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.