批量插入数据 多线程分批

 分批多线程插入

        int num = 0;
        final ThreadPoolExecutor executor = new ThreadPoolExecutor(4,
                8,
                2, TimeUnit.SECONDS,
                new ArrayBlockingQueue<>(10000), new ThreadPoolExecutor.CallerRunsPolicy());
        executor.allowCoreThreadTimeOut(true);
        int batchSize = 1000;
        int future_size = (availableData.size() + batchSize - 1) / batchSize;
        CountDownLatch countDownLatch = new CountDownLatch(future_size);
        List<Future<Integer>> futures = new ArrayList<>(future_size);
        List<Info> tempList = new ArrayList<>(512);
        for (int i = 0; i < availableData.size(); i++) {
            tempList.add(availableData.get(i));
            //判断批次,够N个数开始处理一波
            if ((i + 1) % batchSize == 0 || (i + 1) == availableData.size()) {
                log.info("处理数据集");
                List<Info> threadList = new ArrayList<>(tempList);
                Future<Integer> future = executor.submit(() -> {
                    int num_t = 0;
                    //todo 你的线程处理,或者数据库操作等
                    for (Info temp : threadList) {
                        try {
                            InfoMapper.addInfo(temp);
                            num_t++;
                        } catch (Exception e) {
                            if (e instanceof DuplicateKeyException) {
                                log.debug("跳过插入重复数据");
                            } else {
                                throw e;
                            }
                        }
                    }
                    threadList.clear();
                    countDownLatch.countDown();
                    return num_t;
                });
                futures.add(future);
                tempList.clear();
            }
        }
        try {
            //设置等待两分钟,两分钟没处理完先返回响应
            countDownLatch.await(2, TimeUnit.MINUTES);
        } catch (InterruptedException e) {
            log.error(e.getMessage(), e);
        }
        for (Future<Integer> future : futures) {
            try {
                num += future.get();
            } catch (InterruptedException | ExecutionException e) {
                log.error(e.getMessage(), e);
            }
        }
        return num;

 简单批量插入

        int num = 0;
        List<List<Info>> list = ListUtils.partition(availableData, 1000);
        SqlSession sqlSession = sqlSessionFactory.openSession(ExecutorType.BATCH, false);
        InfoMapper sqlSession_InfoMapper = sqlSession.getMapper(InfoMapper.class);
        for (List<Info> Infos : list) {
            for (Info availableDatum : Infos) {
                try {
                    sqlSession_InfoMapper.addInfo(availableDatum);
                    num++;
                } catch (Exception e) {
                    if (e instanceof DuplicateKeyException) {
                        log.debug("跳过插入重复数据");
                    } else {
                        throw e;
                    }
                }
            }
            sqlSession.commit();
            sqlSession.clearCache();
        }
        sqlSession.close();
        availableData.clear();
        return num;

两种速度差不多

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值