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()+"\tgank了1000条数据\t"+entity.getLong("id"));
}
}
if(objects.length>0){
sqlRunner.executeBatch(update,objects);
System.out.println(Thread.currentThread().getName()+"\tgank了1000条数据\t"+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();
}
}
} }
} }

hutools之批量更新的更多相关文章

  1. SQL批量更新 关系表更新

    很多人在做数据的批量更新时..如果更新的内容是从其他表查出来的..很容易这么写.. UPDATE TABLE1 SET COLUMN1=(SELECT SUM(SOMETHING) FROM TABL ...

  2. SQL 将2张不相关的表拼接成2列,批量更新至另一张表

    update SO_Master set LotteryNo=t2.LotteryNo,UpdateTime=GETDATE() --select sm.LotteryNo,sm.SysNo,t2.L ...

  3. [PDO绑定参数]使用PHP的PDO扩展进行批量更新操作

    最近有一个批量更新数据库表中某几个字段的需求,在做这个需求的时候,使用了PDO做参数绑定,其中遇到了一个坑. 方案选择 笔者已知的做批量更新有以下几种方案: 1.逐条更新 这种是最简单的方案,但无疑也 ...

  4. Ado.net[登录,增删改查,Get传值,全选,不选,批量删除,批量更新]

    [虽然说,开发的时候,我们可以使用各种框架,ado.net作为底层的东西,作为一个合格的程序员,在出问题的时候我们还是要知道如何调试] 一.增删改查 cmd.ExecuteReader();执行查询, ...

  5. MongoDB学习笔记~大叔分享批量添加—批量更新—批量删除

    回到目录 说它是批量操作,就是说将集合对象一次提交到服务器,并对数据进行持久化,如果您的代码是一次一次的提交,那不算是批量操作!在之前的mongodb仓储中并没有对批量更新和批量删除进行实现,而今天在 ...

  6. jdbc-批量插入、批量删除、批量更新

    一.JDBC的批量插入 JDBC批量插入主要用于数据导入和日志记录因为日志一般都是先写在文件下的等.    我用Mysql5.1.5的JDBC driver 分别对三种比较常用的方法做了测试   方法 ...

  7. MYSQL 处理批量更新数据的一些经验。

    首先,我们需要了解下MYSQL CASE EXPRESSION 语法. 手册传送门:http://dev.mysql.com/doc/refman/5.7/en/control-flow-functi ...

  8. mybatis执行批量更新update

    Mybatis的批量插入这里有http://ljhzzyx.blog.163.com/blog/static/38380312201353536375/.目前想批量更新,如果update的值是相同的话 ...

  9. postgres 批量更新内容

    在程序中遇到这样的需求, 数据库表格式如下 需要把批量更新status, 如name = fox 时, status = 1, name = boa 时,status = 2 .... 类似的 pos ...

随机推荐

  1. 调用存储在session属性里的东西

    将对象放在session里面 request.getSession().setAttribute("username", username); //放到会话里 永EL表达式调用 $ ...

  2. vim 显示行号 查找的命令简单总结

    1. linux vim 进行查找的方法 在command 模式下面 输入 /what-you-search 就可以搜索 注意  n 是向下查找 N 是向上查找 不想搜索了 输入 :set nol 就 ...

  3. Wordpress 所有 hook 钩子

    muplugins_loaded 在必须使用的插件加载之后. registered_taxonomy 对于类别,post_tag 等 Registered_post_type 用于帖子,页面等 plu ...

  4. hive的hiveserver2模式启动不起来,发现Hadoop一直处于安全模式

    hive的hiveserver2模式启动不起来,发现Hadoop一直处于安全模式 命令介绍 命令hadoop fs –safemode get 查看安全模式状态 命令hadoop fs –safemo ...

  5. Hadoop集群搭建-05安装配置YARN

    Hadoop集群搭建-04安装配置HDFS  Hadoop集群搭建-03编译安装hadoop Hadoop集群搭建-02安装配置Zookeeper Hadoop集群搭建-01前期准备 先保证集群5台虚 ...

  6. 【LOJ】#3085. 「GXOI / GZOI2019」特技飞行

    LOJ#3085. 「GXOI / GZOI2019」特技飞行 这显然是两道题,求\(C\)是一个曼哈顿转切比雪夫后的线段树扫描线 求\(AB\),对向交换最大化和擦身而过最大化一定分别为最大值和最小 ...

  7. 区间dp之 "石子合并"系列(未完结)

    A. 石子合并<1> 内存限制:128 MiB 时间限制:1000 ms 标准输入输出 题目类型:传统评测 方式:文本比较   题目描述 有N堆石子排成一排(n<=100),现要将石 ...

  8. dash shell 的一些总结

    最近写个一些dash shell 相关的代码,中间遇到了一些坑以及需要注意的地方,记录一下 1 参数 numberofargmuments(){ echo "The number of ar ...

  9. 学习笔记--Tarjan算法之割点与桥

    前言 图论中联通性相关问题往往会牵扯到无向图的割点与桥或是下一篇博客会讲的强连通分量,强有力的\(Tarjan\)算法能在\(O(n)\)的时间找到割点与桥 定义 若您是第一次了解\(Tarjan\) ...

  10. 记录FTPClient超时处理的相关问题(转)

    https://www.cnblogs.com/dasusu/p/10006899.html 记录 FTPClient 超时处理的相关问题   apache 有个开源库:commons-net,这个开 ...