This topic describes how to use Simple Log Service SDK for C++ 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.
ImportantThe 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 C++ of the latest version is installed. For more information, see Alibaba Cloud Simple Log Service SDK for C++.
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 rows of data. For more information, see Enable Dedicated SQL.
Simple 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 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 operation supports the SQL-92 syntax. You must specify a filter condition and a time range in the WHERE clause of an SQL statement.
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 Simple Log Service SDK for C++.
#include <cstdlib>
// 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.
std::string endpoint = "cn-hangzhou.log.aliyuncs.com";
// Configure environment variables. In this example, the AccessKey ID and AccessKey secret are obtained from environment variables.
string accessId = (string)getenv("ALIBABA_CLOUD_ACCESS_KEY_ID");
string accessKey = (string)getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET");
// The name of the project.
std::string project = "your_project_name";
// The name of the Logstore.
std::string logStore = "your_logstore";
// Create a Simple Log Service client.
LOGClient client(endpoint, accessId, accessKey);
// Execute an SQL statement in the specified Logstore.
try
{
std::string sql = "* | select count(1)";
int from = time(NULL) - 600;
int to = from + 600;
LogStoreSqlResponse logsResponse = client.ExecuteLogStoreSql(project, logStore,
1627268185,1627269085,"* | SELECT count(*)",true);
// Print the statistics of the analysis result.
std::cout << "Returned sql result:" << std::endl
<< "count:" << logsResponse.result.logline << std::endl // The number of rows of log data that is returned in the analysis result.
<< "processed rows:" << logsResponse.processedRows << std::endl // The number of rows of log data that is processed.
<< "elapsed milli:" << logsResponse.elapsedMilli << std::endl // The time that is required to execute the SQL statement.
<< "cpu sec:" << logsResponse.cpuSec << std::endl // 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.
<< "cpu core:" << logsResponse.cpuCore << std::endl; // The number of CPU cores that are used to execute the SQL statement after the Dedicated SQL feature is enabled.
for (std::vector<LogItem>::const_iterator itr = logsResponse.result.logdatas.begin();
itr != logsResponse.result.logdatas.end(); ++itr)
{
const LogItem &item = *itr;
for (std::vector<std::pair<std::string, std::string>>::const_iterator itr_data = item.data.begin();
itr_data != item.data.end(); ++itr_data)
{
std::cout << itr_data->first << ":" << itr_data->second;
}
}
}
catch (LOGException &e)
{
std::cout << "error code :" << e.GetErrorCode() << std::endl;
std::cout << "error message :" << e.GetMessage() << std::endl;
throw e;
}
// Execute an SQL statement in the specified project.
try
{
int now = time(NULL);
std::string sql = "select count(1) as cnt from xxx where __time__ > " + to_string(now);
ProjectSqlResponse logsResponse = client.ExecuteProjectSql(project,"select avg(latency),max(latency) ,count(1) as c from sample-logstore where status>200 and __time__>=1500975424 and __time__ < 1501035044 GROUP BY method ORDER BY c",true);
// Print the statistics of the analysis result.
std::cout << "Returned sql result:" << std::endl
<< "count:" << logsResponse.result.logline << std::endl // The number of rows of log data that is returned in the analysis result.
<< "processed rows:" << logsResponse.processedRows << std::endl // The number of rows of log data that is processed.
<< "elapsed milli:" << logsResponse.elapsedMilli << std::endl // The time that is required to execute the SQL statement.
<< "cpu sec:" << logsResponse.cpuSec << std::endl // 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.
<< "cpu core:" << logsResponse.cpuCore << std::endl; // The number of CPU cores that are used to execute the SQL statement after the Dedicated SQL feature is enabled.
for (std::vector<LogItem>::const_iterator itr = logsResponse.result.logdatas.begin(); itr != logsResponse.result.logdatas.end(); ++itr)
{
const LogItem &item = *itr;
for (std::vector<std::pair<std::string, std::string>>::const_iterator itr_data = item.data.begin();
itr_data != item.data.end(); ++itr_data)
{
std::cout << itr_data->first << ":" << itr_data->second;
}
}
}
catch (LOGException &e)
{
std::cout << "error code :" << e.GetErrorCode() << std::endl;
std::cout << "error message :" << e.GetMessage() << std::endl;
throw e;
}
ExecuteLogStoreSql operation
You can call the ExecuteLogStoreSql operation to use the Dedicated SQL feature. Requests must be in the
LogStoreSqlResponse logsResponse = client.ExecuteLogStoreSql(project, logStore, from, to, query, powerSql)
format. The following table describes the request parameters.Parameter
Type
Required
Example
Description
project
String
Yes
N/A
The name of the project.
When you create a Simple Log Service client, you must specify a value for the project parameter. Therefore, you do not need to configure the parameter again.
logStore
String
Yes
N/A
The name of the Logstore.
When you create a Simple Log Service client, you must specify a value for the logStore parameter. Therefore, you do not need to configure the parameter again.
from
Long
Yes
1627268185
The beginning of the time range to 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
1627269085
The end of the time range to 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.
query
String
Yes
"* | SELECT count(*)"
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 rows of data to return. For more information, see LIMIT clause.
powerSql
Boolean
No
true
Specifies whether to use Dedicated SQL. For more information, see Enable Dedicated SQL. Valid values:
true: uses Dedicated SQL.
false (default): uses Standard SQL.
ExecuteProjectSql operation
You can call the ExecuteProjectSql operation to use the Dedicated SQL feature. Requests must be in the
ProjectSqlResponse logsResponse = client.ExecuteProjectSql(project, query, powerSql)
format. The following table describes the request parameters.Parameter
Type
Required
Example
Description
project
String
Yes
N/A
The name of the project.
When you create a Simple Log Service client, you must specify a value for the project parameter. Therefore, you do not need to configure the parameter again.
query
String
Yes
"select avg(latency),max(latency) ,count(1) as c from sample-logstore where status>200 and __time__>=1500975424 and __time__ < 1501035044 GROUP BY method ORDER BY c"
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 rows of data to return. For more information, see LIMIT clause.
powerSql
Boolean
No
true
Specifies whether to use Dedicated SQL. For more information, see Enable Dedicated SQL. Valid values:
true: uses Dedicated SQL.
false (default): uses Standard SQL.