文档

使用GetHistograms查询日志分布数量

更新时间:

本文通过代码示例介绍如何使用GetHistograms接口查询日志在某时间区间中的分布数量。

前提条件

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

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

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

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

  • 已安装日志服务Java SDK。具体操作,请参见安装Java SDK

  • 已写入日志到Logstore并开启索引。具体操作,请参见创建Logstore示例代码创建索引

注意事项

本示例以华东1(杭州)的公网Endpoint为例,其公网Endpoint为https://cn-hangzhou.log.aliyuncs.com。如果您通过与Project同地域的其他阿里云产品访问日志服务,请使用内网Endpointhttps://cn-hangzhou-intranet.log.aliyuncs.com。关于日志服务支持的地域与Endpoint的对应关系,请参见服务入口

原始日志样例

body_bytes_sent:1750
host:www.example.com
http_referer:www.example.com
http_user_agent:Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10_6_6; it-it) AppleWebKit/533.20.25 (KHTML, like Gecko) Version/5.0.4 Safari/533.20.27
http_x_forwarded_for:203.0.XX.XX
remote_addr:203.0.XX.XX
remote_user:p288
request_length:13741
request_method:GET
request_time:71
request_uri:/request/path-1/file-1
http_code:200
time_local:11/Aug/2021:06:52:27
upstream_response_time:0.66

示例代码

以下代码用于查询一小时内来自47.100.XX.XX的访问次数。

import com.aliyun.openservices.log.Client;
import com.aliyun.openservices.log.common.Histogram;
import com.aliyun.openservices.log.exception.LogException;
import com.aliyun.openservices.log.response.GetHistogramsResponse;

public class GetHistograms {
    public static void main(String[] args) throws LogException {
        // 本示例从环境变量中获取AccessKey ID和AccessKey Secret。
        String accessId = System.getenv("ALIBABA_CLOUD_ACCESS_KEY_ID");
        String accessKey = System.getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET");
        // 输入Project名称。
        String projectName = "ali-test-project";
        // 日志服务的服务接入点。此处以杭州为例,其它地域请根据实际情况填写。
        String host = "https://cn-hangzhou.log.aliyuncs.com";

        // 创建日志服务Client。
        Client client = new Client(host, accessId, accessKey);

        try {
            // 输入Logstore名称。
            String logstoreName = "ali-test-logstore";

            // 查询一小时内来自47.100.XX.XX的访问次数。
            String query = "remote_addr:47.100.XX.XX";
            System.out.println(String.format("ready to get histograms from %s",logstoreName));

            // fromTime和toTime表示查询日志的时间范围,Unix时间戳格式。查询区间为一个小时。
            int fromTime = (int) (System.currentTimeMillis()/1000 - 3600);
            int toTime = fromTime + 3600;

            GetHistogramsResponse response = client.GetHistograms(projectName,logstoreName,fromTime,toTime,"",query);
            for (Histogram histogram : response.GetHistograms()) {
                // 由于区间较多,只输出有日志分布的区间。
                if (0 < histogram.GetCount()){
                    // 输出日志数量。
                    System.out.println("log number is :" + histogram.GetCount());
                    // 日志数量分布的开始时间。
                    System.out.println("from time is :" + histogram.GetFrom());
                    // 日志数量分布的结束时间。
                    System.out.println("to time is :" + histogram.GetTo());
                    // 该时间区间内的结果是否完整。
                    System.out.println("is completed :" + histogram.IsCompleted());
                }
            }
            System.out.println(String.format("get histograms from %s success",logstoreName));

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

预期结果如下。该查询结果说明一小时内来自47.100.XX.XX的访问次数为10,查询结果完整。

ready to get histograms from nginx-moni
log number is :10
from time is :1667875920
to time is :1667879520
is completed :true
get histograms from nginx-moni success

相关文档

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

  • 更多示例代码,请参见Aliyun Log Java SDK on GitHub

  • 本页导读 (1)
文档反馈