全部产品
Search
文档中心

日志服务:通过PHP SDK使用SQL独享版

更新时间:Oct 26, 2023

本文介绍通过PHP SDK使用SQL独享版的代码示例。

前提条件

  • 已创建RAM用户并完成授权。具体操作,请参见创建RAM用户并完成授权

  • 已配置环境变量ALIBABA_CLOUD_ACCESS_KEY_IDALIBABA_CLOUD_ACCESS_KEY_SECRET。具体操作,请参见配置环境变量

    重要
    • 阿里云账号的AccessKey拥有所有API的访问权限,建议您使用RAM用户的AccessKey进行API访问或日常运维。

    • 强烈建议不要把AccessKey ID和AccessKey Secret保存到工程代码里,否则可能导致AccessKey泄露,威胁您账号下所有资源的安全。

  • 已完成PHP SDK安装。更多信息,请参见安装PHP SDK

背景信息

日志服务提供SQL独享版,用于增强SQL分析能力,支持千亿行数据的计算。更多信息,请参见开启SQL独享版

日志服务提供Aliyun_Log_Models_LogStoreSqlRequest接口和Aliyun_Log_Models_ProjectSqlRequest接口,帮助您更简单的使用SQL独享版。

  • Aliyun_Log_Models_LogStoreSqlRequest接口:在指定Logstore中使用SQL独享版。该接口支持的查询和分析语法兼容标准的SQL92语法,格式为查询语句|分析语句,其中分析语句采用标准的SQL92语法。

  • Aliyun_Log_Models_ProjectSqlRequest接口:在指定Project中使用SQL独享版。该接口支持的查询和分析语法为标准的SQL92语法,即您的过滤条件和查询时间要写在SQL分析语句的WHERE语句中。

说明

如果您执行分析操作时,需要先过滤一部分数据再分析,建议您使用查询语句|分析语句语法,效率更高,即推荐使用Aliyun_Log_Models_LogStoreSqlRequest接口。

代码示例

代码示例如下,更多信息,请参见Aliyun Log PHP SDK

// 日志服务的服务入口。此处以杭州为例,其它地域请根据实际情况填写。
$endpoint = 'cn-hangzhou.log.aliyuncs.com';
// 本示例从环境变量中获取AccessKey ID和AccessKey Secret。
$accessKeyId = getenv('ALIBABA_CLOUD_ACCESS_KEY_ID');        
$accessKey = getenv('ALIBABA_CLOUD_ACCESS_KEY_SECRET'); 
// Project名称。
$project = 'aliyun-test-project';
// Logstore名称。
$logstore = 'aliyun-test-logstore';
// STS token。
$token = "your_token";
// 创建日志服务Client。
$client = new Aliyun_Log_Client($endpoint, $accessKeyId, $accessKey,$token);

// 在指定的Logstore内执行SQL分析。  
$from = time()-3600;
$to = time();
$query = "* | select count(method)";
$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";
    } 
   // 打印计算结果的统计信息。
   // 处理的日志行数。
    print "proccesedRows:".$response -> getProcessedRows()."\n";
    // SQL分析执行的时长。
    print "elapsedMilli:".$response -> getElapsedMilli()."\n";
    // 开启SQL独享版后,执行SQL分析所花费的CPU时间,单位为秒。SQL独享版按照CPU时间计费,更多信息,请参见计费项。
    print "cpuSec:".$response -> getCpuSec()."\n";
    // 开启SQL独享版后,执行SQL分析所使用的CPU核数。
    print "cpuCores:".$response -> getCpuCores()."\n";

} catch (Aliyun_Log_Exception $ex) {
 logVarDump($ex);
} catch (Exception $ex) {
 logVarDump($ex);
}   

// 在指定的Project内执行SQL分析。
$query = "select count(method) from sls_operation_log 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";
  }
  // 打印计算结果的统计信息。
  // 处理的日志行数。
  print "proccesedRows:".$response -> getProcessedRows()."\n";
  // SQL分析执行的时长。
  print "elapsedMilli:".$response -> getElapsedMilli()."\n";
  // 开启SQL独享版后,执行SQL分析所花费的CPU时间,单位为秒。SQL独享版按照CPU时间计费,更多信息,请参见计费项。
  print "cpuSec:".$response -> getCpuSec()."\n";
  // 开启SQL独享版后,执行SQL分析所使用的CPU核数。
  print "cpuCores:".$response -> getCpuCores()."\n";
  print "requestId:".$response ->getRequestId()."\n";

} catch (Aliyun_Log_Exception $ex) {
 logVarDump($ex);
} catch (Exception $ex) {
   logVarDump($ex);
}
  • Aliyun_Log_Models_LogStoreSqlRequest接口

    调用Aliyun_Log_Models_LogStoreSqlRequest接口使用SQL独享版,接口格式为:

    $from = time()-3600;
    $to = time();
    $query = "* | select count(method)";
    $request = new Aliyun_Log_Models_LogStoreSqlRequest($project,$logstore,$from,$to,$query,$powerSql);

    各个参数说明如下表所示。

    参数名称

    类型

    是否必选

    示例

    说明

    $project

    String

    不涉及

    Project名称。

    在创建Client时,已定义$project,此处无需配置。

    $logstore

    String

    不涉及

    Logstore名称。

    在创建Client时,已定义$logstore,此处无需配置。

    $from

    Long

    time()-3600

    查询起始时间。Unix时间戳格式,表示从1970-1-1 00:00:00 UTC计算起的秒数。

    $to

    Long

    time()

    查询结束时间点。Unix时间戳格式,表示从1970-1-1 00:00:00 UTC计算起的秒数。

    $query

    String

    "* | select count(method)"

    日志服务查询和分析语句,格式为查询语句|分析语句。更多信息,请参见基础语法

    日志服务默认返回100行结果,您也可以使用LIMIT子句指定返回结果的行数。更多信息,请参见LIMIT子句

    $powerSql

    Boolean

    true

    是否使用SQL独享版。更多信息,请参见开启SQL独享版

    • true:使用SQL独享版。

    • false(默认值):使用SQL普通版。

  • Aliyun_Log_Models_ProjectSqlRequest接口

    调用Aliyun_Log_Models_ProjectSqlRequest接口使用SQL独享版,接口格式如下:

    $query = "select count(method) from sls_operation_log where __time__ > to_unixtime(now()) - 300 and __time__ < to_unixtime(now())";
    $request = new Aliyun_Log_Models_ProjectSqlRequest($project,$query,$powerSql);   

    各个参数说明如下表所示。

    参数名称

    类型

    是否必选

    示例

    说明

    $project

    String

    不涉及

    Project名称。

    在创建Client时,已定义$project,此处无需配置。

    $query

    String

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

    标准的SQL语句,即您的过滤条件和查询时间要写在SQL分析语句的WHERE语句中。

    日志服务默认返回100行结果,您也可以使用LIMIT子句指定返回结果的行数。更多信息,请参见LIMIT子句

    $powerSql

    Boolean

    true

    是否使用SQL独享版。更多信息,请参见开启SQL独享版

    • true:使用SQL独享版。

    • false(默认值):使用SQL普通版。