Use Simple Log Service SDK for PHP to run SQL queries with the Dedicated SQL feature.
Prerequisites
-
Simple Log Service is activated. 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 on all API operations. Using these credentials to perform operations in Simple Log Service is a high-risk operation. We recommend that you use a Resource Access Management (RAM) user to call API operations or perform routine O&M. To create a RAM user, log on to the RAM console. Make sure that the RAM user has the management permissions on Simple Log Service resources. For more information, see Create a RAM user and grant permissions.
-
Simple Log Service SDK for PHP is installed. For more information, see Install the PHP SDK.
Background information
The Dedicated SQL feature enhances SQL analysis capabilities and can process hundreds of billions of data records. For more information, see High-performance fully accurate query and analysis (Dedicated SQL).
Simple Log Service provides the following operations for the Dedicated SQL feature:
-
Aliyun_Log_Models_LogStoreSqlRequest: runs Dedicated SQL queries in a specified Logstore. This operation supports the standard SQL-92 syntax. A query statement is in the
Search statement|Analytic statementformat, and the analytic statement follows the standard SQL-92 syntax. -
Aliyun_Log_Models_ProjectSqlRequest: runs Dedicated SQL queries in a specified project. This operation supports the standard SQL-92 syntax. You must specify a filter condition and a time range in the WHERE clause of an SQL statement.
To filter data before analysis, we recommend that you call the Aliyun_Log_Models_LogStoreSqlRequest operation and specify a query statement in the Search statement|Analytic statement format to improve analysis efficiency.
Sample code
The following sample code shows how to use the Dedicated SQL feature. For more information, see Alibaba Cloud Simple Log Service SDK for PHP.
<?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 Simple Log Service endpoint for the China (Hangzhou) region is used. Replace the parameter value with the actual endpoint.
$endpoint = 'cn-hangzhou.log.aliyuncs.com';
// In this example, the AccessKey ID and AccessKey secret are obtained from environment variables.
$accessKeyId = getenv('ALIBABA_CLOUD_ACCESS_KEY_ID');
$accessKey = getenv('ALIBABA_CLOUD_ACCESS_KEY_SECRET');
// The name of the project.
$project = 'aliyun-test-project';
// The name of the Logstore.
$logstore = 'aliyun-test-logstore';
// The Security Token Service (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, $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 required 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 the topics that are related to 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 required 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 the topics that are related to 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
You can call the Aliyun_Log_Models_LogStoreSqlRequest operation to use the Dedicated SQL feature. The request format is as follows:
$from = time()-3600; $to = time(); $query = "* | select count(0)"; $topic = ""; $request = new Aliyun_Log_Models_LogStoreSqlRequest($project,$logstore,$from,$to,$topic,$query,$powerSql);The following table describes the parameters.
Parameter
Type
Required
Example
Description
$project
String
Yes
N/A
The project name. Specified when the Simple Log Service client is created. You do not need to configure it again.
$logstore
String
Yes
N/A
The Logstore name. Specified when the Simple Log Service client is created. You do not need to configure it again.
$from
Long
Yes
time()-3600
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.
$to
Long
Yes
time()
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.
$topic
String
No
topic-test
The topic of logs.
$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 High-performance fully accurate query and analysis (Dedicated SQL). Valid values:
-
true: uses the Dedicated SQL feature.
-
false (default): uses the Standard SQL feature.
-
-
Aliyun_Log_Models_ProjectSqlRequest operation
You can call the Aliyun_Log_Models_ProjectSqlRequest operation to use the Dedicated SQL feature. The request format is as follows:
$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 following table describes the parameters.
Parameter
Type
Required
Example
Description
$project
String
Yes
N/A
The project name. Specified when the Simple Log Service client is created. You do not need to configure it 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 and time range 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 High-performance fully accurate query and analysis (Dedicated SQL). Valid values:
-
true: uses the Dedicated SQL feature.
-
false (default): uses the Standard SQL feature.
-