zoukankan      html  css  js  c++  java
  • hutools之批量更新

    public class HutoolTest {
        private static DataSource dataSource = DSFactory.get();  //读取默认路径下的配置文件,数据库连接以及线程池的配置
        private static SqlRunner sqlRunner = SqlRunner.create(dataSource);
    
        public static String count = "select count(1) from company_with_industry where id > 5";
        public static String currentPage = "select max(id) from company_with_industry where id in (select id from (select id from company_with_industry where id>? order by id limit 10000) as ids)";
        public static String select = "select * from company_with_industry where id>? order by id limit 10000";
        public static String update = "update company_with_industry set industry = ? where id = ?";
        private static Queue<Long> queue = new ConcurrentLinkedDeque<>();
        static Map<String, String> map = new HashMap<>();
        static ExecutorService threadPool = Executors.newFixedThreadPool(10);
    
        @Test
        public void test() {
            initQueue();
            readFile();
            updateIndustry();
        }
    
        public static void initQueue() {
            Entity entity = Entity.create("company_with_industry");
            long count = 0;
            long totalPage = 0;
            long currentId = 5;
            try {
                count = sqlRunner.count(entity);
                totalPage = count % 10000 == 0 ? count / 10000 : count / 10000 + 1;
                queue.add(currentId);
                for (int i = 1; i < totalPage; i++) {
                    long id = sqlRunner.queryNumber(currentPage, currentId).longValue();
                    currentId = id;
                    //System.out.println(currentId);
                    queue.add(currentId);
                }
                System.out.println(queue);
            } catch (SQLException e) {
                e.printStackTrace();
            }
            System.out.println(count);
        }
    
        public static void readFile() {
            try {
                BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(new File("行业关键字.txt")), "UTF-8"));
                String line = "";
                while ((line = br.readLine()) != null) {
                    System.out.println(line);
                    String[] ss = line.split("--");
                    for (String keyword : ss[1].split(",")) {
                        map.put(keyword, ss[0]);
                    }
                }
                System.out.println(map);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    
        public static void updateIndustry() {
            while (true) {
                if(queue.size()>0){
                    long currentPage = queue.poll();
                    if (currentPage != 0) {
                        threadPool.execute(() -> {
                            try {
                                List<Entity> list = sqlRunner.query(select, currentPage);
                                Object[][] objects = new Object[1000][2];
                                int count = 0;
                                for (Entity entity : list) {
                                    String entName = entity.getStr("ent_name");
                                    if (!entName.contains("?")) {
                                        for (String keyword : map.keySet()) {
                                            if (entName.contains(keyword)) {
    
                                                objects[count][0] = map.get(keyword);
                                                objects[count][1] =  entity.getLong("id");
                                                count++;
                                                break;
                                            }
                                        }
                                    }
                                    if(count==1000){
                                        sqlRunner.executeBatch(update,objects);
                                        count = 0;
                                        objects = new Object[1000][2];
                                        System.out.println(Thread.currentThread().getName()+"	gank了1000条数据	"+entity.getLong("id"));
                                    }
                                }
                                if(objects.length>0){
                                    sqlRunner.executeBatch(update,objects);
                                    System.out.println(Thread.currentThread().getName()+"	gank了1000条数据	"+list.get(list.size()-1).getLong("id"));
                                }
                            } catch (Exception e) {
                                e.printStackTrace();
                                System.out.println(currentPage);
                            }
    
                        });
    
                    } else {
                        System.out.println("没任务了,休息5秒钟!");
                        try {
                            Thread.currentThread().sleep(5 * 1000);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }
                }
    
            }
        }
    
    }
  • 相关阅读:
    Maven中使用描述文件切换环境配置
    整合MyBatis到Spring中实现Dao层自动装配
    使用MyBatis搭建项目时报 java.io.IOException: Could not find resource
    数据库CPU占用高排查
    JS 根据时区获取时间
    国外服务器 winserver2012 安装IIS后,安装urlrewrite模块总是自动停止应用程序池
    sql中char(9) char(10) char(13)
    通过 Microsoft.Ace.OLEDB 接口导入 EXCEL 到SQLSERVER
    SDL 当前连接查询脚本
    C# System.Drawing.Graphics 画图后,如何保存一个低质量的图片,一个占用空间较小的图片
  • 原文地址:https://www.cnblogs.com/shirandedan/p/9469846.html
Copyright © 2011-2022 走看看