1.依赖
<!--hutool工具箱--> <dependency> <groupId>cn.hutool</groupId> <artifactId>hutool-all</artifactId> <version>5.6.6</version> </dependency>
2.核心代码
- 根据路径获取磁盘空间数据
/** * 根据路径获取磁盘空间数据 * * @param path 路径 * @return 磁盘空间数据 */ public Map<String, Object> queryDiskSpaceDataByPath(String path) { File file = new File(path); long totalSpace = file.getTotalSpace(); long freeSpace = file.getFreeSpace(); long freeSpaceLong = freeSpace / 1024 / 1024 / 1024; long totalSpaceLong = totalSpace / 1024 / 1024 / 1024; double freeRate = 0.0; if (totalSpaceLong > 1) { freeRate = freeSpaceLong * 1.0 / totalSpaceLong * 100; } // 数据封装 Map<String, Object> diskDataMap = new HashMap<>(4); diskDataMap.put("path", path); diskDataMap.put("diskSpace", "磁盘剩余及总大小[" + freeSpaceLong + "G/" + totalSpaceLong + "G]"); diskDataMap.put("diskUsageRate", "磁盘剩余率[" + String.format("%.2f", freeRate) + "%]"); return diskDataMap; }
- 根据参数判断当前表是否需要清理分区
/** * 根据参数判断当前表是否需要清理分区 * * @param partitionDataMap 表的分区信息 * @param retentionYears 存储年限 * @return 包含判断结果的表分区信息 */ public boolean toDetermineIfTableNeedsToBeClearedUp(Map<String, Object> partitionDataMap, int retentionYears) { String partitionType = MapUtils.getString(partitionDataMap, "partitionType"); String partitionMin = MapUtils.getString(partitionDataMap, "partitionMin"); // 根据type获取差值 long differenceValue = getDateDifferenceValueByParam(partitionType, partitionMin); boolean isToBe = determineIfTableNeedsToBeClearedUp(partitionType, differenceValue, retentionYears); partitionDataMap.put("isToClearedUp", isToBe); return isToBe; }
- 根据参数获取差值【跟当前年月】
/** * 根据参数获取差值 * * @param type 类型 year month day * @param min 最小分区值【最早分区】 * @return 差值 */ private long getDateDifferenceValueByParam(String type, String min) { long differenceValue = 0; switch (type) { case "year": differenceValue = DateUtil.betweenYear(new Date(), DateUtil.parse(min, "yyyy"), false); break; case "month": differenceValue = DateUtil.betweenMonth(new Date(), DateUtil.parse(min, "yyyyMM"), false); break; case "day": differenceValue = DateUtil.betweenDay(new Date(), DateUtil.parse(min, "yyyyMMdd"), false); break; default: break; } return differenceValue; }
- 根据参数判断大小【判断差值是否大于0】
/** * 根据参数判断大小 * * @param type 类型 year month day * @param differenceValue 差值 * @param retentionYears 比对值 * @return 是否大于 */ private boolean determineIfTableNeedsToBeClearedUp(String type, long differenceValue, int retentionYears) { boolean isToBe = false; switch (type) { case "year": isToBe = differenceValue > retentionYears; break; case "month": isToBe = differenceValue > (retentionYears * 12); break; case "day": isToBe = differenceValue > (retentionYears * 365); break; default: break; } return isToBe; }
- 获取需要调整开始时间的分区【临界分区】
/** * 获取需要调整开始时间的分区 * * @param partitionDataMap 表的分区信息 * @param retentionYears 存储年限 * @return 调整开始时间的分区 */ public String getAdjustmentPartitionName(Map<String, Object> partitionDataMap, int retentionYears) { // 解析参数 String partitionType = MapUtils.getString(partitionDataMap, "partitionType"); String partitionPrefix = MapUtils.getString(partitionDataMap, "partitionPrefix"); String partitionMax = MapUtils.getString(partitionDataMap, "partitionMax"); // 获取需要调整的分区名称 String adjustmentPartition = getAdjustmentPartitionName(partitionType, partitionMax, retentionYears); return partitionPrefix + adjustmentPartition; }
- 生成要清理的分区列表【需要删除的分区不包含临界分区】
/** * 生成要清理的分区列表 * * @param partitionDataMap 表的分区信息 * @param retentionYears 存储年限 * @return 要清理的分区列表 */ public List<String> getClearedUpPartitionTableNames(Map<String, Object> partitionDataMap, int retentionYears) { // 解析参数 String partitionType = MapUtils.getString(partitionDataMap, "partitionType"); String partitionMin = MapUtils.getString(partitionDataMap, "partitionMin"); String partitionMax = MapUtils.getString(partitionDataMap, "partitionMax"); // 获取需要调整的分区名称 String adjustmentPartition = getAdjustmentPartitionName(partitionType, partitionMax, retentionYears); // 生成清理分区列表 return getClearedUpListByParam(partitionType, adjustmentPartition, partitionMin); }
- 根据参数获取调整分区名称(调整分区的开始时间)
/** * 根据参数获取调整分区名称(调整分区的开始时间) * * @param type 分区类型 year month day * @param partitionMax 最大分区 * @param retentionYears 存储年限 * @return 需要调整的分区名称 */ private String getAdjustmentPartitionName(String type, String partitionMax, int retentionYears) { String adjustPartitionName = ""; switch (type) { case "year": adjustPartitionName = DateUtil.format(DateUtil.offsetDay(DateUtil.parse(partitionMax, "yyyy"), -365 * retentionYears), "yyyy"); break; case "month": adjustPartitionName = DateUtil.format(DateUtil.offsetDay(DateUtil.parse(partitionMax, "yyyyMM"), -365 * retentionYears), "yyyyMM"); break; case "day": adjustPartitionName = DateUtil.format(DateUtil.offsetDay(DateUtil.parse(partitionMax, "yyyyMMdd"), -365 * retentionYears), "yyyyMMdd"); break; default: break; } return adjustPartitionName; }
- 根据参数获取区间分区表名称
/** * 根据参数获取区间分区表名称 * * @param type 扶你去类型 year month day * @param partitionMax 最大分区值 * @param partitionMin 最小分区值 * @return 区间分区表名称列表 */ private List<String> getClearedUpListByParam(String type, String partitionMax, String partitionMin) { List<String> clearedUpList = new ArrayList<>(); switch (type) { case "year": LocalDateTime startYear = LocalDateTime.of(Integer.parseInt(partitionMin), 1, 1, 0, 0); LocalDateTime endYear = LocalDateTime.of(Integer.parseInt(partitionMax), 1, 1, 0, 0); long numOfYearsBetween = ChronoUnit.YEARS.between(startYear, endYear) - 1; if (numOfYearsBetween >= 0) { for (long i = 0; i <= numOfYearsBetween; i++) { clearedUpList.add("p_year_" + DateUtil.format(startYear.plusYears(i), "yyyy")); } } break; case "month": LocalDateTime startMonth = LocalDateTime.of(Integer.parseInt(partitionMin.substring(0, 4)), Integer.parseInt(partitionMin.substring(4)), 1, 0, 0); LocalDateTime endMonth = LocalDateTime.of(Integer.parseInt(partitionMax.substring(0, 4)), Integer.parseInt(partitionMax.substring(4)), 1, 0, 0); long numOfMonthsBetween = ChronoUnit.MONTHS.between(startMonth, endMonth) - 1; if (numOfMonthsBetween >= 0) { for (long i = 0; i <= numOfMonthsBetween; i++) { clearedUpList.add("p_month_" + DateUtil.format(startMonth.plusMonths(i), "yyyyMM")); } } break; case "day": LocalDateTime startDay = LocalDateTime.of(Integer.parseInt(partitionMin.substring(0, 4)), Integer.parseInt(partitionMin.substring(4, 6)), Integer.parseInt(partitionMin.substring(6)), 0, 0); LocalDateTime endDay = LocalDateTime.of(Integer.parseInt(partitionMax.substring(0, 4)), Integer.parseInt(partitionMax.substring(4, 6)), Integer.parseInt(partitionMax.substring(6)), 0, 0); long numOfDaysBetween = ChronoUnit.DAYS.between(startDay, endDay) - 1; if (numOfDaysBetween >= 0) { for (long i = 0; i <= numOfDaysBetween; i++) { clearedUpList.add("p_day_" + DateUtil.format(startDay.plusDays(i), "yyyyMMdd")); } } break; default: break; } return clearedUpList; }
3.用到的核心类
- java.time.temporal.ChronoUnit【这个类之前用的很少】
- cn.hutool.core.date.DateUtil【hutool工具箱的时间工具】