使用多线程及线程池批量拷贝数据到MongoDB
2021/10/25 19:11:01
本文主要是介绍使用多线程及线程池批量拷贝数据到MongoDB,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
文章目录
- 前言
- 一、原来代码
- 二、改进后代码
- 1.使用new Thread方式
- 2.使用Runnable接口
- 3.使用线程池
- 总结
提示:本文仅供学习交流,请勿用于非法活动!
前言
本文大概内容:
例如:随着MongoDB的广泛应用,电商用到MongoDB也越来越多。本文主要是在将购物车模块切换到MongoDB框架前,如何快速将Mysql中购物车大批量订单拷贝到MongoDB数据库中?
一、原来代码
如下,我们将拷贝100万条数据到MongoDB中。
public void copyCartToMongo() { List<Cart> carts = cartMapper.selectAll(); if(carts.size() >0 ){ cartMongoService.saveCartList(carts); } }
二、改进后代码
public void copyCartToMongoByThread() { // 1.我们先将批量查询及拷贝的数据分批 EntityWrapper<Cart> wrapper = SQLHelper.buildEmptyWrapper(Cart.class); long count = cartMapper.selectCount(SQLHelper.build(Cart.class).geEntityWrapper()); int preCount = 100; int operateCount = 0; int num = operateCount = (int)count / preCount; if(count % preCount == 0){ operateCount = num; }else { operateCount = num + 1; } for(int i=0;i<operateCount;i++){ logger.info("保存或更新第"+(i+1)*preCount+"条数据"); Page<Cart> page = new Page<>(i,preCount); List<Cart> cartList = cartMapper.selectPage(page, wrapper); // 2.通过多线程、线程池(其他替换即可) this.copyCartToMongoThread(cartList); } }
上述代码中有以下三种方式实现:
1.使用new Thread方式
代码如下:
public void copyCartToMongoThread(List<Cart> cartList){ final CountDownLatch latch = new CountDownLatch(cartList.size()); try{ for(Cart cart:cartList){ Thread thread = new Thread(() -> { cartMongoService.saveCart(cart); latch.countDown(); }); thread.start(); } latch.await(); } catch (Exception e) { e.printStackTrace(); } }
2.使用Runnable接口
代码如下:
public void copyCartToMongoThread(List<Cart> cartList){ final CountDownLatch latch = new CountDownLatch(cartList.size()); try { for (Cart cart:cartList){ new Thread(new Runnable() { @Override public void run() { cartMongoService.saveCart(cart); latch.countDown(); } }).start(); } latch.await(); } catch (InterruptedException e) { e.printStackTrace(); } }
3.使用线程池
代码如下:
public void copyCartToMongoThreadPool(List<Cart> cartList){ final CountDownLatch latch = new CountDownLatch(cartList.size()); // 配置线程池个数 ExecutorService executorService = Executors.newFixedThreadPool(1); try{ for(Cart cart:cartList){ executorService.submit(new Runnable() { @Override public void run() { try{ cartMongoService.saveCart(cart); // 一直阻塞当前线程,直到计数器值为0,保证并发 latch.await(); } catch (Exception e) { e.printStackTrace(); } } }); latch.countDown(); } } catch (Exception e) { e.printStackTrace(); } }
总结
总结:
以上就是批量拷贝数据到MongoDB的三种改进方法。
如果什么问题欢迎留言、关注,相互交流、学习!
这篇关于使用多线程及线程池批量拷贝数据到MongoDB的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-06-26解决google chrome helper 内存占用较高!
- 2024-04-01got an unexpected keyword argument
- 2024-03-30维多利亚的秘密 golang入坑系统
- 2024-03-29mongodb sort by date
- 2024-03-29go swagger
- 2024-03-25mongodb cdc
- 2024-03-25how to use go in vscode
- 2024-03-22mongooseserverselectionerror: connect econnrefused ::1:27017
- 2024-03-21pymongo insert_many
- 2024-03-18projection mongodb