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()+"\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之批量更新的更多相关文章
- SQL批量更新 关系表更新
很多人在做数据的批量更新时..如果更新的内容是从其他表查出来的..很容易这么写.. UPDATE TABLE1 SET COLUMN1=(SELECT SUM(SOMETHING) FROM TABL ...
- SQL 将2张不相关的表拼接成2列,批量更新至另一张表
update SO_Master set LotteryNo=t2.LotteryNo,UpdateTime=GETDATE() --select sm.LotteryNo,sm.SysNo,t2.L ...
- [PDO绑定参数]使用PHP的PDO扩展进行批量更新操作
最近有一个批量更新数据库表中某几个字段的需求,在做这个需求的时候,使用了PDO做参数绑定,其中遇到了一个坑. 方案选择 笔者已知的做批量更新有以下几种方案: 1.逐条更新 这种是最简单的方案,但无疑也 ...
- Ado.net[登录,增删改查,Get传值,全选,不选,批量删除,批量更新]
[虽然说,开发的时候,我们可以使用各种框架,ado.net作为底层的东西,作为一个合格的程序员,在出问题的时候我们还是要知道如何调试] 一.增删改查 cmd.ExecuteReader();执行查询, ...
- MongoDB学习笔记~大叔分享批量添加—批量更新—批量删除
回到目录 说它是批量操作,就是说将集合对象一次提交到服务器,并对数据进行持久化,如果您的代码是一次一次的提交,那不算是批量操作!在之前的mongodb仓储中并没有对批量更新和批量删除进行实现,而今天在 ...
- jdbc-批量插入、批量删除、批量更新
一.JDBC的批量插入 JDBC批量插入主要用于数据导入和日志记录因为日志一般都是先写在文件下的等. 我用Mysql5.1.5的JDBC driver 分别对三种比较常用的方法做了测试 方法 ...
- MYSQL 处理批量更新数据的一些经验。
首先,我们需要了解下MYSQL CASE EXPRESSION 语法. 手册传送门:http://dev.mysql.com/doc/refman/5.7/en/control-flow-functi ...
- mybatis执行批量更新update
Mybatis的批量插入这里有http://ljhzzyx.blog.163.com/blog/static/38380312201353536375/.目前想批量更新,如果update的值是相同的话 ...
- postgres 批量更新内容
在程序中遇到这样的需求, 数据库表格式如下 需要把批量更新status, 如name = fox 时, status = 1, name = boa 时,status = 2 .... 类似的 pos ...
随机推荐
- Nmap使用手册参数详细说明
nmap –iflist : 查看本地主机的接口信息和路由信息 -A :选项用于使用进攻性方式扫描 -T4: 指定扫描过程使用的时序,总有6个级别(0-5),级别越高,扫描速度越快,但也容易被防火 ...
- 【CodeForces - 939A】Love Triangle(模拟)
Love Triangle Descriptions: 正如你所知道的,没有男性飞机也没有女性飞机.然而,地球上的每一个平面都喜欢另一个平面.地球上有n个平面,编号从1到n,编号i的平面喜欢编号fi的 ...
- Vue.js 使用 $refs 定位 DOM 出现 undefined
找到这篇文章,写得不错,记录一下.https://www.jianshu.com/p/090937a480b5
- Mysql——通配符和正则表达式的使用
1.like操作符和百分号通配符 %表示任何字符出现任意次数. 查询出表TABLE中NAME字段中任意位置包含i的行: select * from TABLE where NAME like '%i% ...
- ArrayList类的set()方法
ArrayList类的set()方法用于更新指定位置的内容,若内容是new出来的,则需要调用该set()方法:否则,不需要调用该set()方法,示例如下 User.java public class ...
- ES简介及特点
1.ES是什么? ES是一个高度可伸缩的开源的全文检索和分析引擎,它允许你以近实时的方式快速存储.搜索.分析大量数据,ES是基于Lucence开发,隐藏其复杂性,提供了简单易用的restful api ...
- PHPexcel使用 技巧
phpexcel不用多说了 导出表格时经常会用到 本帖主要记录一下几个经常用到的操作 # 设置自动换行 $PHPExcel->getActiveSheet()->getStyle(&q ...
- k8s-高可用架构设计
docker的私有仓库harbor.容器化kubernetes部分组建.使用阿里云日志服务收集日志. 部署完成后,你将理解系统各组件的交互原理,进而能快速解决实际问题,所以本文档主要适合于那些有一定k ...
- 使用python的ctypes库实现内存的动态申请和释放
1.申请前内存占用情况 2.申请内存 from ctypes import * import time #在这里申请1G的内存,单位k mem = create_string_buffer(1024* ...
- 15条SQLite3语句
15条SQLite3语句 转自:http://www.thegeekstuff.com/2012/09/sqlite-command-examples/ SQLite3 is very light ...