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. 【VS开发】修改窗口背景颜色大全

    如何修改frame窗口的背景颜色?  MDI窗口的客户区是由frame窗口拥有的另一个窗口覆盖的.为了改变frame窗口背景的颜色,只需要这个客户区的背景颜色就可以了.你必须自己处理WM_ERASEB ...

  2. vue-cli3 取消eslint 校验代码 真正的解决办法

    在网上找了各种办法都没解决,看了下文档就解决了 关闭vue-cli3.0 报错:eslint-disable-next-line to ignore the next line.   注意我这里是VU ...

  3. 使用zookeeper作为分布式锁以及设计一种通知监听模式

    1.创建实例/** * 初始化单例的便捷方法 */ public static void init() { getInstance(); } /** * 获取单例 * @return */ publi ...

  4. [转帖]Breeze部署kubernetes1.13.2高可用集群

    Breeze部署kubernetes1.13.2高可用集群 2019年07月23日 10:51:41 willblog 阅读数 673 标签: kubernetes 更多 个人分类: kubernet ...

  5. 【一个蒟蒻的挣扎】最小生成树—Kruskal算法

    济南集训第五天的东西,这篇可能有点讲不明白提前抱歉(我把笔记忘到别的地方了 最小生成树 概念:一个有 n 个结点的连通图的生成树是原图的极小连通子图,且包含原图中的所有 n 个结点,并且有保持图连通的 ...

  6. 输出1-n的全排列dfs

     https://ac.nowcoder.com/acm/contest/998/C #include<stdio.h> #include<iostream> #include ...

  7. golang net包学习笔记

    阅读源代码发现在net包中主要实现了ip.tcp.udp.unix等通信方式.它们大致可以分成两大类:其一,ip.udp.unix(DGRAM),这是一些无链接的协议,其二,tcp.unix(STRE ...

  8. go 表单

    package main import ( "fmt" "io" "net/http" ) const form = `<html&g ...

  9. mybatis数组和集合的长度判断及插入

    1.在使用foreach的是collection属性,该属性是必须指定的,但是在不同情况下,该属性的值是不一样的,主要有一下4种情况: 如果传入的是单参数且参数类型是一个List的时候,collect ...

  10. Java super与this

    一.super()与this() 1.super(): 只能出现在子类构造器中 且必须位于第一行. 所有子类构造器默认调用super()方法 参数决定使用父类哪一个构造器. 若父类没有相应的构造器,则 ...