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