更新时间:2024-10-14 GMT+08:00
分享

分页显示查询分析结果

用户在查询分析日志时,查询分析结果内容过多会影响显示速度和查询体验。云日志服务LTS支持分页显示查询分析结果,可控制每次返回的日志数量。本文介绍查询结果和分析结果的分页方法。

分页方式

云日志服务的日志搜索和统计图表功能支持在查询分析语句中同时实现关键字查询和针对查询结果进行SQL分析。

  • 日志搜索:使用关键字查询,获取原始日志内容。您可以通过查询日志接口中的line_num、search_type、limit和is_desc参数实现多种形式的翻页效果。更多信息请参见查询日志
  • 统计图表:使用SQL对查询结果进行分析,获取统计结果。您可以通过SQL中的LIMIT语法实现分页。更多信息请参见SQL查询语法概述

日志搜索分页

查询日志接口中的line_num、search_type、limit和is_desc参数说明如下:

  • line_num:日志单行序列号,第一次查询时不需要此参数,后续分页查询时需要使用,可从上次查询的返回信息中获取。line_num应在start_time 和end_time之间。 若已开启自定义时间功能,在使用该字段的同时,还需要增加__time__字段共同进行分页查询。
  • search_type:首次查询为 “init”, 分页查询时为 “forwards”向前翻页或者“backwards”向后翻页, 默认为首次查询“init”, 与 is_desc 参数配合进行分页查询。
  • limit:表示每次查询的日志条数,不填时默认为50,建议您设置为100。
  • is_desc:顺序或者倒序查询,默认为false(顺序查询)。

在分页读取时,从上一次的查询结果中获取line_num,设置search_type决定是向前翻页还是向后翻页,设定每次查询的日志条数limit,决定日志的时间排序is_desc。并且结果的isQueryComplete为true状态,则表示读取了所有数据。

  • Java示例代码参考如下,更多信息请参考云日志服务Java SDK(标准插件)
     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    package com.huaweicloud.sdk.test;
    import com.huaweicloud.sdk.core.auth.ICredential;
    import com.huaweicloud.sdk.core.auth.BasicCredentials;
    import com.huaweicloud.sdk.core.exception.ConnectionException;
    import com.huaweicloud.sdk.core.exception.RequestTimeoutException;
    import com.huaweicloud.sdk.core.exception.ServiceResponseException;
    import com.huaweicloud.sdk.lts.v2.region.LtsRegion;
    import com.huaweicloud.sdk.lts.v2.*;
    import com.huaweicloud.sdk.lts.v2.model.*;
    public class ListLogsSolution  {
        public static void main(String[] args) {
            // The AK and SK used for authentication are hard-coded or stored in plaintext, which has great security risks. It is recommended that the AK and SK be stored in ciphertext in configuration files or environment variables and decrypted during use to ensure security.
            // In this example, AK and SK are stored in environment variables for authentication. Before running this example, set environment variables CLOUD_SDK_AK and CLOUD_SDK_SK in the local environment
            String ak = System.getenv("CLOUD_SDK_AK");
            String sk = System.getenv("CLOUD_SDK_SK");
            ICredential auth = new BasicCredentials()
                .withAk(ak)
                .withSk(sk);
            LtsClient client = LtsClient.newBuilder()
                .withCredential(auth)
                .withRegion(LtsRegion.valueOf("region"))
                .build();
            ListLogsRequest request = new ListLogsRequest();
            request.withLogGroupId("group_id");
            request.withLogStreamId("stream_id");
            QueryLtsLogParams body = new QueryLtsLogParams();
            // 每次返回的原始日志条数
            body.withLimit(3);
            // 向前翻页或向后翻页 第一次查询可不填或填默认值init
            body.withSearchType(QueryLtsLogParams.SearchTypeEnum.fromValue("init"));
            // 是否按时间倒序
            body.withIsDesc(false);
            // 查询结束时间
            body.withEndTime("1727429160000");
            // 查询开始时间
            body.withStartTime("1727428260000");
            // 是否返回结果条数
            body.withIsCount(true);
            request.withBody(body);
            while (true) {
                try {
                    ListLogsResponse response = client.listLogs(request);
                    System.out.println(response.toString());
                    // 如果日志条数等于limit,说明还有日志没有查询完,需要继续查询
                    if (response.getCount() < 3) {
                        return;
                    }
                    // 获取查询结果中最后一条日志的单行序号,作为查询起始的序号
                    body.withLineNum(response.getLogs().get(2).getLineNum());
                    // 向前翻页或向后翻页 forwards为向前翻页
                    body.withSearchType(QueryLtsLogParams.SearchTypeEnum.fromValue("forwards"));
                } catch (ConnectionException e) {
                    e.printStackTrace();
                } catch (RequestTimeoutException e) {
                    e.printStackTrace();
                } catch (ServiceResponseException e) {
                    e.printStackTrace();
                    System.out.println(e.getHttpStatusCode());
                    System.out.println(e.getRequestId());
                    System.out.println(e.getErrorCode());
                    System.out.println(e.getErrorMsg());
                }
            }
        }
    }
    

统计图表分页

您可以使用SQL中的Limit语法实现分析结果分析显示。

1
limit num offset num2

示例如下所示:

1
2
* | select hostid,podname  limit 0
* | select hostid,podname  limit 500 offset 500

参数说明如下所示:

  • offset:指定从某一行开始读取分析结果。
  • limit:指定当前请求读取的行数,最大值为100,000。如果一次读取太多,会影响网络延时和客户端的处理速度。

例如,通过* | select hostid,podname limit 20 offset 10 语句进行查询分析,指定返回100行日志。您可以通过分页指定每次读取20行,共5次读取完成,示例如下:

  • Java示例代码参考如下,更多信息请参考云日志服务Java SDK(标准插件)
    package com.huaweicloud.sdk.test;
    import com.huaweicloud.sdk.core.auth.ICredential;
    import com.huaweicloud.sdk.core.auth.BasicCredentials;
    import com.huaweicloud.sdk.core.exception.ConnectionException;
    import com.huaweicloud.sdk.core.exception.RequestTimeoutException;
    import com.huaweicloud.sdk.core.exception.ServiceResponseException;
    import com.huaweicloud.sdk.lts.v2.region.LtsRegion;
    import com.huaweicloud.sdk.lts.v2.*;
    import com.huaweicloud.sdk.lts.v2.model.*;
    public class ListLogsSolution {
        public static void main(String[] args) {
            // The AK and SK used for authentication are hard-coded or stored in plaintext, which has great security risks. It is recommended that the AK and SK be stored in ciphertext in configuration files or environment variables and decrypted during use to ensure security.
            // In this example, AK and SK are stored in environment variables for authentication. Before running this example, set environment variables CLOUD_SDK_AK and CLOUD_SDK_SK in the local environment
            String ak = System.getenv("CLOUD_SDK_AK");
            String sk = System.getenv("CLOUD_SDK_SK");
            ICredential auth = new BasicCredentials()
                .withAk(ak)
                .withSk(sk);
            LtsClient client = LtsClient.newBuilder()
                .withCredential(auth)
                .withRegion(LtsRegion.valueOf("region"))
                .build();
            ListLogsRequest request = new ListLogsRequest();
            request.withLogGroupId("group_id");
            request.withLogStreamId("stream_id");
            QueryLtsLogParams body = new QueryLtsLogParams();
            body.withIsAnalysisQuery(true);
            // offset为查询偏移量记录,第一次查询设定为0,limit为返回结果的条数,最大值为10000。
            long offset = 0;
            long limit = 3;
            // 修改sql语句中的偏移量
            body.withQuery(String.format("* | select hostid,podname limit %s offset %s",limit,offset));
            body.withEndTime("1727429160000");
            body.withStartTime("1727428260000");
            request.withBody(body);
            while (true) {
                try {
                    ListLogsResponse response = client.listLogs(request);
                    System.out.println(response.toString());
                    if (response.getAnalysisLogs().size() < 3) {
                        return;// 分页查询,如果返回结果为空,则表示查询完成,跳出循环。
                    }
                    offset = offset + limit;
                    body.withQuery(String.format("* | select hostid,podname limit %s offset %s",limit,offset));
                } catch (ConnectionException e) {
                    e.printStackTrace();
                } catch (RequestTimeoutException e) {
                    e.printStackTrace();
                } catch (ServiceResponseException e) {
                    e.printStackTrace();
                    System.out.println(e.getHttpStatusCode());
                    System.out.println(e.getRequestId());
                    System.out.println(e.getErrorCode());
                    System.out.println(e.getErrorMsg());
                }
            }
        }
    }

相关文档