文档首页/ 云日志服务 LTS/ API参考/ API说明/ 日志管理/ 查询结构化日志(日志分析)
更新时间:2025-08-07 GMT+08:00
分享

查询结构化日志(日志分析)

功能介绍

通过SQL语句查询指定日志流下的结构化日志内容(日志分析)。

日志分析查询功能支持的局点信息请参考用户指南-日志可视化里的可视化章节。数据聚合结果不超过100亿条。仅支持分析最近30天内的数据,30天以上的数据不支持SQL分析。

调用方法

请参见如何调用API

URI

POST /v2/{project_id}/streams/{log_stream_id}/struct-content/query

表1 路径参数

参数

是否必选

参数类型

描述

project_id

String

参数解释:

项目ID,可以从调用API处获取,也可以从控制台获取。获取方式请参见:获取项目ID

约束限制:

不涉及。

取值范围:

不涉及。

默认取值:

不涉及。

log_stream_id

String

参数解释:

日志流ID,获取方式请参见:获取日志组ID和日志流ID

约束限制:

不涉及。

取值范围:

不涉及。

默认取值:

不涉及。

请求参数

表2 请求Header参数

参数

是否必选

参数类型

描述

X-Auth-Token

String

参数解释:

从IAM服务获取的用户Token,获取方式请参见:获取用户Token

约束限制:

不涉及。

取值范围:

不涉及。

默认取值:

不涉及。

Content-Type

String

参数解释:

用于定义消息体的格式,该字段填为:application/json;charset=utf8。

约束限制:

不涉及。

取值范围:

不涉及。

默认取值:

不涉及。

表3 请求Body参数

参数

是否必选

参数类型

描述

query

String

参数解释:

分析语句。更多信息请参考SQL函数。例如:select time_ceil(__time,'PT1M') as t, count(*) as pv group by t 用于分析查询时间段内每分钟的日志行数。

约束限制:

不涉及。

取值范围:

不涉及。

默认取值:

不涉及。

format

String

参数解释:

整体查询结果的返回格式。

约束限制:

不涉及。

取值范围:

k-v

默认取值:

k-v

time_range

TimeRange object

参数解释:

查询日志的时间范围信息。

约束限制:

结束时间大于起始时间。

时间区间不得超过30天。

当时间区间超过对应日志流的保存时间时,默认查询时间区间与日志保存时间区间的交集。

whether_to_rows

Boolean

参数解释:

返回的数据格式是否为行数据。

约束限制:

不涉及。

取值范围:

  • true:当前返回的数据格式为行数据。

  • false:当前返回的数据格式为列数据。

默认取值:

false

表4 TimeRange

参数

是否必选

参数类型

描述

sql_time_zone

String

参数解释:

SQL语句的时区设置。用处理时间相关函数或时间类型的字段返回时的时区转换。

时区格式有-0800、 -08:00、America/Los_Angeles和UTC等。

例如:select MILLIS_TO_TIMESTAMP(0) as time 表示将毫秒值的时间戳转化为时间类型。

  • 当sql_time_zone取值为UTC 时,time返回的时间格式为1970-01-01T00:00:00.000Z。

  • 当sql_time_zone取值为+0800时,time返回的时间格式1970-01-01T08:00:00.000+08:00。

  • 当sql_time_zone取值为America/Los_Angeles时,time返回的时间格式为1969-12-31T16:00:00.000-08:00。

约束限制:

不涉及。

取值范围:

不涉及。

默认取值:

UTC

start_time

Long

参数解释:

查询起始时间,timestamp时间戳格式,表示从1970-01-01T00:00:00.000Z UTC计算起的毫秒数。

约束限制:

不涉及。

取值范围:

  • 大于0。

  • 当小于日志流保存时间的最小值时,会默认修改为日志流保存时间区间的最小值。

默认取值:

不涉及。

end_time

Long

参数解释:

查询结束时间,timestamp时间戳格式,表示从1970-01-01T00:00:00.000Z UTC计算起的毫秒数。

约束限制:

不涉及。

取值范围:

大于0。

默认取值:

不涉及。

start_time_gt

Boolean

参数解释:

查询是否包含起始时间点。

约束限制:

不涉及。

取值范围:

  • true:包含起始时间点。

  • false:不包含起始时间点。

默认取值:

false。

end_time_lt

Boolean

参数解释:

查询是否包含结束时间点。

约束限制:

不涉及。

取值范围:

  • true:包含结束时间点。

  • false:不包含结束时间点。

默认取值:

false

响应参数

状态码:200

请求响应成功。

状态码:400

表5 响应Body参数

参数

参数类型

描述

error_code

String

参数解释:

错误码,请参见:错误码

取值范围:

不涉及。

error_msg

String

参数解释:

调用失败响应信息描述。

取值范围:

不涉及。

状态码:500

表6 响应Body参数

参数

参数类型

描述

error_code

String

参数解释:

错误码,请参见:错误码

取值范围:

不涉及。

error_msg

String

参数解释:

调用失败响应信息描述。

取值范围:

不涉及。

请求示例

  • 当“query”参数填写为 select *。

    POST https://{endpoint}/v2/{project_id}/streams/{log_stream_id}/struct-content/query
    
    {
      "query" : "select *",
      "format" : "k-v",
      "time_range" : {
        "sql_time_zone" : "UTC",
        "start_time" : 1613806109000,
        "end_time" : 1613806110000,
        "start_time_gt" : false,
        "end_time_lt" : false
      }
    }
  • 当“query”参数填写为select count(test3) as mycount, sum(test4) as mysum。

    POST https://{endpoint}/v2/{project_id}/streams/{log_stream_id}/struct-content/query
    
    {
      "query" : "select count(test3) as mycount, sum(test4) as mysum",
      "format" : "k-v",
      "time_range" : {
        "sql_time_zone" : "UTC",
        "start_time" : 1613806109000,
        "end_time" : 1613806110000,
        "start_time_gt" : false,
        "end_time_lt" : false
      }
    }
  • 当“query”参数填写为 select count(test3), sum(test4) group by(test1)。

    POST https://{endpoint}/v2/{project_id}/streams/{log_stream_id}/struct-content/query
    
    {
      "query" : "select count(test3), sum(test4) group by(test1)",
      "format" : "k-v",
      "time_range" : {
        "sql_time_zone" : "UTC",
        "start_time" : 1613806109000,
        "end_time" : 1613806110000,
        "start_time_gt" : false,
        "end_time_lt" : false
      }
    }
  • 当“whether_to_rows”参数填写为 true。

    POST https://{endpoint}/v2/{project_id}/streams/{log_stream_id}/struct-content/query
    
    {
      "query" : "select count(test3), sum(test4) group by(test1)",
      "format" : "k-v",
      "time_range" : {
        "sql_time_zone" : "UTC",
        "start_time" : 1613806109000,
        "end_time" : 1613806110000,
        "start_time_gt" : false,
        "end_time_lt" : false
      },
      "whether_to_rows" : true
    }

响应示例

状态码:200

请求响应成功。

{
  "pv" : [ 90 ]
}

状态码:400

非法请求。 建议根据error_msg直接修改该请求,不要重试该请求。

{
  "error_code" : "LTS.2022",
  "error_msg" : "Column 'aa' not found in any table"
}

状态码:500

表明服务端能被请求访问到,但是服务内部出错。

{
  "error_code" : "LTS.0010",
  "error_msg" : "Internal Server Error"
}

SDK代码示例

SDK代码示例如下。

  • 当“query”参数填写为 select *。

     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
    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 ListStructuredLogsWithTimeRangeSolution {
    
        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");
            String projectId = "{project_id}";
    
            ICredential auth = new BasicCredentials()
                    .withProjectId(projectId)
                    .withAk(ak)
                    .withSk(sk);
    
            LtsClient client = LtsClient.newBuilder()
                    .withCredential(auth)
                    .withRegion(LtsRegion.valueOf("<YOUR REGION>"))
                    .build();
            ListStructuredLogsWithTimeRangeRequest request = new ListStructuredLogsWithTimeRangeRequest();
            request.withLogStreamId("{log_stream_id}");
            QueryLtsStructLogParamsNew body = new QueryLtsStructLogParamsNew();
            TimeRange timeRangebody = new TimeRange();
            timeRangebody.withSqlTimeZone("UTC")
                .withStartTime(1613806109000L)
                .withEndTime(1613806110000L)
                .withStartTimeGt(false)
                .withEndTimeLt(false);
            body.withTimeRange(timeRangebody);
            body.withFormat("k-v");
            body.withQuery("select *");
            request.withBody(body);
            try {
                ListStructuredLogsWithTimeRangeResponse response = client.listStructuredLogsWithTimeRange(request);
                System.out.println(response.toString());
            } 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());
            }
        }
    }
    
  • 当“query”参数填写为select count(test3) as mycount, sum(test4) as mysum。

     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
    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 ListStructuredLogsWithTimeRangeSolution {
    
        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");
            String projectId = "{project_id}";
    
            ICredential auth = new BasicCredentials()
                    .withProjectId(projectId)
                    .withAk(ak)
                    .withSk(sk);
    
            LtsClient client = LtsClient.newBuilder()
                    .withCredential(auth)
                    .withRegion(LtsRegion.valueOf("<YOUR REGION>"))
                    .build();
            ListStructuredLogsWithTimeRangeRequest request = new ListStructuredLogsWithTimeRangeRequest();
            request.withLogStreamId("{log_stream_id}");
            QueryLtsStructLogParamsNew body = new QueryLtsStructLogParamsNew();
            TimeRange timeRangebody = new TimeRange();
            timeRangebody.withSqlTimeZone("UTC")
                .withStartTime(1613806109000L)
                .withEndTime(1613806110000L)
                .withStartTimeGt(false)
                .withEndTimeLt(false);
            body.withTimeRange(timeRangebody);
            body.withFormat("k-v");
            body.withQuery("select count(test3) as mycount, sum(test4) as mysum");
            request.withBody(body);
            try {
                ListStructuredLogsWithTimeRangeResponse response = client.listStructuredLogsWithTimeRange(request);
                System.out.println(response.toString());
            } 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());
            }
        }
    }
    
  • 当“query”参数填写为 select count(test3), sum(test4) group by(test1)。

     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
    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 ListStructuredLogsWithTimeRangeSolution {
    
        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");
            String projectId = "{project_id}";
    
            ICredential auth = new BasicCredentials()
                    .withProjectId(projectId)
                    .withAk(ak)
                    .withSk(sk);
    
            LtsClient client = LtsClient.newBuilder()
                    .withCredential(auth)
                    .withRegion(LtsRegion.valueOf("<YOUR REGION>"))
                    .build();
            ListStructuredLogsWithTimeRangeRequest request = new ListStructuredLogsWithTimeRangeRequest();
            request.withLogStreamId("{log_stream_id}");
            QueryLtsStructLogParamsNew body = new QueryLtsStructLogParamsNew();
            TimeRange timeRangebody = new TimeRange();
            timeRangebody.withSqlTimeZone("UTC")
                .withStartTime(1613806109000L)
                .withEndTime(1613806110000L)
                .withStartTimeGt(false)
                .withEndTimeLt(false);
            body.withTimeRange(timeRangebody);
            body.withFormat("k-v");
            body.withQuery("select count(test3), sum(test4) group by(test1)");
            request.withBody(body);
            try {
                ListStructuredLogsWithTimeRangeResponse response = client.listStructuredLogsWithTimeRange(request);
                System.out.println(response.toString());
            } 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());
            }
        }
    }
    
  • 当“whether_to_rows”参数填写为 true。

     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
    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 ListStructuredLogsWithTimeRangeSolution {
    
        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");
            String projectId = "{project_id}";
    
            ICredential auth = new BasicCredentials()
                    .withProjectId(projectId)
                    .withAk(ak)
                    .withSk(sk);
    
            LtsClient client = LtsClient.newBuilder()
                    .withCredential(auth)
                    .withRegion(LtsRegion.valueOf("<YOUR REGION>"))
                    .build();
            ListStructuredLogsWithTimeRangeRequest request = new ListStructuredLogsWithTimeRangeRequest();
            request.withLogStreamId("{log_stream_id}");
            QueryLtsStructLogParamsNew body = new QueryLtsStructLogParamsNew();
            TimeRange timeRangebody = new TimeRange();
            timeRangebody.withSqlTimeZone("UTC")
                .withStartTime(1613806109000L)
                .withEndTime(1613806110000L)
                .withStartTimeGt(false)
                .withEndTimeLt(false);
            body.withWhetherToRows(true);
            body.withTimeRange(timeRangebody);
            body.withFormat("k-v");
            body.withQuery("select count(test3), sum(test4) group by(test1)");
            request.withBody(body);
            try {
                ListStructuredLogsWithTimeRangeResponse response = client.listStructuredLogsWithTimeRange(request);
                System.out.println(response.toString());
            } 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());
            }
        }
    }
    
  • 当“query”参数填写为 select *。

     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
    # coding: utf-8
    
    import os
    from huaweicloudsdkcore.auth.credentials import BasicCredentials
    from huaweicloudsdklts.v2.region.lts_region import LtsRegion
    from huaweicloudsdkcore.exceptions import exceptions
    from huaweicloudsdklts.v2 import *
    
    if __name__ == "__main__":
        # 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
        ak = os.environ["CLOUD_SDK_AK"]
        sk = os.environ["CLOUD_SDK_SK"]
        projectId = "{project_id}"
    
        credentials = BasicCredentials(ak, sk, projectId)
    
        client = LtsClient.new_builder() \
            .with_credentials(credentials) \
            .with_region(LtsRegion.value_of("<YOUR REGION>")) \
            .build()
    
        try:
            request = ListStructuredLogsWithTimeRangeRequest()
            request.log_stream_id = "{log_stream_id}"
            timeRangebody = TimeRange(
                sql_time_zone="UTC",
                start_time=1613806109000,
                end_time=1613806110000,
                start_time_gt=False,
                end_time_lt=False
            )
            request.body = QueryLtsStructLogParamsNew(
                time_range=timeRangebody,
                format="k-v",
                query="select *"
            )
            response = client.list_structured_logs_with_time_range(request)
            print(response)
        except exceptions.ClientRequestException as e:
            print(e.status_code)
            print(e.request_id)
            print(e.error_code)
            print(e.error_msg)
    
  • 当“query”参数填写为select count(test3) as mycount, sum(test4) as mysum。

     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
    # coding: utf-8
    
    import os
    from huaweicloudsdkcore.auth.credentials import BasicCredentials
    from huaweicloudsdklts.v2.region.lts_region import LtsRegion
    from huaweicloudsdkcore.exceptions import exceptions
    from huaweicloudsdklts.v2 import *
    
    if __name__ == "__main__":
        # 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
        ak = os.environ["CLOUD_SDK_AK"]
        sk = os.environ["CLOUD_SDK_SK"]
        projectId = "{project_id}"
    
        credentials = BasicCredentials(ak, sk, projectId)
    
        client = LtsClient.new_builder() \
            .with_credentials(credentials) \
            .with_region(LtsRegion.value_of("<YOUR REGION>")) \
            .build()
    
        try:
            request = ListStructuredLogsWithTimeRangeRequest()
            request.log_stream_id = "{log_stream_id}"
            timeRangebody = TimeRange(
                sql_time_zone="UTC",
                start_time=1613806109000,
                end_time=1613806110000,
                start_time_gt=False,
                end_time_lt=False
            )
            request.body = QueryLtsStructLogParamsNew(
                time_range=timeRangebody,
                format="k-v",
                query="select count(test3) as mycount, sum(test4) as mysum"
            )
            response = client.list_structured_logs_with_time_range(request)
            print(response)
        except exceptions.ClientRequestException as e:
            print(e.status_code)
            print(e.request_id)
            print(e.error_code)
            print(e.error_msg)
    
  • 当“query”参数填写为 select count(test3), sum(test4) group by(test1)。

     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
    # coding: utf-8
    
    import os
    from huaweicloudsdkcore.auth.credentials import BasicCredentials
    from huaweicloudsdklts.v2.region.lts_region import LtsRegion
    from huaweicloudsdkcore.exceptions import exceptions
    from huaweicloudsdklts.v2 import *
    
    if __name__ == "__main__":
        # 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
        ak = os.environ["CLOUD_SDK_AK"]
        sk = os.environ["CLOUD_SDK_SK"]
        projectId = "{project_id}"
    
        credentials = BasicCredentials(ak, sk, projectId)
    
        client = LtsClient.new_builder() \
            .with_credentials(credentials) \
            .with_region(LtsRegion.value_of("<YOUR REGION>")) \
            .build()
    
        try:
            request = ListStructuredLogsWithTimeRangeRequest()
            request.log_stream_id = "{log_stream_id}"
            timeRangebody = TimeRange(
                sql_time_zone="UTC",
                start_time=1613806109000,
                end_time=1613806110000,
                start_time_gt=False,
                end_time_lt=False
            )
            request.body = QueryLtsStructLogParamsNew(
                time_range=timeRangebody,
                format="k-v",
                query="select count(test3), sum(test4) group by(test1)"
            )
            response = client.list_structured_logs_with_time_range(request)
            print(response)
        except exceptions.ClientRequestException as e:
            print(e.status_code)
            print(e.request_id)
            print(e.error_code)
            print(e.error_msg)
    
  • 当“whether_to_rows”参数填写为 true。

     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
    # coding: utf-8
    
    import os
    from huaweicloudsdkcore.auth.credentials import BasicCredentials
    from huaweicloudsdklts.v2.region.lts_region import LtsRegion
    from huaweicloudsdkcore.exceptions import exceptions
    from huaweicloudsdklts.v2 import *
    
    if __name__ == "__main__":
        # 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
        ak = os.environ["CLOUD_SDK_AK"]
        sk = os.environ["CLOUD_SDK_SK"]
        projectId = "{project_id}"
    
        credentials = BasicCredentials(ak, sk, projectId)
    
        client = LtsClient.new_builder() \
            .with_credentials(credentials) \
            .with_region(LtsRegion.value_of("<YOUR REGION>")) \
            .build()
    
        try:
            request = ListStructuredLogsWithTimeRangeRequest()
            request.log_stream_id = "{log_stream_id}"
            timeRangebody = TimeRange(
                sql_time_zone="UTC",
                start_time=1613806109000,
                end_time=1613806110000,
                start_time_gt=False,
                end_time_lt=False
            )
            request.body = QueryLtsStructLogParamsNew(
                whether_to_rows=True,
                time_range=timeRangebody,
                format="k-v",
                query="select count(test3), sum(test4) group by(test1)"
            )
            response = client.list_structured_logs_with_time_range(request)
            print(response)
        except exceptions.ClientRequestException as e:
            print(e.status_code)
            print(e.request_id)
            print(e.error_code)
            print(e.error_msg)
    
  • 当“query”参数填写为 select *。

     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
    package main
    
    import (
    	"fmt"
    	"github.com/huaweicloud/huaweicloud-sdk-go-v3/core/auth/basic"
        lts "github.com/huaweicloud/huaweicloud-sdk-go-v3/services/lts/v2"
    	"github.com/huaweicloud/huaweicloud-sdk-go-v3/services/lts/v2/model"
        region "github.com/huaweicloud/huaweicloud-sdk-go-v3/services/lts/v2/region"
    )
    
    func main() {
        // 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
        ak := os.Getenv("CLOUD_SDK_AK")
        sk := os.Getenv("CLOUD_SDK_SK")
        projectId := "{project_id}"
    
        auth := basic.NewCredentialsBuilder().
            WithAk(ak).
            WithSk(sk).
            WithProjectId(projectId).
            Build()
    
        client := lts.NewLtsClient(
            lts.LtsClientBuilder().
                WithRegion(region.ValueOf("<YOUR REGION>")).
                WithCredential(auth).
                Build())
    
        request := &model.ListStructuredLogsWithTimeRangeRequest{}
    	request.LogStreamId = "{log_stream_id}"
    	sqlTimeZoneTimeRange:= "UTC"
    	startTimeGtTimeRange:= false
    	endTimeLtTimeRange:= false
    	timeRangebody := &model.TimeRange{
    		SqlTimeZone: &sqlTimeZoneTimeRange,
    		StartTime: int64(1613806109000),
    		EndTime: int64(1613806110000),
    		StartTimeGt: &startTimeGtTimeRange,
    		EndTimeLt: &endTimeLtTimeRange,
    	}
    	request.Body = &model.QueryLtsStructLogParamsNew{
    		TimeRange: timeRangebody,
    		Format: "k-v",
    		Query: "select *",
    	}
    	response, err := client.ListStructuredLogsWithTimeRange(request)
    	if err == nil {
            fmt.Printf("%+v\n", response)
        } else {
            fmt.Println(err)
        }
    }
    
  • 当“query”参数填写为select count(test3) as mycount, sum(test4) as mysum。

     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
    package main
    
    import (
    	"fmt"
    	"github.com/huaweicloud/huaweicloud-sdk-go-v3/core/auth/basic"
        lts "github.com/huaweicloud/huaweicloud-sdk-go-v3/services/lts/v2"
    	"github.com/huaweicloud/huaweicloud-sdk-go-v3/services/lts/v2/model"
        region "github.com/huaweicloud/huaweicloud-sdk-go-v3/services/lts/v2/region"
    )
    
    func main() {
        // 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
        ak := os.Getenv("CLOUD_SDK_AK")
        sk := os.Getenv("CLOUD_SDK_SK")
        projectId := "{project_id}"
    
        auth := basic.NewCredentialsBuilder().
            WithAk(ak).
            WithSk(sk).
            WithProjectId(projectId).
            Build()
    
        client := lts.NewLtsClient(
            lts.LtsClientBuilder().
                WithRegion(region.ValueOf("<YOUR REGION>")).
                WithCredential(auth).
                Build())
    
        request := &model.ListStructuredLogsWithTimeRangeRequest{}
    	request.LogStreamId = "{log_stream_id}"
    	sqlTimeZoneTimeRange:= "UTC"
    	startTimeGtTimeRange:= false
    	endTimeLtTimeRange:= false
    	timeRangebody := &model.TimeRange{
    		SqlTimeZone: &sqlTimeZoneTimeRange,
    		StartTime: int64(1613806109000),
    		EndTime: int64(1613806110000),
    		StartTimeGt: &startTimeGtTimeRange,
    		EndTimeLt: &endTimeLtTimeRange,
    	}
    	request.Body = &model.QueryLtsStructLogParamsNew{
    		TimeRange: timeRangebody,
    		Format: "k-v",
    		Query: "select count(test3) as mycount, sum(test4) as mysum",
    	}
    	response, err := client.ListStructuredLogsWithTimeRange(request)
    	if err == nil {
            fmt.Printf("%+v\n", response)
        } else {
            fmt.Println(err)
        }
    }
    
  • 当“query”参数填写为 select count(test3), sum(test4) group by(test1)。

     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
    package main
    
    import (
    	"fmt"
    	"github.com/huaweicloud/huaweicloud-sdk-go-v3/core/auth/basic"
        lts "github.com/huaweicloud/huaweicloud-sdk-go-v3/services/lts/v2"
    	"github.com/huaweicloud/huaweicloud-sdk-go-v3/services/lts/v2/model"
        region "github.com/huaweicloud/huaweicloud-sdk-go-v3/services/lts/v2/region"
    )
    
    func main() {
        // 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
        ak := os.Getenv("CLOUD_SDK_AK")
        sk := os.Getenv("CLOUD_SDK_SK")
        projectId := "{project_id}"
    
        auth := basic.NewCredentialsBuilder().
            WithAk(ak).
            WithSk(sk).
            WithProjectId(projectId).
            Build()
    
        client := lts.NewLtsClient(
            lts.LtsClientBuilder().
                WithRegion(region.ValueOf("<YOUR REGION>")).
                WithCredential(auth).
                Build())
    
        request := &model.ListStructuredLogsWithTimeRangeRequest{}
    	request.LogStreamId = "{log_stream_id}"
    	sqlTimeZoneTimeRange:= "UTC"
    	startTimeGtTimeRange:= false
    	endTimeLtTimeRange:= false
    	timeRangebody := &model.TimeRange{
    		SqlTimeZone: &sqlTimeZoneTimeRange,
    		StartTime: int64(1613806109000),
    		EndTime: int64(1613806110000),
    		StartTimeGt: &startTimeGtTimeRange,
    		EndTimeLt: &endTimeLtTimeRange,
    	}
    	request.Body = &model.QueryLtsStructLogParamsNew{
    		TimeRange: timeRangebody,
    		Format: "k-v",
    		Query: "select count(test3), sum(test4) group by(test1)",
    	}
    	response, err := client.ListStructuredLogsWithTimeRange(request)
    	if err == nil {
            fmt.Printf("%+v\n", response)
        } else {
            fmt.Println(err)
        }
    }
    
  • 当“whether_to_rows”参数填写为 true。

     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
    package main
    
    import (
    	"fmt"
    	"github.com/huaweicloud/huaweicloud-sdk-go-v3/core/auth/basic"
        lts "github.com/huaweicloud/huaweicloud-sdk-go-v3/services/lts/v2"
    	"github.com/huaweicloud/huaweicloud-sdk-go-v3/services/lts/v2/model"
        region "github.com/huaweicloud/huaweicloud-sdk-go-v3/services/lts/v2/region"
    )
    
    func main() {
        // 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
        ak := os.Getenv("CLOUD_SDK_AK")
        sk := os.Getenv("CLOUD_SDK_SK")
        projectId := "{project_id}"
    
        auth := basic.NewCredentialsBuilder().
            WithAk(ak).
            WithSk(sk).
            WithProjectId(projectId).
            Build()
    
        client := lts.NewLtsClient(
            lts.LtsClientBuilder().
                WithRegion(region.ValueOf("<YOUR REGION>")).
                WithCredential(auth).
                Build())
    
        request := &model.ListStructuredLogsWithTimeRangeRequest{}
    	request.LogStreamId = "{log_stream_id}"
    	sqlTimeZoneTimeRange:= "UTC"
    	startTimeGtTimeRange:= false
    	endTimeLtTimeRange:= false
    	timeRangebody := &model.TimeRange{
    		SqlTimeZone: &sqlTimeZoneTimeRange,
    		StartTime: int64(1613806109000),
    		EndTime: int64(1613806110000),
    		StartTimeGt: &startTimeGtTimeRange,
    		EndTimeLt: &endTimeLtTimeRange,
    	}
    	whetherToRowsQueryLtsStructLogParamsNew:= true
    	request.Body = &model.QueryLtsStructLogParamsNew{
    		WhetherToRows: &whetherToRowsQueryLtsStructLogParamsNew,
    		TimeRange: timeRangebody,
    		Format: "k-v",
    		Query: "select count(test3), sum(test4) group by(test1)",
    	}
    	response, err := client.ListStructuredLogsWithTimeRange(request)
    	if err == nil {
            fmt.Printf("%+v\n", response)
        } else {
            fmt.Println(err)
        }
    }
    

更多编程语言的SDK代码示例,请参见API Explorer的代码示例页签,可生成自动对应的SDK代码示例。

状态码

状态码

描述

200

请求响应成功。

400

非法请求。 建议根据error_msg直接修改该请求,不要重试该请求。

500

表明服务端能被请求访问到,但是服务内部出错。

错误码

请参见错误码

相关文档