Sfoglia il codice sorgente

oracle分区批量创建

sbj 1 anno fa
parent
commit
6360b43819

+ 10 - 1
src/main/java/com/example/sqlrun/entity/TablespacePartitionRule.java

@@ -11,7 +11,7 @@ import lombok.Data;
  * 表空间以及表分区创建规则表
  * @TableName TABLESPACE_PATITION_RULE
  */
-@TableName(value ="NHDT.TABLESPACE_PARTITION_RULE")
+@TableName(value ="GHJG_BASICS.TABLESPACE_PARTITION_RULE")
 @Data
 public class TablespacePartitionRule implements Serializable {
     /**
@@ -82,6 +82,15 @@ public class TablespacePartitionRule implements Serializable {
     @TableField("LAST_ADD_PARTITION_TIME")
     private Date lastAddPartitionTime;
 
+    @TableField("IS_DELETE")
+    private Integer isDeleted;
+
+    @TableField("TABLESPACE_TYPE")
+    private String tableSpaceType;
+
+    @TableField("PARTITION_TYPE")
+    private String partitionType;
+
     @TableField(exist = false)
     private static final long serialVersionUID = 1L;
 

+ 31 - 1
src/main/java/com/example/sqlrun/fixSql/MakePartitionSql.java

@@ -23,7 +23,7 @@ public class MakePartitionSql {
    * @param wantedDay             需要创建多少个
    * @return
    */
-  public static List<String> makePartitionSql(String tableName, String partitionNamePrefix, String tableSpaceName,Date startDate,Integer wantedDay){
+  public static List<String> makePartitionSqlByDay(String tableName, String partitionNamePrefix, String tableSpaceName,Date startDate,Integer wantedDay){
     String startSql = "alter table " + tableName + " add partition " + partitionNamePrefix;
     Date maxDate = DateUtils.dayAddNum(startDate, wantedDay);
     ArrayList<String> targetSqls = new ArrayList<>();
@@ -37,4 +37,34 @@ public class MakePartitionSql {
 
     return targetSqls;
   }
+
+  public static List<String> makePartitionSqlByMonth(String tableName, String partitionPrefix,
+      String tablespaceName,Date date) {
+    String startSql = "alter table " + tableName + " add partition " + partitionPrefix;
+    ArrayList<String> targetSqls = new ArrayList<>();
+    while (date.getMonth()+1 <= 12){
+      String targetSql = startSql + DateUtils.dateToString(date,"yyyyMM") + " values less than " + "(TO_DATE('" + DateUtils.dateToString(DateUtils.monthAddNum(date,1),"yyyy-MM-01 00:00:00") + "'" + "," + "'" + "yyyy-MM-dd hh24:mi:ss" + "'" + "))" + " tablespace " + tablespaceName;
+      targetSqls.add(targetSql);
+      if (date.getMonth()+1 == 12) {
+        break;
+      }
+      date = DateUtils.monthAddNum(date,1);
+
+    }
+    return targetSqls;
+  }
+
+  public static List<String> makePartitionSqlByDayForQuarter(String tableName, String partitionNamePrefix,
+      String tableSpaceName, Date date) {
+    String startSql = "alter table " + tableName + " add partition " + partitionNamePrefix;
+    ArrayList<String> targetSqls = new ArrayList<>();
+    Date maxDate = DateUtils.monthAddNum(date, 3);
+    while (date.compareTo(maxDate) < 0){
+      Date stDate = date;
+      date = DateUtils.dayAddNum(date,1);
+      String targetSql = startSql + DateUtils.dateToString(stDate,"yyyyMMdd") + " values less than " + "(TO_DATE('" + DateUtils.dateToString(date,"yyyy-MM-dd 00:00:00") + "'" + "," + "'" + "yyyy-MM-dd hh24:mi:ss" + "'" + "))" + " tablespace " + tableSpaceName;
+      targetSqls.add(targetSql);
+    }
+    return targetSqls;
+  }
 }

+ 64 - 9
src/main/java/com/example/sqlrun/fixSql/MakeTableSpace.java

@@ -2,7 +2,6 @@ package com.example.sqlrun.fixSql;
 
 import com.example.sqlrun.myEnum.SpaceEnum;
 import com.shanghaigeography.Util.DateUtils;
-import java.io.File;
 import java.text.ParseException;
 import java.util.ArrayList;
 import java.util.Date;
@@ -19,7 +18,7 @@ import org.springframework.util.StringUtils;
  * @time: 2023/12/5 13:50 @Version: 1.0
  */
 public class MakeTableSpace {
-  public static List<Map> makeTableSpaceSql(
+  public static List<Map> makeTableSpaceSqlByMonth(
       String tableSpaceNamePrefix,
       String tableSpaceFileNamePrefix,
       String tableSpacePath,
@@ -35,10 +34,6 @@ public class MakeTableSpace {
     if (StringUtils.isEmpty(tableSpacePath) || !StringUtils.hasLength(tableSpacePath)) {
       tableSpacePath = "D:\\app\\Administrator\\dm\\TEST\\" + tableSpaceFileNamePrefix;
     }else {
-      File file = new File(tableSpacePath);
-      if (!file.exists()){
-          file.mkdirs();
-      }
       tableSpacePath =tableSpacePath +  "/" + tableSpaceFileNamePrefix;
     }
     //Date stDate = DateUtils.stringToDate(startDate, "yyyyMM");
@@ -68,9 +63,69 @@ public class MakeTableSpace {
       tableSpaceSql.add(hashMap);
       startDate = DateUtils.monthAddNum(startDate, 1);
     }
-    // String sql = "create tablespace \"GHJGCHANNEL_TEST\"  datafile
-    // 'D:\\APP\\ADMINISTRATOR\\ORADATA\\TEST\\TEST1.DBF' size 1M autoextend on next 1M MAXSIZE
-    // 10M";
     return tableSpaceSql;
   }
+
+  public static List<Map> makeTableSpaceSqlByYear(String tablespacePrefix, String datafilePrefix,
+      String datafilePath, Integer initSize, Integer nextSize, Integer maxSize,
+      Date lastAddTablespaceTime, Integer wanted) {
+    List<Map> tableSpaceSql = new ArrayList<>();
+    String startSql = "create tablespace " + "\"" + tablespacePrefix;
+    String datafile = " datafile ";
+    if (StringUtils.isEmpty(datafilePath) || !StringUtils.hasLength(datafilePath)) {
+      datafilePath = "D:\\app\\Administrator\\dm\\TEST\\" + datafilePrefix;
+    }else {
+      datafilePath =datafilePath +  "/" + datafilePrefix;
+    }
+    Date maxDate = DateUtils.yearAddNum(lastAddTablespaceTime, wanted);
+    while (lastAddTablespaceTime.compareTo(maxDate) < 0){
+      String year = DateUtils.dateToString(lastAddTablespaceTime,"yyyy");
+      String targetSql = startSql + year + "\"" + datafile + "'" + datafilePath + year + ".DBF" + "'" + " size " + initSize + "M" + " autoextend on next " + nextSize + "M" + " maxsize " + maxSize + "M";
+      HashMap<String, Object> hashMap = new HashMap<>();
+      hashMap.put(SpaceEnum.TABLESPACE_SQL.getName(),targetSql);
+      hashMap.put(SpaceEnum.DATE.getName(), lastAddTablespaceTime);
+      hashMap.put(SpaceEnum.TABLESPACENAME.getName(),tablespacePrefix + year);
+      tableSpaceSql.add(hashMap);
+      lastAddTablespaceTime  = DateUtils.yearAddNum(lastAddTablespaceTime,1);
+    }
+    return tableSpaceSql;
+  }
+
+  public static List<Map> makeTableSpaceSqlByQuarter(String tablespacePrefix, String datafilePrefix,
+      String datafilePath, Integer initSize, Integer nextSize,
+      Integer maxSize, Date lastAddTablespaceTime, Integer wanted) {
+    List<Map> tableSpaceSql = new ArrayList<>();
+    String startSql = "create tablespace " + "\"" + tablespacePrefix;
+    String datafile = " datafile ";
+    if (StringUtils.isEmpty(datafilePath) || !StringUtils.hasLength(datafilePath)) {
+      datafilePath = "D:\\app\\Administrator\\dm\\TEST\\" + datafilePrefix;
+    }else {
+      datafilePath =datafilePath +  "/" + datafilePrefix;
+    }
+    // 根据季度建表空间,3个月为一个季度
+    for (Integer i = 0; i < wanted; i++) {
+      Integer quarter= WhateQuarter(lastAddTablespaceTime.getMonth() + 1);
+      String targetSql = startSql + DateUtils.getYear(lastAddTablespaceTime) + quarter + "\"" + datafile + "'" + datafilePath + DateUtils.getYear(lastAddTablespaceTime) + quarter + ".DBF" + "'" + " size " + initSize + "M" + " autoextend on next" + nextSize + "M" + " maxsize " + maxSize + "M";
+      HashMap<String, Object> hashMap = new HashMap<>();
+      hashMap.put(SpaceEnum.TABLESPACE_SQL.getName(),targetSql);
+      hashMap.put(SpaceEnum.DATE.getName(), lastAddTablespaceTime);
+      hashMap.put(SpaceEnum.TABLESPACENAME.getName(), tablespacePrefix + DateUtils.getYear(lastAddTablespaceTime) + quarter);
+      tableSpaceSql.add(hashMap);
+      lastAddTablespaceTime = DateUtils.monthAddNum(lastAddTablespaceTime,3);
+    }
+    return tableSpaceSql;
+  }
+
+  private static Integer WhateQuarter(int i) {
+    if (i >= 0 && i<= 3){
+      return 1;
+    }else if (i>=4 && i<=6){
+      return 2;
+    }else if (i>=7 && i<=9){
+      return 3;
+    }else if (i>=10 && i<=12){
+      return 4;
+    }
+    return null;
+  }
 }

+ 1 - 0
src/main/java/com/example/sqlrun/schedule/MakeSpaceAndPartition.java

@@ -29,6 +29,7 @@ public class MakeSpaceAndPartition {
         for (TablespacePartitionRule rule : sqlNeedList) {
           spaceAndPartitionService.runSQL(rule);
         }
+      System.out.println("ok");
       }catch (Exception e){
         log.error("创建表空间以及表分区异常" + e.getMessage());
       }

+ 151 - 108
src/main/java/com/example/sqlrun/service/impl/SpaceAndPartitionServiceImpl.java

@@ -1,5 +1,6 @@
 package com.example.sqlrun.service.impl;
 
+import com.baomidou.mybatisplus.core.toolkit.Wrappers;
 import com.example.sqlrun.entity.TablespacePartitionRule;
 import com.example.sqlrun.fixSql.MakePartitionSql;
 import com.example.sqlrun.fixSql.MakeTableSpace;
@@ -25,126 +26,168 @@ import java.sql.Statement;
 
 /**
  * @ClassName: SpaceAndPartitionServiceImpl
+ *
  * @description: TODO
  * @author: sbj
  * @time: 2023/12/5 15:55 @Version: 1.0
  */
 @Service
 public class SpaceAndPartitionServiceImpl implements SpaceAndPartitionService {
-    @Resource(name = "statement")
-    private Statement statement;
+  @Resource(name = "statement")
+  private Statement statement;
 
-    @Override
-    public void runSQL(TablespacePartitionRule rule) throws ParseException, SQLException {
-        // 获取需要创建表空间的sql
-        List<Map> tableSpaces =
-                MakeTableSpace.makeTableSpaceSql(
-                        rule.getTablespacePrefix(),
-                        rule.getDatafilePrefix(),
-                        rule.getDatafilePath(),
-                        rule.getInitSize(),
-                        rule.getNextSize(),
-                        rule.getMaxSize(),
-                        rule.getLastAddTablespaceTime(),
-                        rule.getWanted());
-        for (Map tableSpace : tableSpaces) {
-            Date date = (Date) tableSpace.get(SpaceEnum.DATE.getName());
-            List<String> partitionSql =
-                    MakePartitionSql.makePartitionSql(
-                            rule.getTableName(),
-                            rule.getPartitionPrefix(),
-                            tableSpace.get(SpaceEnum.TABLESPACENAME.getName()).toString(),
-                            date,
-                            YearMonth.of(date.getYear() + 1900, date.getMonth() + 1).lengthOfMonth());
-      System.out.println(tableSpace.get(SpaceEnum.TABLESPACE_SQL.getName()).toString());
-       statement.execute(tableSpace.get(SpaceEnum.TABLESPACE_SQL.getName()).toString());
-                  partitionSql.forEach(
-                          i -> {
-                              try {
-                                  statement.addBatch(i);
-                              } catch (SQLException throwables) {
-                                  throwables.printStackTrace();
-                              }
-                          });
-                  statement.executeBatch();
-                  statement.clearBatch();
-      partitionSql.forEach(System.out::println);
-        }
-        Date lastDate = (Date) tableSpaces.get(tableSpaces.size() - 1).get(SpaceEnum.DATE.getName());
-        rule.setLastAddTablespaceTime(DateUtils.monthAddNum(lastDate, 1));
-        rule.setLastAddPartitionTime(DateUtils.dayAddNum(DateUtils.monthAddNum(lastDate, 1), -1));
-        ruleService.updateById(rule);
+  @Override
+  public void runSQL(TablespacePartitionRule rule) throws ParseException, SQLException {
+    // 获取需要创建表空间的sql
+    List<Map> tableSpaces = null;
+    if ("MONTH".equals(rule.getTableSpaceType())){
+        tableSpaces = MakeTableSpace.makeTableSpaceSqlByMonth(
+            rule.getTablespacePrefix(),
+            rule.getDatafilePrefix(),
+            rule.getDatafilePath(),
+            rule.getInitSize(),
+            rule.getNextSize(),
+            rule.getMaxSize(),
+            rule.getLastAddTablespaceTime(),
+            rule.getWanted());
+    }else if ("YEAR".equals(rule.getTableSpaceType())){
+      tableSpaces = MakeTableSpace.makeTableSpaceSqlByYear(
+          rule.getTablespacePrefix(),
+          rule.getDatafilePrefix(),
+          rule.getDatafilePath(),
+          rule.getInitSize(),
+          rule.getNextSize(),
+          rule.getMaxSize(),
+          rule.getLastAddTablespaceTime(),
+          rule.getWanted());
+    }else if ("QUARTER".equals(rule.getTableSpaceType())){
+      tableSpaces = MakeTableSpace.makeTableSpaceSqlByQuarter(
+          rule.getTablespacePrefix(),
+          rule.getDatafilePrefix(),
+          rule.getDatafilePath(),
+          rule.getInitSize(),
+          rule.getNextSize(),
+          rule.getMaxSize(),
+          rule.getLastAddTablespaceTime(),
+          rule.getWanted());
+    }
+    if (tableSpaces == null || tableSpaces.isEmpty()){
+      return;
     }
 
-    @Autowired
-    private TablespacePartitionRuleService ruleService;
-
-    @Override
-    public List<TablespacePartitionRule> getSqlNeed() {
-        return ruleService.list();
+    for (Map tableSpace : tableSpaces) {
+      List<String> partitionSql = null;
+      if ("DAY".equals(rule.getPartitionType()) && "MONTH".equals(rule.getTableSpaceType())){
+        Date date = (Date) tableSpace.get(SpaceEnum.DATE.getName());
+        partitionSql =
+            MakePartitionSql.makePartitionSqlByDay(
+                rule.getTableName(),
+                rule.getPartitionPrefix(),
+                tableSpace.get(SpaceEnum.TABLESPACENAME.getName()).toString(),
+                date,
+                YearMonth.of(date.getYear() + 1900, date.getMonth() + 1).lengthOfMonth());
+      }else if ("MONTH".equals(rule.getPartitionType()) && "YEAR".equals(rule.getTableSpaceType())){
+         partitionSql = MakePartitionSql.makePartitionSqlByMonth(rule.getTableName(),
+             rule.getPartitionPrefix(),
+             tableSpace.get(SpaceEnum.TABLESPACENAME.getName()).toString(),(Date) tableSpace.get(SpaceEnum.DATE.getName()));
+      }else if("DAY".equals(rule.getPartitionType()) && "QUARTER".equals(rule.getTableSpaceType())){
+        partitionSql = MakePartitionSql.makePartitionSqlByDayForQuarter(rule.getTableName(),rule.getPartitionPrefix(),tableSpace.get(SpaceEnum.TABLESPACENAME.getName()).toString(),(Date) tableSpace.get(SpaceEnum.DATE.getName()));
+      }
+      System.out.println(tableSpace.get(SpaceEnum.TABLESPACE_SQL.getName()).toString() + ";");
+      // statement.execute(tableSpace.get(SpaceEnum.TABLESPACE_SQL.getName()).toString());
+      //      partitionSql.forEach(
+      //          i -> {
+      //            try {
+      //              statement.addBatch(i);
+      //            } catch (SQLException throwables) {
+      //              throwables.printStackTrace();
+      //            }
+      //          });
+      // statement.executeBatch();
+      // statement.clearBatch();
+      assert partitionSql != null;
+      partitionSql.forEach(i -> System.out.println(i + ";"));
     }
+    Date lastDate = (Date) tableSpaces.get(tableSpaces.size() - 1).get(SpaceEnum.DATE.getName());
+    rule.setLastAddTablespaceTime(DateUtils.monthAddNum(lastDate, 1));
+    rule.setLastAddPartitionTime(DateUtils.dayAddNum(DateUtils.monthAddNum(lastDate, 1), -1));
+    //ruleService.updateById(rule);
+  }
+
+  @Autowired private TablespacePartitionRuleService ruleService;
+
+  @Override
+  public List<TablespacePartitionRule> getSqlNeed() {
+    return ruleService.list(
+        Wrappers.<TablespacePartitionRule>lambdaQuery()
+            .eq(TablespacePartitionRule::getIsDeleted, 0));
+  }
 
-    @Override
-    public String createTableSpace(Map<String, String> paramBody) {
-        try {
-            String tableSpaceNamePrefix = paramBody.get("tableSpaceName");
-            String tableSpaceFileNamePrefix = paramBody.get("tableSpaceFileName");
-            String tableSpacePath = paramBody.get("tableSpacePath");
-            Integer initSize = Integer.valueOf(paramBody.get("initSize"));
-            Integer nextSize = Integer.valueOf(paramBody.get("nextSize"));
-            Integer maxSize = Integer.valueOf(paramBody.get("maxSize"));
-            Date startDate = DateUtils.stringToDate(paramBody.get("startDate"), "yyyy-MM-dd");
-            Integer wanted = Integer.valueOf(paramBody.get("wanted"));
-            List<Map> maps =
-                    MakeTableSpace.makeTableSpaceSql(
-                            tableSpaceNamePrefix,
-                            tableSpaceFileNamePrefix,
-                            tableSpacePath,
-                            initSize,
-                            nextSize,
-                            maxSize,
-                            startDate,
-                            wanted);
-            for (Map map : maps) {
-                statement.execute(map.get(SpaceEnum.TABLESPACE_SQL.getName()).toString());
-            }
-            Map map = maps.get(maps.size() - 1);
-            return "最后一个表空间时间"
-                    + map.get(SpaceEnum.DATE.getName())
-                    + "\n"
-                    + "最后一个表空间名称"
-                    + map.get(SpaceEnum.TABLESPACENAME.getName());
-        } catch (Exception e) {
-            return "error" + e.getMessage();
-        }
+  @Override
+  public String createTableSpace(Map<String, String> paramBody) {
+    try {
+      String tableSpaceNamePrefix = paramBody.get("tableSpaceName");
+      String tableSpaceFileNamePrefix = paramBody.get("tableSpaceFileName");
+      String tableSpacePath = paramBody.get("tableSpacePath");
+      Integer initSize = Integer.valueOf(paramBody.get("initSize"));
+      Integer nextSize = Integer.valueOf(paramBody.get("nextSize"));
+      Integer maxSize = Integer.valueOf(paramBody.get("maxSize"));
+      Date startDate = DateUtils.stringToDate(paramBody.get("startDate"), "yyyy-MM-dd");
+      Integer wanted = Integer.valueOf(paramBody.get("wanted"));
+      List<Map> maps =
+          MakeTableSpace.makeTableSpaceSqlByMonth(
+              tableSpaceNamePrefix,
+              tableSpaceFileNamePrefix,
+              tableSpacePath,
+              initSize,
+              nextSize,
+              maxSize,
+              startDate,
+              wanted);
+      for (Map map : maps) {
+        statement.execute(map.get(SpaceEnum.TABLESPACE_SQL.getName()).toString());
+      }
+      Map map = maps.get(maps.size() - 1);
+      return "最后一个表空间时间"
+          + map.get(SpaceEnum.DATE.getName())
+          + "\n"
+          + "最后一个表空间名称"
+          + map.get(SpaceEnum.TABLESPACENAME.getName());
+    } catch (Exception e) {
+      return "error" + e.getMessage();
     }
+  }
 
-    @Override
-    public String createTablePartition(Map<String, String> param) {
-        try {
-            String tableName = param.get("tableName");
-            String partitionNamePrefix = param.get("partitionNamePrefix");
-            String tableSpaceName = param.get("tableSpaceName");
-            Date startDate = DateUtils.stringToDate(param.get("startDate"), "yyyy-MM-dd");
-            Integer wantedDay = null;
-            if (StringUtils.isBlank(param.get("wantedDay"))) {
-                Calendar instance = Calendar.getInstance();
-                instance.setTime(startDate);
-                wantedDay = instance.getActualMaximum(Calendar.DAY_OF_MONTH) - instance.get(Calendar.DAY_OF_MONTH) + 1;
-            } else {
-                wantedDay = Integer.valueOf(param.get("wantedDay"));
-            }
-            List<String> partitionSql = MakePartitionSql
-                    .makePartitionSql(tableName, partitionNamePrefix, tableSpaceName, startDate, wantedDay);
-            for (String s : partitionSql) {
-                statement.addBatch(s);
-            }
-            statement.executeBatch();
-            statement.clearBatch();
-            //h
-            return "success";
-        } catch (Exception e) {
-            return "error" + e.getMessage();
-        }
+  @Override
+  public String createTablePartition(Map<String, String> param) {
+    try {
+      String tableName = param.get("tableName");
+      String partitionNamePrefix = param.get("partitionNamePrefix");
+      String tableSpaceName = param.get("tableSpaceName");
+      Date startDate = DateUtils.stringToDate(param.get("startDate"), "yyyy-MM-dd");
+      Integer wantedDay = null;
+      if (StringUtils.isBlank(param.get("wantedDay"))) {
+        Calendar instance = Calendar.getInstance();
+        instance.setTime(startDate);
+        wantedDay =
+            instance.getActualMaximum(Calendar.DAY_OF_MONTH)
+                - instance.get(Calendar.DAY_OF_MONTH)
+                + 1;
+      } else {
+        wantedDay = Integer.valueOf(param.get("wantedDay"));
+      }
+      List<String> partitionSql =
+          MakePartitionSql.makePartitionSqlByDay(
+              tableName, partitionNamePrefix, tableSpaceName, startDate, wantedDay);
+      for (String s : partitionSql) {
+        statement.addBatch(s);
+      }
+      statement.executeBatch();
+      statement.clearBatch();
+      // h
+      return "success";
+    } catch (Exception e) {
+      return "error" + e.getMessage();
     }
+  }
 }

+ 6 - 6
src/main/resources/application.yaml

@@ -6,12 +6,12 @@ spring:
     hikari:
       idle-timeout: 30000
       maximum-pool-size: 150
-    password: SHHW1234
-    username: nhdt
-    url: jdbc:oracle:thin:@//10.135.10.193/orcl
-#    password: GHJG@123
-#    username: ghjg_basics
-#    url: jdbc:oracle:thin:@//10.114.4.56/shhwdb
+#    password: SHHW1234
+#    username: nhdt
+#    url: jdbc:oracle:thin:@//10.135.10.193/orcl
+    password: GHJG@123
+    username: ghjg_basics
+    url: jdbc:oracle:thin:@//10.114.4.56/shhwdb
 #    password: SHHW1234
 #    url: jdbc:oracle:thin:@//127.0.0.1:1521/orcl
 #    username: nhgis