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

前提条件

  • 已开通日志服务。更多信息,请参见开通日志服务
  • 已创建并获取AccessKey。更多信息,请参见访问密钥

    阿里云账号AccessKey拥有所有API的访问权限,风险很高。强烈建议您创建并使用RAM用户进行API访问或日常运维。RAM用户需具备操作日志服务资源的权限。具体操作,请参见为RAM用户授权

  • 已安装Java开发环境。

    日志服务Java SDK支持JRE 6.0及以上的Java运行环境,您可以执行java -version命令检查您已安装的Java版本。如果未安装,可以从Java官方网站下载安装包并完成安装。

  • 已安装0.6.68及以上版本的Java SDK。更多信息,请参见安装Java SDK

背景信息

日志服务提供SQL独享版,用于增强SQL分析能力,支持千亿行数据的计算。更多信息,请参见开启SQL独享版
日志服务提供executeLogStoreSql接口和executeProjectSql接口,帮助您更简单的使用SQL独享版。
  • executeLogStoreSql接口:在指定Logstore中使用SQL独享版。该接口支持的查询和分析语法兼容标准的SQL92语法,格式为查询语句|分析语句,其中分析语句采用标准的SQL92语法。
  • executeProjectSql接口:在指定Project中使用SQL独享版。该接口支持的查询和分析语法为标准的SQL92语法,即您的过滤条件和查询时间要写在SQL分析语句的WHERE语句中。
说明 如果您执行分析操作时,需要先过滤一部分数据再分析,建议您使用查询语句|分析语句语法,效率更高,即推荐使用executeLogStoreSql接口。

示例代码

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

import com.aliyun.openservices.log.Client;
import com.aliyun.openservices.log.common.LogContent;
import com.aliyun.openservices.log.common.LogItem;
import com.aliyun.openservices.log.common.QueriedLog;
import com.aliyun.openservices.log.exception.LogException;
import com.aliyun.openservices.log.response.GetLogsResponse;

import java.util.Date;

public class SlsSample {

    public static void main(String args[]) throws LogException,
            InterruptedException {
        //阿里云访问密钥AccessKey。更多信息,请参见访问密钥。阿里云账号AccessKey拥有所有API的访问权限,风险很高。强烈建议您创建并使用RAM用户进行API访问或日常运维。
        String accessId = "your_access_id";
        String accessKey = "your_access_key";
        //Project名称。
        String project = "aliyun-test-project";
        //日志服务的服务入口。更多信息,请参见服务入口。此处以杭州为例,其它地域请根据实际情况填写。
        String host = "cn-hangzhou.log.aliyuncs.com"; 
        //Logstore名称。
        String logStore = "aliyun-test-logstore";
        //Shard ID。
        int shardId = 0;
        
        //创建日志服务Client。
        Client client = new Client(host, accessId, accessKey);

        //在指定的Logstore内执行SQL分析。
        try {
            String sql = "* | select count(1)";
            int from = (int) (new Date().getTime() / 1000 - 600);
            int to = (int) (new Date().getTime() / 1000);
            GetLogsResponse logsResponse = client.executeLogstoreSql(project, logStore, 1627268185, 1627269085, "* | SELECT count(*)", true);
            System.out.println("Returned sql result count:" + logsResponse.GetCount());
            for (QueriedLog log : logsResponse.getLogs()) {
                LogItem item = log.GetLogItem();
                System.out.println("time : " + item.mLogTime);
                for (LogContent content : item.mContents) {
                    System.out.println(content.mKey + ":" + content.mValue);
                }
            }
            //打印计算结果的统计信息。
            //处理的日志行数。
            System.out.println("proccesedRows:" + logsResponse.getProcessedRow());
            //SQL分析执行的时长。
            System.out.println("elapsedMilli:" + logsResponse.getElapsedMilliSecond());
            //开启SQL独享版后,执行SQL分析所花费的CPU时间,单位为秒。SQL独享版按照CPU时间计费,更多信息,请参见计费项。
            System.out.println("cpuSec:" + logsResponse.getCpuSec());
            //开启SQL独享版后,执行SQL分析所使用的CPU核数。
            System.out.println("cpuCores:" + logsResponse.getCpuCores());
            System.out.println("requestId:" + logsResponse.GetRequestId());

        } catch (LogException e) {
            System.out.println("error code :" + e.GetErrorCode());
            System.out.println("error message :" + e.GetErrorMessage());
            throw e;
        }

        //在指定的Project内执行SQL分析。
        try {
            int now = (int) (new Date().getTime() / 1000);
            String sql = "select count(1) as cnt from xxx where __time__ > " + now;
            GetLogsResponse 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);
            System.out.println("Returned sql result count:" + logsResponse.GetCount());
            for (QueriedLog log : logsResponse.getLogs()) {
                LogItem item = log.GetLogItem();
                for (LogContent content : item.mContents) {
                    System.out.println(content.mKey + ":" + content.mValue);
                }
            }
            //打印计算结果的统计信息。
            //处理的日志行数。
            System.out.println("proccesedRows:" + logsResponse.getProcessedRow());
            //SQL分析执行的时长。
            System.out.println("elapsedMilli:" + logsResponse.getElapsedMilliSecond());
            //开启SQL独享版后,执行SQL分析所花费的CPU时间,单位为秒。SQL独享版按照CPU时间计费,更多信息,请参见计费项。
            System.out.println("cpuSec:" + logsResponse.getCpuSec());
            //开启SQL独享版后,执行SQL分析所使用的CPU核数。
            System.out.println("cpuCores:" + logsResponse.getCpuCores());
            System.out.println("requestId:" + logsResponse.GetRequestId());
        } catch (LogException e) {
            System.out.println("error code :" + e.GetErrorCode());
            System.out.println("error message :" + e.GetErrorMessage());
            throw e;
        }
    }
}
  • executeLogStoreSql接口
    调用executeLogStoreSql接口使用SQL独享版,格式为GetLogsResponse logsResponse = client.executeLogstoreSql(project, logStore, from, to, query, powerSql),各个参数说明如下表所示。
    参数名称类型是否必选示例说明
    projectString不涉及Project名称。

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

    logStoreString不涉及Logstore名称。

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

    fromLong1627268185查询起始时间。Unix时间戳格式,表示从1970-1-1 00:00:00 UTC计算起的秒数。
    toLong1627269085查询结束时间点。Unix时间戳格式,表示从1970-1-1 00:00:00 UTC计算起的秒数。
    queryString"* | SELECT count(*)"日志服务查询和分析语句,格式为查询语句|分析语句。更多信息,请参见基础语法

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

    powerSqlBooleantrue是否使用SQL独享版。更多信息,请参见开启SQL独享版
    • true:使用SQL独享版。
    • false(默认值):使用SQL普通版。
  • executeProjectSql接口
    调用executeProjectSql接口使用SQL独享版,格式为GetLogsResponse logsResponse = client.executeProjectSql(project, query, powerSql),各个参数说明如下表所示。
    参数名称类型是否必选示例说明
    projectString不涉及Project名称。

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

    queryString"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子句

    powerSqlBooleantrue是否使用SQL独享版。更多信息,请参见开启SQL独享版
    • true:使用SQL独享版。
    • false(默认值):使用SQL普通版。

相关文档

  • 在调用API接口过程中,若服务端返回结果中包含错误信息,则表示调用API接口失败。您可以参考API错误码对照表查找对应的解决方法。更多信息,请参见API错误处理对照表
  • 阿里云OpenAPI开发者门户提供调试、SDK、示例和配套文档。通过OpenAPI,您无需手动封装请求和签名操作,就可以快速对日志服务API进行调试。更多信息,请参见OpenAPI开发者门户
  • 为满足越来越多的自动化日志服务配置需求,日志服务提供命令行工具CLI(Command Line Interface)。更多信息,请参见日志服务命令行工具CLI
  • 更多示例代码,请参见Aliyun Log Java SDK on GitHub