java处理大数据量分批入库


描述,说吧数据也不算大,几十万,只可惜服务器和数据库以及项目优化不够,延迟贼高,所以搞个分批入库,解决下速度。直接开始撸。

声明:代码中业务逻辑忽略,公用这些数组拆分,时间比较可以采用。另附上,mybatis的xml入库语句。
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;


@Component
public class CollisionJob {
    private PbdLogger logger = PbdLogger.getLogger(CollisionJob.class);
    @Autowired
    private VehicleDao vehicleDao;
    @Autowired
    private AcrossVehicleDao acrossVehicleDao;
    //@Scheduled(cron="0 10 12 ? * *") 
    @Scheduled(fixedDelay=5000)
     public void insertPasslog() throws InterruptedException {
         logger.info("开始拉取车辆表数据");
         SimpleDateFormat simple=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
         Map<String,String> map=new HashMap<String,String>();
        //截止时间为当前时间
         String endTime=ToolDateTime.getFormatDate();
         //开始时间为当前时间推两个月
         String startTime=ToolDateTime.getAnyDate(endTime, 0, 0, -3,ToolDateTime.PATTERN_YMD_24);
         String oneTime="";
         String twoTime="";
        //每次同步前查询mysql库表中对应数据,拿到最小和最大时间,然后去到距离当前时间两个月的最终时间,如果最小时间早于最终时间,把之前的数据删掉。
         Map<String, String> tim=vehicleDao.queryMaxMinTime();
         //如果为空代表第一次入库,时间用当前时间和两月之内
         if(tim==null||tim.isEmpty()) {
             map.put("startTime",startTime);
             map.put("endTime",endTime);
         }else {
             for(Map.Entry<String, String> mp:tim.entrySet()) {
                 if("maxTime".equals(mp.getKey())) {
                     oneTime=simple.format(mp.getValue());
                 }else if("minTime".equals(mp.getKey())) {
                     twoTime=simple.format(mp.getValue());
                 }
             }
             //如果不为空,需要将不再两个月内的数据清除,执行对应语句
             //先对比取到的最小时间是否小于两个月内
            boolean times= compareTime(twoTime,startTime);
            if(times) {
                //为真就清除小于两个月的数据
                vehicleDao.deleteTimMonth(startTime);
            }
             map.put("startTime",oneTime);
             map.put("endTime",endTime);
         }
         List<AcrossVehicle> list=acrossVehicleDao.queryTwoMonth(map);
         int numzhi=list.size();
         if(numzhi>300) {
             numzhi=300;
         }
         List<List<AcrossVehicle>> array=createList(list,numzhi);
         for(int i=0;i<array.size();i++) {
             List<AcrossVehicle> hello=array.get(i);
            //清空之后开始添加数据
                vehicleDao.insertPasslogMysql(hello);
                logger.info("开始同步到mysql库");
                 System.out.println("添加数量"+hello.size());
                 logger.info("同步mysql结束");
         }
         logger.info("开始拉取车辆表数据结束"+list.size());
     }
     public static List<List<AcrossVehicle>>  createList(List<AcrossVehicle> targe,int size) {
            List<List<AcrossVehicle>> listArr = new ArrayList<List<AcrossVehicle>>();
            //获取被拆分的数组个数
            int arrSize = targe.size()%size==0?targe.size()/size:targe.size()/size+1;
            for(int i=0;i<arrSize;i++) {
                List<AcrossVehicle>    sub = new ArrayList<AcrossVehicle>();
                //把指定索引数据放入到list中
                for(int j=i*size;j<=size*(i+1)-1;j++) {
                    if(j<=targe.size()-1) {
                        sub.add(targe.get(j));
                    }
                }
                listArr.add(sub);
            }
            return listArr;
        }
     public static boolean compareTime(String one,String two) {
         boolean isTime=true;
         DateFormat sim=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        try {
            if(sim.parse(one).getTime()<sim.parse(two).getTime()) {
                return isTime;
            }
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return false;
     }
}

===========================
<insert id="insertPasslogMysql" parameterType="java.util.List">
        insert into t_vehicle_passlog_mysql 
        (id,villageCode,tollgateID,cameraId,recordId,plateNumber,
        inOutTime,inOutType,channelName,platePic,rowTime,mrowTime) 
        values 
        <foreach collection="list" item="item" index="index" separator=",">
            (#{item.id},#{item.villageCode},#{item.tollgateID},#{item.cameraId},#{item.recordId},
            #{item.plateNumber},#{item.inOutTime},
            #{item.inOutType},#{item.channelName},#{item.platePic},#{item.rowTime},#{item.mrowTime})
        </foreach>
        ON DUPLICATE KEY UPDATE 
        id=VALUES(id),villageCode=VALUES(villageCode),tollgateID=VALUES(tollgateID),cameraId=VALUES(cameraId),
        recordId=VALUES(recordId),plateNumber=VALUES(plateNumber),inOutTime=VALUES(inOutTime),inOutType=VALUES(inOutType)
        ,channelName=VALUES(channelName),platePic=VALUES(platePic),rowTime=VALUES(rowTime),mrowTime=VALUES(mrowTime)
    </insert>

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM