All Products
Search
Document Center

Simple Log Service:Use Simple Log Service SDK for Node.js to use the Dedicated SQL feature

Last Updated:Sep 18, 2024

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

Prerequisites

  • A Resource Access Management (RAM) user is created, and the required permissions are granted to the RAM user. For more information, see Create a RAM user and grant permissions to the RAM user.

  • The ALIBABA_CLOUD_ACCESS_KEY_ID and ALIBABA_CLOUD_ACCESS_KEY_SECRET environment variables are configured. For more information, see Configure environment variables in Linux, macOS, and Windows.

    Important
    • The AccessKey pair of an Alibaba Cloud account has permissions on all API operations. We recommend that you use the AccessKey pair of a RAM user to call API operations or perform routine O&M.

    • We recommend that you do not save the AccessKey ID or AccessKey secret in your project code. Otherwise, the AccessKey pair may be leaked, and the security of all resources within your account may be compromised.

  • Simple Log Service SDK for Node.js is installed. For more information, see Install Simple Log Service SDK for Node.js.

Background information

Simple Log Service provides the Dedicated SQL feature to enhance SQL analysis capabilities. You can use this feature to process hundreds of billions of data records. 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 provides an example on how to use the Dedicated SQL feature. For more information, see Alibaba Cloud Simple Log Service SDK for Node.js.

const ALY = require('./node_modules/aliyun-sdk/index')

var sls = new ALY.SLS({
    // In this example, the AccessKey ID and AccessKey secret are obtained from environment variables. 
    "accessKeyId": process.env.ALIBABA_CLOUD_ACCESS_KEY_ID,                  
    "secretAccessKey": process.env.ALIBABA_CLOUD_ACCESS_KEY_SECRET, 
    // In this example, the security token is obtained from environment variables. 
    "securityToken" : process.env.ALIYUN_SECURITY_TOKEN,

    // The Simple Log Service endpoint. In this example, the Simple Log Service endpoint for 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 Simple Log Service API. You do not need to modify the parameter. 
    apiVersion: '2015-06-01'

    // Optional. 
    //,httpOptions: {
    //    timeout: 1000  // You can set the parameter to 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 of the time range to query. 
var to = Math.floor(new Date().getTime() / 1000);
// The beginning of the time range to query. 
var from = to - 900; // The value 900 indicates that the query time range is 15 minutes. 

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);
            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 consumed 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 Dedicated SQL feature based on the CPU time. For more information, see the topics that are related to billable items. 
        console.log('success:', data);
    }); 
 sls.executeProjectSql({
    projectName: projectName,
    logStoreName: logStoreName,
    from: from,
    to: to,
    query: 'select count(0) from ' + logStoreName + ' where __time__ > '+ from +' and __time__ < '+to+'',
    powerSql:true
}, function(err, data) {
        if (err) {
            console.log('error:', 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 consumed 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 Dedicated SQL feature based on the CPU time. For more information, see the topics that are related to billable items. 
        console.log('success:', data);
    
    });            

You can call the executeLogStoreSql operation to use the Dedicated SQL feature. You must create a request 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

aliyun-project-test

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 configure the parameter again.

logStoreName

String

Yes

aliyun-logstore-test

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 configure the parameter again.

from

Long

Yes

1718760075

The beginning of the time range to query. The value is a UNIX timestamp representing the number of seconds that have elapsed since January 1, 1970, 00:00:00 UTC.

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

to

Long

Yes

1718788875

The end of the time range to query. The value is a UNIX timestamp representing the number of seconds that have elapsed since January 1, 1970, 00:00:00 UTC.

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

query

String

Yes

'*|select count(method)'

The query statement. Format: Search statement|Analytic statement. For more information, see Syntax.

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

powerSql

Boolean

No

true

Specifies whether to use the Dedicated SQL feature. For more information, see Enable Dedicated SQL. Valid values:

  • true: uses the Dedicated SQL feature.

  • false (default): uses the Standard SQL feature.

You can call the executeProjectSql operation to use the Dedicated SQL feature. You must create a request in the following format:

sls.executeProjectSql({
    projectName: projectName,
    query: 'select count(method) from sls_operation_log where __time__ > to_unixtime(now()) - 300 and __time__ < to_unixtime(now())',
    powerSql:true
})

The following table describes the request parameters.

Parameter

Type

Required

Example

Description

projectName

String

Yes

aliyun-project-test

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 configure the parameter again.

query

String

Yes

select count(method) from sls_operation_log where __time__ > to_unixtime(now()) - 300 and __time__ < to_unixtime(now())

The SQL statement. You must specify the search condition in the WHERE clause of the SQL statement.

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

powerSql

Boolean

No

true

Specifies whether to use the Dedicated SQL feature. For more information, see Enable Dedicated SQL. Valid values:

  • true: uses the Dedicated SQL feature.

  • false (default): uses the Standard SQL feature.