This topic explains how to utilize the Dedicated SQL feature using the Simple Log Service SDK for PHP.
Prerequisites
Simple Log Service is activated. For more information, see Activate Simple Log Service.
-
An AccessKey pair is created and obtained. For more information, see AccessKey pair.
The AccessKey pair of an Alibaba Cloud account has permissions for all API operations. Using these credentials for operations in Simple Log Service is high-risk. It is strongly recommended to perform API operations or routine O&M as a RAM user with the necessary Simple Log Service management permissions. For more information, see Grant permissions to a RAM user.
-
Simple Log Service SDK for PHP is installed. For more information, see Install Simple Log Service SDK for PHP.
Background information
Simple Log Service enhances SQL analysis capabilities with the Dedicated SQL feature, capable of processing hundreds of billions of rows of data. For more information, see Overview of the Dedicated SQL feature.
Simple Log Service offers Aliyun_Log_Models_LogStoreSqlRequest and Aliyun_Log_Models_ProjectSqlRequest operations for efficient use of the Dedicated SQL feature.
-
Aliyun_Log_Models_LogStoreSqlRequest utilizes the Dedicated SQL feature within a specified Logstore. This operation adheres to the SQL-92 syntax, employing a format of
query statement|analytic statement
, with the analytic statement conforming to the SQL-92 standards. -
Aliyun_Log_Models_ProjectSqlRequest: This operation enables the use of the Dedicated SQL feature within a specified project and supports SQL-92 syntax. An SQL statement must include a filter condition and a time range in the WHERE clause.
To filter data prior to analysis for enhanced efficiency, it is recommended to employ the query statement|analytic statement
syntax. Consequently, the use of the Aliyun_Log_Models_LogStoreSqlRequest operation is advised.
Sample code
Below is the sample code. For more information, see Aliyun Log PHP SDK.
<?PHP
require_once realpath(dirname(__FILE__) . './aliyun-log-php-sdk-master/Log_Autoload.php');
class test
{
public static function main()
{
// The Simple Log Service endpoint. In this example, the endpoint for the China (Hangzhou) region is used. Replace this parameter value with your actual endpoint.
$endpoint = 'cn-hangzhou.log.aliyuncs.com';
// Obtain an AccessKey ID and an AccessKey secret from environment variables.
$accessKeyId = getenv('ALIBABA_CLOUD_ACCESS_KEY_ID');
$accessKey = getenv('ALIBABA_CLOUD_ACCESS_KEY_SECRET');
// The project name.
$project = 'aliyun-test-project';
// The Logstore name.
$logstore = 'aliyun-test-logstore';
// The STS token.
$token = '';
// Create a Simple Log Service client.
$client = new Aliyun_Log_Client($endpoint, $accessKeyId, $accessKey, $token);
// Execute an SQL statement in the specified Logstore.
$from = time() - 3600;
$to = time();
$query = "* | select count(0)";
$topic = "";
$request = new Aliyun_Log_Models_LogStoreSqlRequest($project, $logstore, $from, $to, $topic, $query, true);
try {
$response = $client->executeLogStoreSql($request);
foreach ($response->getLogs() as $log) {
print $log->getTime() . "\t";
foreach ($log->getContents() as $key => $value) {
print $key . ":" . $value . "\t";
}
print "\n";
}
// Display the statistics about the analysis results.
// The number of lines of log data that is processed.
print "proccesedRows:" . $response->getProcessedRows() . "\n";
// The time that is consumed to execute the SQL statement.
print "elapsedMilli:" . $response->getElapsedMilli() . "\n";
// 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 Billable items.
print "cpuSec:" . $response->getCpuSec() . "\n";
// The number of CPU cores that are used to execute the SQL statement after the Dedicated SQL feature is enabled.
print "cpuCores:" . $response->getCpuCores() . "\n";
} catch (Aliyun_Log_Exception $ex) {
logVarDump($ex);
} catch (Exception $ex) {
logVarDump($ex);
}
// Execute an SQL statement in the specified project.
$query = "select count(0) from gs-api where __time__ > to_unixtime(now()) - 300 and __time__ < to_unixtime(now())";
$request = new Aliyun_Log_Models_ProjectSqlRequest($project, $query, True);
try {
$response = $client->executeProjectSql($request);
#$response = $client->getProjectLogs($request);
foreach ($response->getLogs() as $log) {
print $log->getTime() . "\t";
foreach ($log->getContents() as $key => $value) {
print $key . ":" . $value . "\t";
}
print "\n";
}
// Display the statistics about the analysis results.
// The number of lines of log data that is processed.
print "proccesedRows:" . $response->getProcessedRows() . "\n";
// The time that is consumed to execute the SQL statement.
print "elapsedMilli:" . $response->getElapsedMilli() . "\n";
// 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 Billable items.
print "cpuSec:" . $response->getCpuSec() . "\n";
// The number of CPU cores that are used to execute the SQL statement after the Dedicated SQL feature is enabled.
print "cpuCores:" . $response->getCpuCores() . "\n";
print "requestId:" . $response->getRequestId() . "\n";
} catch (Aliyun_Log_Exception $ex) {
logVarDump($ex);
} catch (Exception $ex) {
logVarDump($ex);
}
}
}
test::main();
-
Aliyun_Log_Models_LogStoreSqlRequest Operation
To use the Dedicated SQL feature, call the Aliyun_Log_Models_LogStoreSqlRequest operation in the following format:
$from = time()-3600; $to = time(); $query = "* | select count(0)"; $topic = ""; $request = new Aliyun_Log_Models_LogStoreSqlRequest($project,$logstore,$from,$to,$topic,$query,$powerSql);
The parameters are described in the table below.
Parameter name
Type
Required
Example
Description
$project
String
Yes
N/A
The project name.
The $project parameter is defined when you create a client. You do not need to configure this parameter.
$logstore
String
Yes
N/A
The Logstore name.
The $logstore parameter is defined when you create a client. You do not need to configure this parameter.
$from
Long
Yes
time()-3600
The start time of the query. The value 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.
$to
Long
Yes
time()
The end time of the time range that is specified in the request. The value 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.
$topic
String
No
topic-test
The topic of the log.
$query
String
Yes
"* | select count(method)"
The query and analysis statement of Simple Log Service. The format is
query statement|analytic statement
. For more information, see Basic 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 Overview of the Dedicated SQL feature.
true: uses the Dedicated SQL feature.
false: uses the Standard SQL feature. This is the default value.
-
Aliyun_Log_Models_ProjectSqlRequest Operation
To use the Dedicated SQL feature, call the Aliyun_Log_Models_ProjectSqlRequest operation in the following format:
$query = "select count(0) from sls_operation_log where __time__ > to_unixtime(now()) - 300 and __time__ < to_unixtime(now())"; $request = new Aliyun_Log_Models_ProjectSqlRequest($project,$query,$powerSql);
The parameters are described in the table below.
Parameter name
Type
Required
Example
Description
$project
String
Yes
N/A
The project name.
The $project parameter is defined when you create a client. You do not need to configure this parameter.
$query
String
Yes
" select count(method) from sls_operation_log where __time__ > to_unixtime(now()) - 300 and __time__ < to_unixtime(now())"
An SQL statement in which a filter condition and a time range must be specified in the WHERE clause.
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 enable the Dedicated SQL feature. For more information, see Overview of the Dedicated SQL feature.
true: uses the Dedicated SQL feature.
false: uses the Standard SQL feature. This is the default value.