通过自动化任务进行数据库表分区

前面我简单记录过怎么实现表分区,现在要实现就是让他自动分区。实现方法就是使用Quartz框架 这个框架很强大,有兴趣可以去http://www.quartz-scheduler.org/下载感觉他的功能,总之它的核心就是调度器。“调度器负责管理Quartz应用运行时环境。调度器不是靠自己做所有的工作,而是依赖框架内一些非常重要的部件。Quartz不仅仅是线程和线程管理。为确保可伸缩性,Quartz采用了基于多线程的架构。启动时,框架初始化一套worker线程,这套线程被调度器用来执行预定的作业。这就是Quartz怎样能并发运行多个作业的原理。Quartz依赖一套松耦合的线程池管理部件来管理线程环境”--这段话引用来自百度百科。接着开始配置Quartz (Spring集成) quartz-local-sys.xml配置如下:

	<bean id="ServiceTrigger"
		class="org.springframework.scheduling.quartz.CronTriggerFactoryBean">
		<property name="jobDetail">
			<bean
				class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
<!-- 这个意思就是Spring 管理这个Bean>
				<property name="targetObject" ref="demoThread"></property>
<!--这个类里面的属性之一是targetObject 简而言之就是目标对象 意思就是找那个类>
				<property name="targetMethod" value="execute"></property>
<!-- 这个targetMethod 就是找到类后 执行那个方法>
				<property name="concurrent" value="false"></property>
<!--concurrent 设置为false 就是本次任务没有执行完,但是又到了时间该调度了,这时候就不会在次调度>
			</bean>
		</property>
		<property name="cronExpression" value="0 0/1 * * * ?" />这个配置代表每一种执行一次为了方便测试
<!--这个就是告诉调度器什么时候调度 一般是 秒 分 时 日 月 年 (有时候也有星期) >具体可以参考
cronExpression表达式的写法
</bean>


applicationContext-quartz-local.xml
<bean id="Acheduler"
		class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
		<!-- Triggers集成 -->
		<property name="triggers">
			<list>
				<ref bean="aServiceTrigger" />
			</list>
		</property>
		<property name="quartzProperties">
			<props>
				<prop key="org.quartz.threadPool.threadCount">5</prop>
			</props>
		</property>
		<!-- 启动时延期2秒开始任务 -->
		<property name="startupDelay" value="2" />
	</bean>
	<import resource="quartz-local-sys.xml" />

分区任务类:

部分代码如下:

  // 删除T_INTERFACE_LOG/T_INTERFACE_MSG分区
    public void DropLogorMsg(String tableName, String logo) {
        String s = DataFormat(-31, "yyyyMMdd", Calendar.DATE);
        int sint = Integer.valueOf(s);
        List<String> nameList = partitionJobDao.getPartitionName(tableName);
        for (String name : nameList) {
            String namedate = name.substring(logo.length());
            int pint = Integer.valueOf(namedate);
            if (pint < sint) {
                if (nameList.contains(logo + namedate)) {
                    PartitionJob partition = new PartitionJob();
                    partition.setPartitionName(logo + namedate);
                    partition.setTableName(tableName);
                    partitionJobDao.DropPart(partition);
                }
            }
        }
    }
    
    // 添加分区
    public void AddLogorMsg(String tableName, String logo) {
		log.error(JOB_NAME + "AddLogorMsg" + tableName);
        String snow = DataFormat(0, "yyyyMMdd", Calendar.DATE);
        int snowint = Integer.valueOf(snow);
        List<String> nameList = partitionJobDao.getPartitionName(tableName);
        String namedate;
        for (String name : nameList) {
            namedate = name.substring(logo.length());
            int pint = Integer.valueOf(namedate);
            if (pint > snowint) {
                for (int i = 0; i < 32; i++) {
                    String adds = DataFormat(i, "yyyyMMdd", Calendar.DATE);
                    String svalue = DataFormat(i + 1, "yyyyMMdd", Calendar.DATE);
                    if (!nameList.contains(logo + adds)) {
                        PartitionJob partition = new PartitionJob();
                        partition.setPartitionName(logo + adds);
                        partition.setPartitionValue(svalue);
                        partition.setTableName(tableName);
                        partitionJobDao.AddPart(partition);
                    }
                }
                break;
            }
            
        }
    }
    
    public static String DataFormat(int number, String format, int type) {
        Calendar c = Calendar.getInstance();
        c.add(type, number);
        String date = new SimpleDateFormat(format).format(c.getTime());
        return date;
    }
Mybatis的配置文件:

<!-- t_interface_log_new 每天自动分区 -->
	<insert id="insertLogorMsg" parameterType="PartitionJob">


		ALTER TABLE
		${tableName} ADD PARTITION ${partitionName} VALUES LESS
		THAN
		(TO_DATE(${partitionValue},'YYYYMMDD'))
	</insert>
	
	
	<delete id="delete" parameterType="PartitionJob">
		alter table
		${tableName} drop partition ${partitionName}
	</delete>
	<!-- tb_report_form每个月一号自动分区 -->
	<insert id="insertPart" parameterType="PartitionJob">
		alter table
		${tableName} add partition ${partitionName}
		values(${partitionValue})
	</insert>
	


	<!-- 选择表的分区表名 -->
	<select id="selectPartitonName" parameterType="String"
		resultType="String">
		SELECT t.partition_name  FROM USER_TAB_PARTITIONS t WHERE
		TABLE_NAME=#{tableName}
	</select>

测试时候在一个main 方法里加载需要加载的xml

    static {
        contextMap.put("default", "/applicationContext-*.xml");
    }

 ApplicationContext context = new ClassPathXmlApplicationContext(
                (String[]) contextMap.values().toArray(jobContextArry));
测试通过后就可以修改cronExpression表达式为需求的比如每天分区,同时删除前面几天的分区表。


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

蜗牛乌龟一起走

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值