You can execute the select statement to query data in tables.
Prerequisites
- A Tablestore client is initialized. For more information, see Initialization.
- A mapping table is created. For more information, see Create tables and mapping tables.
Usage notes
Tablestore SDK for Java V5.13.0 or later supports the SQL query feature. Before you use the SQL query feature, make sure that Tablestore SDK for Java V5.13.0 or later is obtained. For more information about the version history of Tablestore SDK for Java, see Tablestore SDK for Java.
Parameters
Parameter | Description |
---|---|
query | The SQL statement. Configure the parameter based on the required feature. |
Examples
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 specify the maximum number
of returned rows to 20. The system returns the request type, the schema of the returned
results, and the returned results of the query statement.
private static void queryData(SyncClient client) {
// Create a SQL request.
SQLQueryRequest request = new SQLQueryRequest("select pk, long_value, double_value, string_value, bool_value from test_table limit 20");
// Obtain the response to the SQL request.
SQLQueryResponse response = client.sqlQuery(request);
// Obtain the SQL request type.
System.out.println("response type: " + response.getSQLStatementType());
// Obtain the schema of the returned results of the SQL request.
SQLTableMeta tableMeta = response.getSQLResultSet().getSQLTableMeta();
System.out.println("response table meta: " + tableMeta.getSchema());
// Obtain the returned results of the SQL request.
SQLResultSet resultSet = response.getSQLResultSet();
System.out.println("response resultset:");
while (resultSet.hasNext()) {
SQLRow row = resultSet.next();
System.out.println(row.getString(0) + ", " + row.getString("pk") + ", " +
row.getLong(1) + ", " + row.getLong("long_value") + ", " +
row.getDouble(2) + ", " + row.getDouble("double_value") + ", " +
row.getString(3) + ", " + row.getString("string_value") + ", " +
row.getBoolean(4) + ", " + row.getBoolean("bool_value"));
}
}
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, 1, 1, 1.0, 1.0, a, a, false, false
bool_null, bool_null, 1, 1, 1.0, 1.0, a, a, null, null
double_null, double_null, 1, 1, null, null, a, a, true, true
long_null, long_null, null, null, 1.0, 1.0, a, a, true, true
string_null, string_null, 1, 1, 1.0, 1.0, null, null, false, false