You can execute the select statement to query data in tables.
Note For more information about the select statement, see Query data.
Prerequisites
- A Tablestore client is initialized. For more information, see Initialization.
- A mapping table is created. For more information, see Create mapping tables.
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 set the maximum number of rows that you want to return to 20. The system returns the request type, the schema of the returned results, and the returned results of the query statement.
const client = require('./client');
const params = {
query: "select * from test_table",
}
client.sqlQuery(params, function (err, resp) {
if (err) {
console.log('sqlQuery error:', err.toString());
} else {
console.log('sqlQuery success:', resp);
console.log(resp.sqlRows.rowCount.toFloat64());
console.log(resp.sqlRows.columnCount);
console.log(resp.sqlRows.sqlTableMeta)
for (let i = 0; i < resp.sqlRows.rowCount.toFloat64(); i++) {
for (let j = 0; j < resp.sqlRows.columnCount; j++) {
let data = resp.sqlRows.get(i, j);
// Handle binary type.
if (resp.sqlRows.sqlTableMeta.schemas[j].typeName === "BINARY") {
let int8Array = data.valueArray();
console.log(int8Array);
}
// Handle Long type.
if (resp.sqlRows.sqlTableMeta.schemas[j].typeName === "LONG") {
console.log(data.toFloat64());
}
console.log("i:" + i, ", j:" + j + ":" + data);
}
}
}
});