Hibernate对于大数据量处理之分表
Hibernate如何处理大数据量的操作呢?主要是数据分库或分表存放,从而提高软件的运行及数据库访问的速度。下面介绍一下对于日志存储的数据分表操作。
日志管理
----------------
1.导航栏-->日志管理
2.LogAction.findAllLogs();
public String findAllLogs(){
this.allLogs = logService.findAllEntities();
return "logListPage" ;
}
3.
4.
日志:
数据量大:
1.分表(动态表).
2.分库.
logs
logs_2013_1
logs_2013_2
使用调度器动态生成日志表
------------------------------
0.dao和service增加执行原生sql语句的功能.
dao
-----------------
public interface BaseDao<T> {
...
public void executeSQL(String sql,Object...objects);
}
public class BaseDaImpl<T> ...{
...
//执行原生的sql语句
public void executeSQL(String sql,Object...objects){
SQLQuery q = sf.getCurrentSession().createSQLQuery(sql);
for(int i = 0 ; i < objects.length ; i ++){
q.setParameter(i, objects[i]);
}
q.executeUpdate();
}
}
service
-----------------------
public interface BaseService<T> {
...
public void executeSQL(String sql,Object...objects);
}
public abstract class BaseServiceImpl<T> implements BaseService<T> {
...
//执行原生的sql语句
public void executeSQL(String sql,Object...objects){
dao.executeSQL(sql, objects);
}
}
1.引入quartz类库
com.springsource.org.quartz-1.6.2.jar
2.创建石英任务
/**
* 使用spring集成的石英调度,动态生成日志表
*/
public class GenerateLogsTableTask extends QuartzJobBean {
//
private LogService logService ;
public void setLogService(LogService logService) {
this.logService = logService;
}
/**
* 执行任务
*/
protected void executeInternal(JobExecutionContext arg0) throws JobExecutionException {
String tableName = LogUtil.generateLogTableName(1);
String sql = "create table if not exists " + tableName + " like logs";
logService.executeSQL(sql);
System.out.println(tableName + " 生成了! " );
tableName = LogUtil.generateLogTableName(2);
sql = "create table if not exists " + tableName + " like logs";
logService.executeSQL(sql);
System.out.println(tableName + " 生成了! " );
}
}
3.配置调度任务
[conf/schedules.xml]
<?xml version="1.0"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd ">
<!-- 任务明细bean,对石英任务包装 -->
<bean id="jobDetailBean" class="org.springframework.scheduling.quartz.JobDetailBean">
<property name="jobClass" value="cn.itcast.surveypark.schedule.GenerateLogsTableTask" />
<property name="jobDataAsMap">
<map>
<entry key="logService" value-ref="logService" />
</map>
</property>
</bean>
<!-- cron触发器bean,设置任务的调度策略的 -->
<bean id="cronTriggerBean" class="org.springframework.scheduling.quartz.CronTriggerBean">
<property name="jobDetail" ref="jobDetailBean" />
<!-- cron表达式 -->
<property name="cronExpression">
<value>0 0 0 15 * ?</value>
</property>
</bean>
<!-- 调度器工厂bean,激活触发器,启动石英任务的 -->
<bean class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
<property name="triggers">
<ref bean="cronTriggerBean"/>
</property>
</bean>
</beans>
4.配置web.xml文件,一同加载spring的所有配置文件
<!-- 通过上下文参数指定spring配置文件的位置 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:beans.xml,classpath:schedules.xml</param-value>
</context-param>
5.修改时间,为触发任务接近的时间,比如2013-1-14 23:59:00秒
6.启动观察效果
使用spring监听器在服务器启动时完成当月日志表和下两个月日志表初始化
-------------------------------------------------------------------
/**
* 初始化日志表监听器
*/
@SuppressWarnings("rawtypes")
@Component
public class IniLogTablesListener implements ApplicationListener{
@Resource
private LogService logService;
public void onApplicationEvent(ApplicationEvent arg0) {
//是否是上下文刷新事件
if(arg0 instanceof ContextRefreshedEvent){
String tableName = LogUtil.generateLogTableName(0);
String sql = "create table if not exists " + tableName + " like logs";
logService.executeSQL(sql);
sql = "create table if not exists " + LogUtil.generateLogTableName(1) + " like logs";
logService.executeSQL(sql);
sql = "create table if not exists " + LogUtil.generateLogTableName(2) + " like logs";
logService.executeSQL(sql);
System.out.println("日志表-"+tableName+",初始化完成");
}
}
}
可扩展性:scalable.伸缩性.
降低数据库存储压力:
分表:整个库的数据量不是很大,但是某个(些)表的数据量较大.