This topic describes how to use Log Service SDK for Node.js to use the Dedicated SQL feature.

Prerequisites

Log Service SDK for Node.js of the latest version is installed. For more information, see Install Log Service SDK for Node.js.

Background information

Log Service provides the Dedicated SQL feature to enhance SQL analysis capabilities. You can use this feature to process hundreds of billions of rows of data. For more information, see Enable Dedicated SQL.
Log Service provides the executeLogStoreSql and executeProjectSql operations. You can call these operations to use the Dedicated SQL feature in an efficient manner.
  • executeLogStoreSql: uses the Dedicated SQL feature in a specified Logstore. This API operation supports the SQL-92 syntax. A query statement is in the Search statement|Analytic statement format and the analytic statement follows the standard SQL-92 syntax.
  • executeProjectSql: uses the Dedicated SQL feature in a specified project. This API operation supports the SQL-92 syntax. You must specify a filter condition and time range in the WHERE clause of an SQL statement.
Note If you want to filter data before you analyze the data, we recommend that you call the executeLogStoreSql operation and specify a query statement that is in the Search statement|Analytic statement format. This improves analysis efficiency.

Sample code

The following sample code shows how to use the Dedicated SQL feature. For more information, see Alibaba Cloud Log Service SDK for Node.js.

var ALY = require('../../index.js');

var sls = new ALY.SLS({
    // The AccessKey pair that is used to access Log Service. For more information, see AccessKey pair. An Alibaba Cloud account has permissions to call all API operations. If you use the AccessKey pair of your Alibaba Cloud account, your resources are exposed to high security risks. We recommend that you log on as a RAM user that has permissions to call API operations or perform routine O&M tasks. 
    "accessKeyId": "your_access_id",
    "secretAccessKey": "your_access_key",
    // The Security Token Service (STS) token. For more information, see AssumeRole. 
    "securityToken" :"your_token",

    // The endpoint of Log Service. For more information, see Endpoints. In this example, the endpoint of the China (Hangzhou) region is used. Replace the parameter value with the actual endpoint. 
    endpoint: 'http://cn-hangzhou.log.aliyuncs.com',

    // The version number of the Log Service API. You do not need to modify the value of the parameter. 
    apiVersion: '2015-06-01'

    // Optional settings. 
    //,httpOptions: {
    //    timeout: 1000  // The value 1000 indicates 1 second. By default, the timeout parameter is not specified. 
    //} 
});
module.exports = sls;

// The name of the project. 
var projectName = 'aliyun-test-project';
// The name of the Logstore. 
var logStoreName = 'aliyun-test-logstore';
// The end time of the time range. 
var to = Math.floor(new Date().getTime() / 1000);
// The start time of the time range. 
var from=to-900; // The value 900 indicates that the start time is 15 minutes earlier than the end time. 

sls.executeLogStoreSql({
        projectName: projectName,
        logStoreName: logStoreName,
        from: from,
        to: to, 
        query: '*|select count(method)',
        powerSql:true 
    }, function(err, data) {
        if (err) {
            console.log('error:', err);
            reject(err);
            return;
        }   
        console.log(data);
        console.log('processed rows',data.headers['x-log-processed-rows']); // The number of rows of log data that is processed. 
        console.log('elapsed milli',data.headers['x-log-elapsed-millisecond']);  // The time that is required to execute the SQL statement. 
        console.log('cores',data.headers['x-log-cpu-cores']);     // The number of CPU cores that are used to execute the SQL statement after the Dedicated SQL feature is enabled. 
        console.log('cpuSec',data.headers['x-log-cpu-sec']);      // The CPU time that is consumed to execute the SQL statement after the Dedicated SQL feature is enabled. Unit: seconds. You are charged for the CPU time that is consumed when you use the Dedicated SQL feature to perform query and analysis operations. For more information, see Billable items. 
        // console.log('success:', data);
        resolve(data);
    }); 
            
You can call the executeLogStoreSql operation to use the Dedicated SQL feature. Requests must be in the following format:
sls.executeLogStoreSql({
        projectName: projectName,
        logStoreName: logStoreName,
        from: from,
        to: to, 
        query:  '*|select count(method)',
        powerSql:true 
    })
The following table describes the request parameters.
Parameter Type Required Example Description
projectName String Yes N/A The name of the project.

When you configure basic settings, you must specify a value for the projectName parameter. Therefore, you do not need to set the parameter again.

logStoreName String Yes N/A The name of the Logstore.

When you configure basic settings, you must specify a value for the logStoreName parameter. Therefore, you do not need to set the parameter again.

from Long Yes N/A The start time of the time range that is specified in the request. The start time is a timestamp that follows the UNIX time format. It is the number of seconds that have elapsed since 00:00:00 UTC, Thursday, January 1, 1970.

When you configure basic settings, you must specify a value for the from parameter. Therefore, you do not need to set the parameter again.

to Long Yes N/A The end time of the time range that is specified in the request. The end time is a timestamp that follows the UNIX time format. It is the number of seconds that have elapsed since 00:00:00 UTC, Thursday, January 1, 1970.

When you configure basic settings, you must specify a value for the to parameter. Therefore, you do not need to set the parameter again.

query String Yes '*|select count(method)' The query statement. Format: Search statement|Analytic statement For more information, see Syntax.

By default, Log Service returns 100 rows of data. You can use a LIMIT clause to specify the number of rows of data to return. For more information, see LIMIT syntax.

powerSql Boolean No true Specifies whether to use the Dedicated SQL feature. For more information, see Enable Dedicated SQL.
  • true: The Dedicated SQL feature is used.
  • false: The Standard SQL feature is used. This is the default value.