查询数据库日志
引导式阅读
Java
查询数据库日志
作者
c***r
上架时间
2023-11-17 08:15:47

1. 介绍

数据管理服务(Data Admin Service,简称DAS),是一种提供数据库可视化操作的服务,包括基础SQL操作、高级数据库管理、智能化运维等功能,旨在帮助用户易用、安全、智能的进行数据库管理。本示例展示如何通过java版本的SDK方式查询数据库日志。

2. 流程图

查询数据库日志

3. 前置条件

1.已 注册 华为云,并完成 实名认证

2.获取华为云开发工具包(SDK),您也可以查看安装JAVA SDK。

3.已获取华为云账号对应的Access Key(AK)和Secret Access Key(SK)。请在华为云控制台“我的凭证 > 访问密钥”页面上创建和查看您的AK/SK。具体请参见 访问密钥

4.已具备开发环境 ,支持Java JDK 1.8及其以上版本。

4. SDK获取和安装

您可以通过Maven方式获取和安装SDK,首先需要在您的操作系统中下载并安装Maven ,安装完成后您只需要在Java项目的pom.xml文件中加入相应的依赖项即可。

具体的SDK版本号请参见 SDK开发中心

<dependency> <groupId>com.huaweicloud.sdk</groupId> <artifactId>huaweicloud-sdk-das</artifactId> <version>3.1.1</version> </dependency>

5. 关键代码片段

以下代码展示如何使用SDK查询数据库日志:

public class DataBaseLogDemo { private static final Logger logger = LoggerFactory.getLogger(DataBaseLogDemo.class.getName()); /** * args[0] = <<YOUR AK>> * args[1] = <<YOUR SK>> * args[2] = <<YOUR REGION_ID>> * args[3] = <<YOUR INSTANCE_ID>> * args[4] = <<YOUR DATASTORE_TYPE>> * args[5] = <<SWITCH_TYPE>> * args[6] = <<SWITCH_STATUS>> * args[7] = <<DATA_EXPORT_START_AT>> * args[8] = <<DATA_EXPORT_END_AT>> */ public static void main(String[] args) { // 认证用的ak和sk硬编码到代码中或者明文存储都有很大的安全风险,建议在配置文件或者环境变量中密文存放,使用时解密,确保安全; // 本示例以ak和sk保存在环境变量中来实现身份认证为例,运行示例前请先在本地环境中设置环境变量HUAWEICLOUD_SDK_AK和HUAWEICLOUD_SDK_SK。 String ak = args[0]; String sk = args[1]; String regionId = args[2]; String instanceId = args[3]; String datastoreType = args[4]; String type = args[5]; Integer status = Integer.valueOf(args[6]); Long startAt = Long.valueOf(args[7]); Long endAt = Long.valueOf(args[8]); ICredential auth = new BasicCredentials() .withAk(ak) .withSk(sk); DasClient client = DasClient.newBuilder() .withCredential(auth) .withRegion(DasRegion.valueOf(regionId)) .build(); // 查询全量SQL和慢SQL的开关状态 showSqlSwitchStatus(client, instanceId, type, datastoreType); // 开启/关闭全量SQL、慢SQL开关 changeSqlSwitch(client, instanceId, type, status, datastoreType); // 导出慢SQL数据 exportSlowQueryLogs(client, instanceId, datastoreType, startAt, endAt); // 导出全量SQL exportSqlStatements(client, instanceId, datastoreType, startAt, endAt); } private static void showSqlSwitchStatus(DasClient client, String instanceId, String type, String datastoreType) { ShowSqlSwitchStatusRequest request = new ShowSqlSwitchStatusRequest(); request.withInstanceId(instanceId); request.withType(type); request.withDatastoreType(datastoreType); invokeClient(() -> { ShowSqlSwitchStatusResponse response = client.showSqlSwitchStatus(request); logger.info(response.toString()); }); } private static void changeSqlSwitch(DasClient client, String instanceId, String type, Integer status, String datastoreType) { ChangeSqlSwitchRequest request = new ChangeSqlSwitchRequest(); ChangeSqlSwitchBody switchBody = new ChangeSqlSwitchBody() .withType(type) .withStatus(status) .withDatastoreType(datastoreType); request.withInstanceId(instanceId); request.withBody(switchBody); invokeClient(() -> { ChangeSqlSwitchResponse response = client.changeSqlSwitch(request); logger.info(response.toString()); }); } private static void exportSlowQueryLogs(DasClient client, String instanceId, String datastoreType, Long startAt, Long endAt) { ExportSlowQueryLogsRequest request = new ExportSlowQueryLogsRequest(); request.withInstanceId(instanceId); request.withDatastoreType(datastoreType); request.withStartAt(startAt); request.withEndAt(endAt); request.withLimit(10); invokeClient(() -> { ExportSlowQueryLogsResponse response = client.exportSlowQueryLogs(request); logger.info(response.toString()); }); } private static void exportSqlStatements(DasClient client, String instanceId, String datastoreType, Long startAt, Long endAt) { ExportSqlStatementsRequest request = new ExportSqlStatementsRequest(); request.withInstanceId(instanceId); request.withDatastoreType(datastoreType); request.withStartAt(startAt); request.withEndAt(endAt); request.withLimit(10); invokeClient(() -> { ExportSqlStatementsResponse response = client.exportSqlStatements(request); logger.info(response.toString()); }); } private static <T> void invokeClient(Runnable runnable) { try { runnable.run(); } catch (ConnectionException e) { logger.error("ConnectionException", e); } catch (RequestTimeoutException e) { logger.error("RequestTimeoutException", e); } catch (ServiceResponseException e) { logger.error("httpStatusCode: {}, errorCode: {}, errorMsg: {}", e.getHttpStatusCode(), e.getErrorCode(), e.getErrorMsg()); } } }

6. 返回结果示例

  • 查询全量SQL和慢SQL的开关状态(ShowSqlSwitchStatus)接口的返回值:
{ "status": "Enabled", "retention_days": 7 }
  • 开启/关闭全量SQL、慢SQL开关(ChangeSqlSwitch)接口的返回值:
{ "status": "Enabled" }
  • 导出慢SQL数据(ExportSlowQueryLogs)接口的返回值:
{ "slow_logs": [ { "execute_at": 1612343898000, "sql": "SELECT sleep(10)\nLIMIT 0, 50;", "database": "123", "client": "[100.79.0.248]", "user": "root[root]", "query_time": 10.000158309936523, "lock_time": 0, "rows_examined": 0, "rows_sent": 1 } ], "next_marker": "DXF1ZXJ5QW5kRmV0Y2gBAAAAAAAJr48WZERxYkx5Q2VRQS1LSXRrRWt0VEN1QQ==" }
  • 导出全量SQL(ShowSqlSwitchStatus)接口的返回值:
{ "statements": [ { "sql": "SELECT 1", "operate_type": "select", "status": "success", "error_no": "", "database": "", "thread_id": "11481954", "client": "100.79.3.154", "user": "root", "execute_at": 1612403000100, "query_time": 0, "lock_time": 0, "rows_examined": 0, "rows_sent": 1, "rows_affected": 0 } ], "next_marker": "DXF1ZXJ5QW5kRmV0Y2gBAAAAAAASG3cWcHVpdktBU1lTbjJMM2tmYXYxZ09nUQ==" }

7.参考链接

请见 查询全量SQL和慢SQL的开关状态 您可以在 API Explorer 中直接运行调试该接口。

请见 开启/关闭全量SQL、慢SQL开关 您可以在 API Explorer 中直接运行调试该接口。

请见 导出慢SQL数据 您可以在 API Explorer 中直接运行调试该接口。

请见 导出全量SQL 您可以在 API Explorer 中直接运行调试该接口。

修订记录

发布日期 文档版本 修订说明
2022-10-30 1.0 文档首次发布