This topic provides an example that describes how to use Database Autonomy Service (DAS) SDK for Java. In this example, the SDK is used to create and query diagnostic reports.

Use DAS SDK for Java to create and query diagnostic reports

Use the following code to install the core library of Alibaba Cloud SDK for Java. The name of the core library is aliyun-java-sdk-core.

 <dependency>
   <groupId>com.aliyun</groupId>
   <artifactId>aliyun-java-sdk-core</artifactId>
   <version>${aliyun-java-sdk-core.version}</version>
 </dependency>
public class TestOpenAPI {
    public static final String product = "DAS";
    public static final String version = "2020-01-16";
    public static final String domain = "das.cn-shanghai.aliyuncs.com";
    public static final String key = "xxx";
    public static final String secret = "xxx";

    public static String requestDASOpenApi(String action, Map<String, Object> params) {
        CommonRequest request = new CommonRequest();
        request.setVersion(version);
        request.setDomain(domain);
        request.setMethod(MethodType.POST);
        request.setProduct(product);
        request.setAction(action);
        if (params != null) {
            params.forEach(request::putBodyParameter);
        }
        DefaultProfile profile = DefaultProfile.getProfile("cn-hangzhou", key, secret);
        DefaultAcsClient client = new DefaultAcsClient(profile);
        try {
            CommonResponse response = client.getCommonResponse(request);
            if (response.getHttpStatus() == 200) {
                return response.getData();
            }
        } catch (ClientException e) {
            e.printStackTrace();
        }
        return null;
    }

    public static void testCreateReport() {
        String action = "CreateDiagnosticReport";
        Long endTime = System.currentTimeMillis();
        Long startTime = endTime - TimeUnit.HOURS.toMillis(6);
        Map<String, Object> param = new HashMap<>();
        param.put("DBInstanceId", "rm-xx");
        param.put("StartTime", startTime);
        param.put("EndTime", endTime);
        System.out.println(requestDASOpenApi(action, param));
    }

    public static void testDescribeReport() {
        String action = "DescribeDiagnosticReportList";
        Map<String, Object> param = new HashMap<>();
        param.put("DBInstanceId", "rm-xxxx");
        System.out.println(requestDASOpenApi(action, param));
    }

    public static void main(String[] args) {
       testDescribeReport();
    }
}