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();
}
}
} }
} }

最新文章

  1. tornado学习笔记14 HTTP1ServerConnection分析
  2. AWS EC2首次使用VPS
  3. Python 第五天 模块(2)
  4. svg琐碎01
  5. JavaScript基础之函数与数组
  6. SqlServer ,storedprocedure操作
  7. ATL一:CWindowImpl
  8. Ajax实现页面后台button click事件无刷新弹窗
  9. Ubuntu 13.10 Mono安装历程
  10. php中echo(),print(),print_r()用法
  11. ModelSerializer Meta设置
  12. Mysql 和 SQLServer 使用SQL差异比较
  13. Spring中,applicationContext.xml 配置文件在web.xml中的配置详解
  14. Java语言访问Redis数据库之Set篇
  15. Spark实现K-Means算法
  16. Xshell中vim退出内容仍停留在屏幕的问题
  17. js-ES6学习笔记-Class(4)
  18. 【代码笔记】iOS-手机系统版本
  19. Linux性能优化之磁盘优化(三)
  20. cocos2d-js 自定义事件监听派发

热门文章

  1. 微信小程序实现navbar导航栏
  2. VMware HorizonView虚拟化桌面TLS问题处理
  3. ES-基本概念
  4. hbase与hdfs的交互
  5. Log4j2配置之Appender详解
  6. 拨开Python迷雾
  7. Unknown custom element: &lt;swiper&gt;
  8. .Net面试题一
  9. 5.Shell变量
  10. java中的接口和php的接口的区别