博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
【数据库中间件】MyCat分表分库规则实现
阅读量:5951 次
发布时间:2019-06-19

本文共 3063 字,大约阅读时间需要 10 分钟。

hot3.png

/***

 * 先根据截取字段的日期分组,再根据字段值数据按天入表
 * Fx_demon
 *
 */

public class PartitionBySubstringRangeDateDay extends AbstractPartitionAlgorithm

        implements RuleAlgorithm {
    
    private static final long serialVersionUID = -6211713068380831727L;
    private static final Logger LOGGER = LoggerFactory.getLogger(PartitionBySubstringRangeDateDay.class);
    private int startIndex; // 从第N位开始截取字符串
    private String dateFormat;// 日期格式 ,yyMMdd或者yyyyMMdd
    private int dateFormatLength;
    /** 日期分组 */
    private List<String[]> rangeDate;
    private int rangeDateSize;
    /** 每个分组对应的节点数量 ,逗号分隔, 配置如 3 , 6 */
    private int[] partitionCount;
    private PartitionByDate[] partitionByDay;
   
    public void init() {
        dateFormatLength = dateFormat.length();
        rangeDateSize = rangeDate.size();
        if (rangeDateSize != partitionCount.length) {
            throw new IllegalArgumentException("rangeDate not match partitionCount");
        }
    }
   
    public Integer calculate(String columnValue) {
        String date = columnValue.substring(startIndex, startIndex + dateFormatLength);
        date = date.replace("-", "");
        int count = 0;
        for (int i = 0; i < rangeDateSize; i++) {
           
            String rangeDateStart = rangeDate.get(i)[0].replace("-", "");
            String rangeDateEnd = rangeDate.get(i)[1].replace("-", "");
           
            if (date.compareTo(rangeDateStart) >= 0 && date.compareTo(rangeDateEnd) <= 0) {
               
                this.partitionByDay[i].setsBeginDate(rangeDate.get(i)[0]);
                this.partitionByDay[i].setsEndDate(rangeDate.get(i)[1]);
               
                return partitionByDay[i].calculate(columnValue) + count;
            } else {
                count += partitionCount[i];
            }
        }
        return null;
    }
    // 在columnValue截取日期的起始记录
    public void setStartIndex(String startIndex) {
        this.startIndex = Integer.parseInt(startIndex);
    }
    // 数据库节点的数量
    public void setPartitionCount(String partitionCount) {
        String[] partitionCountStr = partitionCount.split(",");
        this.partitionCount = new int[partitionCountStr.length];
        this.partitionByDay = new PartitionByDate[partitionCountStr.length];
        for (int i = 0; i < partitionCountStr.length; i++) {
            this.partitionCount[i] = Integer.parseInt(partitionCountStr[i]);
            this.partitionByDay[i] = new PartitionByDate();
            this.partitionByDay[i].setDateFormat(dateFormat);
            this.partitionByDay[i].setsBeginDate("2017-01-01");
            this.partitionByDay[i].setsPartionDay("1");
            this.partitionByDay[i].init();
        }
    }
    /** 日期分组,逗号分隔如 ,【 20170101@20170331 , 20170401@20170630】 或  【 20170101@20170331 , 2017-04-01@2017-06-30】*/
    public void setRangeDate(String rangeDateStr) {
        String[] rangeDateArr = rangeDateStr.split(",");
        this.rangeDate = new LinkedList<>();
        for (String rangeDate : rangeDateArr) {
            String[] date = rangeDate.trim().split("@");
            date[0] = date[0].trim();
            date[1] = date[1].trim();
            this.rangeDate.add(date);
        }
    }
    public void setDateFormat(final String dateFormat) {
        if ((!"yyMMdd".equals(dateFormat) && !"yyyyMMdd".equals(dateFormat) && ( !"yyyy-MM-dd".equals(dateFormat))) ) {
            throw new IllegalArgumentException("only support yyyyMMdd or yyMMdd or yyyy-MM-dd ");
        }
        this.dateFormat = dateFormat;
    }
    @Override
    public int getPartitionNum() {
        int sum = 0;
        for (int i : this.partitionCount)
            sum += i;
        return sum;
    }
}

转载于:https://my.oschina.net/fxdemon/blog/862634

你可能感兴趣的文章