并发插入数据百万条

1、新闻并发入库数据 

@PostMapping(value = "/api/testnews")
@ApiOperation(value = "测试添加新闻动态")
public void testcreate(Integer start,Integer count) {
    this.newsService.testTo161(start,count);
}
/**
 * 测试大批量添加新闻动态接口
 * @param count
 * @param start
 */
void testTo161(Integer start,Integer count);
public void testTo161(Integer start,Integer count) {
    long stime = System.currentTimeMillis();
    ThreadPoolExecutor executor = new ThreadPoolExecutor(10, 10, 60,
            TimeUnit.MILLISECONDS, new LinkedBlockingQueue<Runnable>(10));

    executor.execute(() -> execAdd(start,count));
    executor.execute(() -> execAdd(start+count+1,count));
    executor.execute(() -> execAdd(start+2*count+1,count));
    executor.execute(() -> execAdd(start+3*count+1,count));
    executor.execute(() -> execAdd(start+4*count+1,count));
    executor.execute(() -> execAdd(start+5*count+1,count));
    executor.execute(() -> execAdd(start+6*count+1,count));
    executor.execute(() -> execAdd(start+7*count+1,count));
    executor.execute(() -> execAdd(start+8*count+1,count));
    executor.execute(() -> execAdd(start+9*count+1,count));
    executor.shutdown();

    logger.info("实际执行任务的线程数"+executor.getActiveCount());
    long etime = System.currentTimeMillis();
    logger.info("耗时" + (etime - stime) / 1000 + "秒");
}

private void execAdd(Integer start,Integer count){
    for (int i = start; i < start+count; i++) {
        News news = new News();
        news.setId(UUID.randomUUID().toString());
        news.setTitle("新闻标题"+i);
        news.setAuthor("新闻作者刘"+i+"某");
        news.setPublishStatus(1);
        news.setSource("新闻来源"+i);
        news.setContent("新闻正文"+i);
        news.setTopOrder(0);
        news.setCreateTime(new Date());
        news.setModifyTime(new Date());
        String json = gson.toJson(news, News.class);
        client.prepareIndex()
                .setIndex("lawfullyadministration.dynamicnewstest")
                .setType(TypeConstant.TYPE)
                .setId(news.getId())
                .setSource(json)
                .execute()
                .actionGet();
        logger.info(news.getId() + "入库161完成");
        logger.info("当前入库第"+i+"条记录");
        if(i == 100000){
            break;
        }
    }
}

 

2、政策并发入库数据

@PostMapping(value = "/api/testpolicys")
@ApiOperation(value = "测试添加政策")
public void testcreate(Integer start,Integer count) {
    this.policyService.testTo161(start,count);
}
/**
 * 测试大批量添加接口
 * @param count
 * @param start
 */
void testTo161(Integer start,Integer count);
public void testTo161(Integer start,Integer count) {
    long stime = System.currentTimeMillis();
    ThreadPoolExecutor executor = new ThreadPoolExecutor(10, 10, 60,
            TimeUnit.MILLISECONDS, new LinkedBlockingQueue<Runnable>(10));

    executor.execute(() -> execAdd(start,count));
    executor.execute(() -> execAdd(start+count+1,count));
    executor.execute(() -> execAdd(start+2*count+1,count));
    executor.execute(() -> execAdd(start+3*count+1,count));
    executor.execute(() -> execAdd(start+4*count+1,count));
    executor.execute(() -> execAdd(start+5*count+1,count));
    executor.execute(() -> execAdd(start+6*count+1,count));
    executor.execute(() -> execAdd(start+7*count+1,count));
    executor.execute(() -> execAdd(start+8*count+1,count));
    executor.execute(() -> execAdd(start+9*count+1,count));
    executor.shutdown();

    logger.info("实际执行任务的线程数"+executor.getActiveCount());
    long etime = System.currentTimeMillis();
    logger.info("耗时" + (etime - stime) / 1000 + "秒");
}

private void execAdd(Integer start,Integer count){
    for (int i = start; i < start+count; i++) {
        if(i == 1000000){
            break;
        }
        Policy policy = new Policy();
        policy.setTitle("政策标题" + i);
        policy.setId(UUID.randomUUID().toString());
        policy.setArticleNumber("北京人力资源保障局[2019]" + i + "号");
        policy.setDraftOffice("" + i);
        policy.setPublishStatus(1);
        policy.setPublishDate(new Date());
        policy.setContent("正文" + i);
        policy.setFileId(("文件名" + i).hashCode() + "");
        policy.setFileName("文件名" + i+".doc");
        policy.setPerformDate(new Date());
        policy.setTopicCategory("topic" + i);
        policy.setValidity("现行有效");
        policy.setValidityGrade("地方性法规");
        policy.setCreateTime(new Date());
        policy.setModifyTime(new Date());
        String json = gson.toJson(policy, Policy.class);
        client.prepareIndex()
                .setIndex(/*IndexConstant.POLICY*/"lawfullyadministration.policytest")
                .setType(TypeConstant.TYPE)
                .setId(policy.getId())
                .setSource(json)
                .execute()
                .actionGet();
        logger.info(policy.getId() + "入库161完成");
        logger.info("当前入库第"+policy.getDraftOffice()+"条记录");
    }
}

3、报告并发入库数据

@PostMapping(value = "/api/testreports")
@ApiOperation(value = "测试添加报告")
public void testcreate(Integer start,Integer count) {
    this.reportService.testTo78(start,count);
}

 

/**
 * 测试大批量添加接口
 * @param count
 * @param start
 */
void testTo78(Integer start,Integer count);
private static final Logger logger = LoggerFactory.getLogger(ReportServiceImpl.class);

@Override
public void testTo78(Integer start,Integer count) {
    long stime = System.currentTimeMillis();
    ThreadPoolExecutor executor = new ThreadPoolExecutor(10, 10, 60,
            TimeUnit.MILLISECONDS, new LinkedBlockingQueue<Runnable>(10));

    executor.execute(() -> execAdd(start,count));
    executor.execute(() -> execAdd(start+count+1,count));
    executor.execute(() -> execAdd(start+2*count+1,count));
    executor.execute(() -> execAdd(start+3*count+1,count));
    executor.execute(() -> execAdd(start+4*count+1,count));
    executor.execute(() -> execAdd(start+5*count+1,count));
    executor.execute(() -> execAdd(start+6*count+1,count));
    executor.execute(() -> execAdd(start+7*count+1,count));
    executor.execute(() -> execAdd(start+8*count+1,count));
    executor.execute(() -> execAdd(start+9*count+1,count));
    executor.shutdown();

    logger.info("实际执行任务的线程数"+executor.getActiveCount());
    long etime = System.currentTimeMillis();
    logger.info("耗时" + (etime - stime) / 1000 + "秒");
}

private void execAdd(Integer start,Integer count){
    for (int i = start; i < start+count; i++) {
        if(i == 1000000){
            break;
        }
        Report report = new Report();
        report.setTitle("报告标题" + i);
        report.setId(UUID.randomUUID().toString());
        report.setPublishStatus(1);
        report.setTopOrder(0);
        report.setSource("报告来源"+i);
        report.setAuthor("报告作者"+i);
        report.setContent("报告正文" + i);
        report.setFileId(("报告文件名" + i).hashCode() + "");
        report.setFileName("报告文件名" + i+".doc");
        report.setCreateTime(new Date());
        report.setModifyTime(new Date());
        String json = gson.toJson(report, Report.class);
        client.prepareIndex()
                .setIndex("lawfullyadministration.report")
                .setType(TypeConstant.TYPE)
                .setId(report.getId())
                .setSource(json)
                .execute()
                .actionGet();
        logger.info(report.getId() + "入库78完成");
        logger.info("当前入库第"+i+"条记录");
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值