This topic describes how to use Tablestore SDK for Node.js to query data by executing SQL statements.
Prerequisites
A client is initialized. For more information, see Initialize a Tablestore client.
A mapping table is created. For more information, see Create a mapping table.
Parameters
Parameter | Description |
query | The SQL statement. Configure this parameter based on the required feature. |
Example
The following sample code provides an example on how to query all data in the table named test_table:
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);
// Process data of the Binary type.
if (resp.sqlRows.sqlTableMeta.schemas[j].typeName === "BINARY") {
let int8Array = data.valueArray();
console.log(int8Array);
}
// Process data of the Long type.
if (resp.sqlRows.sqlTableMeta.schemas[j].typeName === "LONG") {
console.log(data.toFloat64());
}
console.log("i:" + i, ", j:" + j + ":" + data);
}
}
}
});