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. 条件DCGAN(2019/09/10)

    最近看到keras的官方GAN代码中有CGAN(全连接层)和卷积GAN(DCGAN),但他并没有给出“条件卷积GAN”,预测就把这两者结合了一下.虽然很多人用其他框架(e.g.TensorFlow)写 ...

  2. 非常好用的vue数字滚动插件vue-countTo

    参考链接:https://blog.csdn.net/gaoxin666/article/details/84635056

  3. 在vue中使用Normalize初始化样式

    参考链接:https://www.jianshu.com/p/34533b45aac1

  4. sha256算法原理

    1. SHA256简介 SHA256是SHA-2下细分出的一种算法 SHA-2下又可再分为六个不同的算法标准 包括了:SHA-224.SHA-256.SHA-384.SHA-512.SHA-512/2 ...

  5. Thinkphp 使用小结

    分页中带查询参数 ...->paginate(15,false,['query'=>request()->param()]); 队列后台自动开启运行 nohup php think ...

  6. Oracle的基本操作-序列的使用

    序列:默认从1开始,一次递增,主要用来给主键赋值使用 create sequence s_person; select s_person.nextval from dual; --dual是一张虚表, ...

  7. COleVariant功能

    COLeVariant是数据库常用到的数据类型.它可以是字符串,整型值,日期等.知道怎样将它转换为CString. COLeVariant类是对VARIANT结构的封装.它的构造函数具有极为强大的功能 ...

  8. 题解 洛谷P1311 【选择客栈】

    可能这做法是最奇葩的ST表 我们枚举k,计算每种色调的客栈各有多少种方法  我们利用一种奇怪的思想:除了不可行的,剩下的都是可行的 所以我们先求出 每种颜色的客栈随机选择两个,一共有多少种结果 接着减 ...

  9. MySQL-复杂查询及条件-起别名-多表查询-04

    目录 基本查询语句及方法 测试数据创建 创建数据库与表 插入表记录数据 数据展示 常见结果排版 另一种结果排版 \G 简单查询语句的书写与执行顺序 查询语句书写 执行顺序 科普-- 起别名 写法 可以 ...

  10. flask 接收参数小坑

    前后端分离: 1.get方式: items = dict(request.args.items()) app_name = items["app_name"].strip() 或 ...