本文介绍通过C++ SDK使用SQL独享版的代码示例。
前提条件
背景信息
日志服务提供ExecuteLogStoreSql接口和ExecuteProjectSql接口,帮助您更简单的使用SQL独享版。
- ExecuteLogStoreSql接口:在指定Logstore中使用SQL独享版。该接口支持的查询和分析语法兼容标准的SQL92语法,格式为
查询语句|分析语句
,其中分析语句采用标准的SQL92语法。 - ExecuteProjectSql接口:在指定Project中使用SQL独享版。该接口支持的查询和分析语法为标准的SQL92语法,即您的过滤条件和查询时间要写在SQL分析语句的WHERE语句中。
说明 如果您执行分析操作时,需要先过滤一部分数据再分析,建议您使用
查询语句|分析语句
语法,效率更高,即推荐使用ExecuteLogStoreSql接口。
代码示例
代码示例如下,更多信息,请参见Aliyun Log C++ SDK。
//日志服务的服务入口。更多信息,请参见服务入口。此处以杭州为例,其它地域请根据实际情况填写。
std::string endpoint = "cn-hangzhou.log.aliyuncs.com";
//阿里云访问密钥AccessKey。更多信息,请参见访问密钥。阿里云账号AccessKey拥有所有API的访问权限,风险很高。强烈建议您创建并使用RAM用户进行API访问或日常运维。
std::string accessId = "your_access_id";
std::string accessKey = "your_access_key";
//Project名称。
std::string project = "your_project_name";
//Logstore名称。
std::string logStore = "your_logstore";
//创建日志服务Client。
LOGClient client(endpoint, accessId, accessKey);
//在指定的Logstore内执行SQL分析。
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);
//打印计算结果的统计信息。
std::cout << "Returned sql result:" << std::endl
<< "count:" << logsResponse.result.logline << std::endl //计算结果中的行数。
<< "processed rows:" << logsResponse.processedRows << std::endl //处理的日志行数。
<< "elapsed milli:" << logsResponse.elapsedMilli << std::endl //SQL分析执行的时长。
<< "cpu sec:" << logsResponse.cpuSec << std::endl //开启SQL独享版后,执行SQL分析所花费的CPU时间,单位为秒。SQL独享版按照CPU时间计费,更多信息,请参见计费项。
<< "cpu core:" << logsResponse.cpuCore << std::endl; //开启SQL独享版后,执行SQL分析所使用的CPU核数。
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;
}
//在指定的Project内执行SQL分析。
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);
//打印计算结果的统计信息。
std::cout << "Returned sql result:" << std::endl
<< "count:" << logsResponse.result.logline << std::endl //计算结果中的行数。
<< "processed rows:" << logsResponse.processedRows << std::endl //处理的日志行数。
<< "elapsed milli:" << logsResponse.elapsedMilli << std::endl //SQL分析执行的时长。
<< "cpu sec:" << logsResponse.cpuSec << std::endl //开启SQL独享版后,执行SQL分析所花费的CPU时间,单位为秒。SQL独享版按照CPU时间计费,更多信息,请参见计费项。
<< "cpu core:" << logsResponse.cpuCore << std::endl; //开启SQL独享版后,执行SQL分析所使用的CPU核数。
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接口
调用ExecuteLogStoreSql接口使用SQL独享版,格式为
LogStoreSqlResponse logsResponse = client.ExecuteLogStoreSql(project, logStore, from, to, query, powerSql)
,各个参数说明如下表所示。参数名称 类型 是否必选 示例 说明 project String 是 不涉及 Project名称。 在创建Client时,已定义project,此处无需配置。
logStore String 是 不涉及 Logstore名称。 在创建Client时,已定义logStore,此处无需配置。
from Long 是 1627268185 查询起始时间。Unix时间戳格式,表示从1970-1-1 00:00:00 UTC计算起的秒数。 to Long 是 1627269085 查询结束时间点。Unix时间戳格式,表示从1970-1-1 00:00:00 UTC计算起的秒数。 query String 是 "* | SELECT count(*)"
日志服务查询和分析语句,格式为 查询语句|分析语句
。更多信息,请参见基础语法。日志服务默认返回100行结果,您也可以使用LIMIT子句指定返回结果的行数。更多信息,请参见LIMIT子句。
powerSql Boolean 否 true 是否使用SQL独享版。更多信息,请参见开启SQL独享版。 - true:使用SQL独享版。
- false(默认值):使用SQL普通版。
- ExecuteProjectSql接口
调用ExecuteProjectSql接口使用SQL独享版,格式为
ProjectSqlResponse logsResponse = client.ExecuteProjectSql(project, query, powerSql)
,各个参数说明如下表所示。参数名称 类型 是否必选 示例 说明 project String 是 不涉及 Project名称。 在创建Client时,已定义project,此处无需配置。
query String 是 "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"
标准的SQL语句,即您的过滤条件和查询时间要写在SQL分析语句的WHERE语句中。 日志服务默认返回100行结果,您也可以使用LIMIT子句指定返回结果的行数。更多信息,请参见LIMIT子句。
powerSql Boolean 否 true 是否使用SQL独享版。更多信息,请参见开启SQL独享版。 - true:使用SQL独享版。
- false(默认值):使用SQL普通版。