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 ...
随机推荐
- 并查集 --以cogs259为例
题目链接:http://cogs.pro:8081/cogs/problem/problem.php?pid=pySmxSVgP [问题描述] 或许你并不知道,你的某个朋友是你的亲戚.他可能是 ...
- 【VS开发】【C/C++开发】传递双重指针申请内存,典型用法
传递双重指针申请内存,典型用法 指针参数是如何传递内存的? 如果函数的参数是一个指针,不要指望用该指针去申请动态内存.如下示例中,Test函数的语句GetMemory(str, 100)并没有使str ...
- 【VS开发】单文档中往视图中加入控件
[VS开发]单文档中往视图中加入控件 标签(空格分隔): [VS开发] 分隔视图的但文档窗口,要显示控件,推荐使用CFormView或者CCtrlView,前者和对话框的做法一致. 在MainFram ...
- IDEA 2019.2破解激活教程(激活到2089年8月,亲测有效,持续更新中...)
本来笔者这边是有个正版激活码可以使用的,但是,2019.9月3号的时候,一些小伙伴反映这个注册码已经失效了,于是拿着自己的 IDEA, 赶快测试了一下,果不其然,已然是不能用了. 好在,笔者又找到了新 ...
- 如何禁止谷歌浏览器隐藏url的www前缀
若要将Chrome浏览器的设置恢复为隐藏HTTP.HTTPS以及WWW前缀,则只需再次进入此页面: chrome://flags/#omnibox-ui-hide-steady-state-url-s ...
- Design Phone Directory
Design a Phone Directory which supports the following operations: get: Provide a number which is not ...
- Smarty内置函数之capture
capture的作用是: 捕获模板输出的数据并将其存储到一个变量,而不是把它们输出到页面,任何在 {capture name="foo"}和{/capture}之间的数据将被存储到 ...
- Jmeter之压测探索和结果分析
1.copy过来的,很有道理的一句话~ 最大并发数:取决于你的业务类型,数据量,处理时的资源需求等,具体多少,需要做一些性能测试来衡量 确定待测试的场景,设计脚本,不断增加并发数量. 2.CPU压不上 ...
- Pytest+allure安装和框架搭建
接口自动化框架搭建 -- 公司系统自测使用,只跑核心业务流程 编辑中...... 1.安装Pytest pip install -U pytest 1.1Pycharm测试脚本运行 创建project ...
- 8-Perl 哈希
1.Perl 哈希哈希是 key/value 对的集合.Perl中哈希变量以百分号 (%) 标记开始.访问哈希元素格式:${key}.以下是一个简单的哈希实例:#!/usr/bin/perl%data ...