本教程详细介绍如何使用Alibaba Cloud SDK for Java查询一台Redis实例的审计日志和备份信息。

前提条件

  • 使用Alibaba Cloud SDK for Java,您需要一个阿里云账号和访问密钥(AccessKey)。 请在阿里云控制台中的AccessKey管理页面上创建和查看您的AccessKey。
  • 确保您已经安装了Alibaba Cloud SDK for Java,准确的SDK版本号,请参见 阿里云开发工具包(SDK)
    <dependencies>
        <!-- https://mvnrepository.com/artifact/com.aliyun/aliyun-java-sdk-core -->
        <dependency>
            <groupId>com.aliyun</groupId>
            <artifactId>aliyun-java-sdk-core</artifactId>
            <version>4.4.3</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/com.aliyun/aliyun-java-sdk-r-kvstore -->
        <dependency>
            <groupId>com.aliyun</groupId>
            <artifactId>aliyun-java-sdk-r-kvstore</artifactId>
            <version>2.1.2</version>
        </dependency>
    </dependencies>

代码示例

本文操作示例主要以代码形式体现,具体代码如下:
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.profile.DefaultProfile;
import com.aliyuncs.r_kvstore.model.v20150101.DescribeAuditRecordsRequest;
import com.aliyuncs.r_kvstore.model.v20150101.DescribeAuditRecordsResponse;
import com.aliyuncs.r_kvstore.model.v20150101.DescribeBackupsRequest;
import com.aliyuncs.r_kvstore.model.v20150101.DescribeBackupsResponse;
import com.google.gson.Gson;
import java.util.List;

/**
 * DescribeAuditRecords    查看Redis实例的审计日志
 * DescribeBackups    查询Redis实例的备份文件信息
 */
public class DescribeRedis {

    // 需要查询的实例ID
    private static String instanceId = "";
    // 备份文件查询开始时间
    private static String describeBackupsStartTime = "";
    // 备份文件查询结束时间
    private static String describeBackupsEndTime = "";
    // 审计日志查询开始时间
    private static String startTime = "";
    // 审计日志查询结束时间
    private static String endTime = "";

    public static void main(String[] args) {
        IAcsClient client = Initialization();
        // 查看Redis实例的审计日志
        List<DescribeAuditRecordsResponse.SQL> items = describeAuditRecords(client);
        // 查询Redis实例的备份文件信息
        List<DescribeBackupsResponse.Backup> backups = describeBackups(client);
    }

    /**
     * DescribeAuditRecords    查看Redis实例的审计日志
     */
    private static List<DescribeBackupsResponse.Backup> describeBackups(IAcsClient client) {
        DescribeBackupsRequest request = new DescribeBackupsRequest();
        // 目标实例的ID。
        request.setInstanceId(instanceId);
        // 查询开始时间,例如:2018-12-21T18:10Z。
        request.setStartTime(describeBackupsStartTime);
        // 查询结束时间,例如:2018-12-28T18:10Z。
        request.setEndTime(describeBackupsEndTime);
        try {
            DescribeBackupsResponse response = client.getAcsResponse(request);
            System.out.println(new Gson().toJson(response));
            return response.getBackups();
        } catch (ClientException e) {
            System.out.println("ErrCode:" + e.getErrCode());
            System.out.println("ErrMsg:" + e.getErrMsg());
            System.out.println("RequestId:" + e.getRequestId());
        }
        return null;
    }

    /**
     * DescribeBackups    查询Redis实例的备份文件信息
     */
    private static List<DescribeAuditRecordsResponse.SQL> describeAuditRecords(IAcsClient client) {
        DescribeAuditRecordsRequest request = new DescribeAuditRecordsRequest();
        // 需要查询的实例的ID。
        request.setInstanceId(instanceId);
        // 查询开始时间,格式:YYYY-MM-DDTHH:mm:ssZ。
        request.setStartTime(startTime);
        // 查询结束时间,必须晚于查询开始时间,格式:YYYY-MM-DDTHH:mm:ssZ。
        request.setEndTime(endTime);
        try {
            DescribeAuditRecordsResponse response = client.getAcsResponse(request);
            System.out.println(new Gson().toJson(response));
            return response.getItems();
        } catch (ClientException e) {
            System.out.println("ErrCode:" + e.getErrCode());
            System.out.println("ErrMsg:" + e.getErrMsg());
            System.out.println("RequestId:" + e.getRequestId());
        }
        return null;
    }

    /**
     * Initialization  初始化公共请求参数
     */
    private static IAcsClient Initialization() {
        // 初始化请求参数
        DefaultProfile profile = DefaultProfile.getProfile(
                "<your-region-id>", // 您的可用区ID
                "<your-access-key-id>", // 您的AccessKey ID
                "<your-access-key-secret>"); // 您的AccessKey Secret
        return new DefaultAcsClient(profile);
    }
}

执行结果

正确的返回结果类似如下:
{
    "requestId": "C08D3C70-A159-42DF-A82C-84517BF91134", 
    "pageNumber": 1, 
    "pageSize": 30, 
    "totalRecordCount": 0, 
    "instanceName": "r-bp1iv3eg4tp6g82nz4", 
    "startTime": "2019-09-24T12:10:00Z", 
    "endTime": "2019-09-24T13:10:00Z", 
    "items": [...]
}
{
    "requestId": "207072B3-A357-442A-9801-A0748B7B2872", 
    "pageNumber": 1, 
    "pageSize": 30, 
    "totalCount": 0, 
    "backups": [...]
}