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

查询结构化日志(新版)

功能介绍

该接口用于查询指定日志流下的结构化日志内容(新版)。

新版查询功能支持的局点信息请参考用户指南-日志可视化里的可视化章节。数据聚合结果不超过100亿条。仅支持分析30天内的数据,30天以上的数据不支持SQL分析。如您有更大的使用需求,请提工单申请。

调用方法

请参见如何调用API

URI

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

表1 路径参数

参数

是否必选

参数类型

描述

project_id

String

项目ID,获取方式请参见:获取项目ID,获取账号ID,日志组ID、日志流ID

缺省值:None

最小长度:32

最大长度:32

log_stream_id

String

日志组ID,获取方式请参见:获取项目ID,获取账号ID,日志组ID、日志流ID

缺省值:None

最小长度:36

最大长度:36

请求参数

表2 请求Header参数

参数

是否必选

参数类型

描述

X-Auth-Token

String

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

缺省值:None

最小长度:1000

最大长度:2000

Content-Type

String

该字段填为:application/json;charset=UTF-8。

缺省值:None

最小长度:30

最大长度:30

表3 请求Body参数

参数

是否必选

参数类型

描述

query

String

sql语句字符串。

最小长度:0

最大长度:102400

format

String

查询结果格式。当前仅支持:"k-v"。

time_range

TimeRange object

时间范围信息。

whether_to_rows

Boolean

返回数据格式,是否为行数据,默认为false。

表4 TimeRange

参数

是否必选

参数类型

描述

sql_time_zone

String

时区信息,默认为“UTC”。

start_time

Long

搜索起始时间(UTC时间,毫秒级)。

end_time

Long

搜索结束时间(UTC时间,毫秒级)。

start_time_gt

Boolean

搜索是否包含起始时间点,默认为false。

end_time_lt

Boolean

搜索是否包含结束时间点,默认为false。

响应参数

状态码: 400

表5 响应Body参数

参数

参数类型

描述

message

message object

接口调用信息。

状态码: 401

表6 响应Body参数

参数

参数类型

描述

message

Message401 object

接口调用信息。

表7 Message401

参数

参数类型

描述

code

String

错误码。

枚举值:

  • LTS.0001

details

String

调用失败响应信息描述。

枚举值:

  • project verify error

状态码: 403

表8 响应Body参数

参数

参数类型

描述

error_code

String

错误码。

枚举值:

  • LTS.0403

error_msg

String

调用失败响应信息描述。

枚举值:

  • Invalid projectId

状态码: 500

表9 响应Body参数

参数

参数类型

描述

error_code

String

错误码。

error_msg

String

调用失败响应信息描述。

请求示例

  • 查询结构化日志

    POST https://{endpoint}/v2/{project_id}/streams/{log_stream_id}/struct-content/query
  • 当“query”参数填写为 select *

    {
      "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

    {
      "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)

    {
      "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

    {
      "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

请求响应成功。

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

    {
      "result" : [ "system context", "global context", "global context", "system context", "system context" ]
    }
  • 当“query”参数填写为 select count(test3) as mycount, sum(test4) as mysum

    {
      "mycount" : [ 50 ],
      "mysum" : [ 7006779039596516000 ]
    }
  • 当“query”参数填写为 select count(test3) as mycount, sum(test4) as mysum group by(test1)

    {
      "mycount" : [ 32, 22 ],
      "mysum" : [ -3632218674319473000, -1344225381314777000 ]
    }
  • 当“whether_to_rows”参数填写为 true

    {
      "result" : [ {
        "mycount" : "32",
        "mysum" : "-3632218674319473398"
      }, {
        "mycount" : "22",
        "mysum" : "-1344225381314777019"
      } ]
    }

状态码: 400

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

{
  "message" : {
    "code" : "LTS.0601",
    "details" : "params validator error"
  }
}

状态码: 401

AuthFailed。鉴权失败, 请确认token后再次请求 。

{
  "message" : {
    "code" : "LTS.0001",
    "details" : "project verify error"
  }
}

状态码: 403

Forbidden。请求被拒绝访问。返回该状态码,表明请求能够到达服务端,且服务端能够理解用户请求,但是拒绝做更多的事情,因为该请 求被设置为拒绝访问,建议直接修改该请求,不要重试该请求。

{
  "error_code" : "LTS.0001",
  "error_msg" : "Invalid projectId"
}

状态码: 500

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

{
  "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
    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");
    
            ICredential auth = new BasicCredentials()
                    .withAk(ak)
                    .withSk(sk);
    
            LtsClient client = LtsClient.newBuilder()
                    .withCredential(auth)
                    .withRegion(LtsRegion.valueOf("<YOUR REGION>"))
                    .build();
            ListStructuredLogsWithTimeRangeRequest request = new ListStructuredLogsWithTimeRangeRequest();
            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
    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");
    
            ICredential auth = new BasicCredentials()
                    .withAk(ak)
                    .withSk(sk);
    
            LtsClient client = LtsClient.newBuilder()
                    .withCredential(auth)
                    .withRegion(LtsRegion.valueOf("<YOUR REGION>"))
                    .build();
            ListStructuredLogsWithTimeRangeRequest request = new ListStructuredLogsWithTimeRangeRequest();
            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
    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");
    
            ICredential auth = new BasicCredentials()
                    .withAk(ak)
                    .withSk(sk);
    
            LtsClient client = LtsClient.newBuilder()
                    .withCredential(auth)
                    .withRegion(LtsRegion.valueOf("<YOUR REGION>"))
                    .build();
            ListStructuredLogsWithTimeRangeRequest request = new ListStructuredLogsWithTimeRangeRequest();
            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
    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");
    
            ICredential auth = new BasicCredentials()
                    .withAk(ak)
                    .withSk(sk);
    
            LtsClient client = LtsClient.newBuilder()
                    .withCredential(auth)
                    .withRegion(LtsRegion.valueOf("<YOUR REGION>"))
                    .build();
            ListStructuredLogsWithTimeRangeRequest request = new ListStructuredLogsWithTimeRangeRequest();
            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
    # coding: utf-8
    
    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 = __import__('os').getenv("CLOUD_SDK_AK")
        sk = __import__('os').getenv("CLOUD_SDK_SK")
    
        credentials = BasicCredentials(ak, sk) \
    
        client = LtsClient.new_builder() \
            .with_credentials(credentials) \
            .with_region(LtsRegion.value_of("<YOUR REGION>")) \
            .build()
    
        try:
            request = ListStructuredLogsWithTimeRangeRequest()
            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
    # coding: utf-8
    
    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 = __import__('os').getenv("CLOUD_SDK_AK")
        sk = __import__('os').getenv("CLOUD_SDK_SK")
    
        credentials = BasicCredentials(ak, sk) \
    
        client = LtsClient.new_builder() \
            .with_credentials(credentials) \
            .with_region(LtsRegion.value_of("<YOUR REGION>")) \
            .build()
    
        try:
            request = ListStructuredLogsWithTimeRangeRequest()
            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
    # coding: utf-8
    
    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 = __import__('os').getenv("CLOUD_SDK_AK")
        sk = __import__('os').getenv("CLOUD_SDK_SK")
    
        credentials = BasicCredentials(ak, sk) \
    
        client = LtsClient.new_builder() \
            .with_credentials(credentials) \
            .with_region(LtsRegion.value_of("<YOUR REGION>")) \
            .build()
    
        try:
            request = ListStructuredLogsWithTimeRangeRequest()
            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
    # coding: utf-8
    
    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 = __import__('os').getenv("CLOUD_SDK_AK")
        sk = __import__('os').getenv("CLOUD_SDK_SK")
    
        credentials = BasicCredentials(ak, sk) \
    
        client = LtsClient.new_builder() \
            .with_credentials(credentials) \
            .with_region(LtsRegion.value_of("<YOUR REGION>")) \
            .build()
    
        try:
            request = ListStructuredLogsWithTimeRangeRequest()
            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
    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")
    
        auth := basic.NewCredentialsBuilder().
            WithAk(ak).
            WithSk(sk).
            Build()
    
        client := lts.NewLtsClient(
            lts.LtsClientBuilder().
                WithRegion(region.ValueOf("<YOUR REGION>")).
                WithCredential(auth).
                Build())
    
        request := &model.ListStructuredLogsWithTimeRangeRequest{}
    	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
    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")
    
        auth := basic.NewCredentialsBuilder().
            WithAk(ak).
            WithSk(sk).
            Build()
    
        client := lts.NewLtsClient(
            lts.LtsClientBuilder().
                WithRegion(region.ValueOf("<YOUR REGION>")).
                WithCredential(auth).
                Build())
    
        request := &model.ListStructuredLogsWithTimeRangeRequest{}
    	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
    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")
    
        auth := basic.NewCredentialsBuilder().
            WithAk(ak).
            WithSk(sk).
            Build()
    
        client := lts.NewLtsClient(
            lts.LtsClientBuilder().
                WithRegion(region.ValueOf("<YOUR REGION>")).
                WithCredential(auth).
                Build())
    
        request := &model.ListStructuredLogsWithTimeRangeRequest{}
    	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
    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")
    
        auth := basic.NewCredentialsBuilder().
            WithAk(ak).
            WithSk(sk).
            Build()
    
        client := lts.NewLtsClient(
            lts.LtsClientBuilder().
                WithRegion(region.ValueOf("<YOUR REGION>")).
                WithCredential(auth).
                Build())
    
        request := &model.ListStructuredLogsWithTimeRangeRequest{}
    	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

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

401

AuthFailed。鉴权失败, 请确认token后再次请求 。

403

Forbidden。请求被拒绝访问。返回该状态码,表明请求能够到达服务端,且服务端能够理解用户请求,但是拒绝做更多的事情,因为该请 求被设置为拒绝访问,建议直接修改该请求,不要重试该请求。

500

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

错误码

请参见错误码

分享:

    相关文档

    相关产品