Snippet Java for RDS

With snippet Java for RDS, you can free your mind

  1. Get Events from RDS

    AWSCredentials credentials = new BasicAWSCredentials(ACCESS_KEY, SECRET_KEY);
    amazonRdsClient = new AmazonRDSClient(credentials);
    AmazonRDSClient amazonRdsClient.setEndpoint("rds.ap-northeast-1.amazonaws.com");
    int PAST_14_DAYS_IN_MINUTES = 20160;
    DescribeEventsRequest request = new DescribeEventsRequest().withDuration(PAST_14_DAYS_IN_MINUTES);
    DescribeEventsResult result = amazonRdsClient.describeEvents(request);
    List events = result.getEvents();
    for (Event event : events) {
      System.out.println(StringUtils.join(
        DateFormatUtils.format(event.getDate(), "yyyy-MM-dd HH:mm:ss"), "\t",
        StringUtils.rightPad(event.getSourceType(), 25, " "), "\t",
        StringUtils.rightPad(event.getSourceIdentifier(), 25, " "), "\t",
        event.getMessage()));
    }
    
  2. Download General Log File

    String LOG_DIR = "log/";
    String DB_INSTANCE_IDENTIFIER = "ENTER_DB_INSTANCE_IDENTIFIER_HERE";
    String FILENAME_CONTAINS = "mysql-general";
    DescribeDBLogFilesRequest describeDBLogFilesRequest = new DescribeDBLogFilesRequest()
        .withDBInstanceIdentifier(DB_INSTANCE_IDENTIFIER)
        .withFilenameContains(FILENAME_CONTAINS);
    List describeDBLogFiles = amazonRdsClient
        .describeDBLogFiles(describeDBLogFilesRequest)
        .getDescribeDBLogFiles();
    for (DescribeDBLogFilesDetails describeDBLogFilesDetails : describeDBLogFiles) {
      String logFileName = describeDBLogFilesDetails.getLogFileName();
      DownloadDBLogFilePortionRequest downloadDBLogFilePortionRequest = new DownloadDBLogFilePortionRequest()
          .withDBInstanceIdentifier(DB_INSTANCE_IDENTIFIER)
          .withLogFileName(logFileName);
      DownloadDBLogFilePortionResult downloadDBLogFilePortion = amazonRdsClient
          .downloadDBLogFilePortion(downloadDBLogFilePortionRequest);
      String logFileLocal = LOG_DIR + logFileName;
      FileUtils.writeStringToFile(new File(logFileLocal),
          downloadDBLogFilePortion.getLogFileData());
      System.out.println("Log file saved : " + logFileLocal);
    }
    

Comments

Popular posts from this blog

Reduce TIME_WAIT Socket Connections