自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

干貨分享:利用Java多線程技術(shù)導(dǎo)入數(shù)據(jù)到Elasticsearch

新聞 前端
作者花了3天的時(shí)間,利用java線程池框架Executors中的FixedThreadPool線程池重寫了MTE導(dǎo)入工具,單臺服務(wù)器導(dǎo)入效率提高十幾倍(合理調(diào)整線程數(shù)據(jù),效率更高)。

 前言

干貨分享:利用java多線程技術(shù)導(dǎo)入數(shù)據(jù)到Elasticsearch

近期接到一個(gè)任務(wù),需要改造現(xiàn)有從mysql往Elasticsearch導(dǎo)入數(shù)據(jù)MTE(mysqlToEs)小工具,由于之前采用單線程導(dǎo)入,千億數(shù)據(jù)需要兩周左右的時(shí)間才能導(dǎo)入完成,導(dǎo)入效率非常低。所以樓主花了3天的時(shí)間,利用java線程池框架Executors中的FixedThreadPool線程池重寫了MTE導(dǎo)入工具,單臺服務(wù)器導(dǎo)入效率提高十幾倍(合理調(diào)整線程數(shù)據(jù),效率更高)。

關(guān)鍵技術(shù)棧

  • Elasticsearch
  • jdbc
  • ExecutorService\Thread
  • sql

工具說明

maven依賴

  1. <dependency> 
  2.  <groupId>mysql</groupId> 
  3.  <artifactId>mysql-connector-java</artifactId> 
  4.  <version>${mysql.version}</version> 
  5. </dependency> 
  6. <dependency> 
  7.  <groupId>org.elasticsearch</groupId> 
  8.  <artifactId>elasticsearch</artifactId> 
  9.  <version>${elasticsearch.version}</version> 
  10. </dependency> 
  11. <dependency> 
  12.  <groupId>org.elasticsearch.client</groupId> 
  13.  <artifactId>transport</artifactId> 
  14.  <version>${elasticsearch.version}</version> 
  15. </dependency> 
  16. <dependency> 
  17.  <groupId>org.projectlombok</groupId> 
  18.  <artifactId>lombok</artifactId> 
  19.  <version>${lombok.version}</version> 
  20. </dependency> 
  21. <dependency> 
  22.  <groupId>com.alibaba</groupId> 
  23.  <artifactId>fastjson</artifactId> 
  24.  <version>${fastjson.version}</version> 
  25. </dependency> 

java線程池設(shè)置

默認(rèn)線程池大小為21個(gè),可調(diào)整。其中POR為處理流程已辦數(shù)據(jù)線程池,ROR為處理流程已閱數(shù)據(jù)線程池。

  1. private static int THREADS = 21
  2. public static ExecutorService POR = Executors.newFixedThreadPool(THREADS); 
  3. public static ExecutorService ROR = Executors.newFixedThreadPool(THREADS); 

定義已辦生產(chǎn)者線程/已閱生產(chǎn)者線程:ZlPendProducer/ZlReadProducer

  1. public class ZlPendProducer implements Runnable { 
  2.  ... 
  3.  @Override 
  4.  public void run() { 
  5.  System.out.println(threadName + "::啟動..."); 
  6.  for (int j = 0; j < Const.TBL.TBL_PEND_COUNT; j++) 
  7.  try { 
  8.  .... 
  9.  int size = 1000
  10.  for (int i = 0; i < count; i += size) { 
  11.  if (i + size > count) { 
  12.  //作用為size***沒有100條數(shù)據(jù)則剩余幾條newList中就裝幾條 
  13.  size = count - i; 
  14.  } 
  15.  String sql = "select * from " + tableName + " limit " + i + ", " + size; 
  16.  System.out.println(tableName + "::sql::" + sql); 
  17.  rs = statement.executeQuery(sql); 
  18.  List<HistPendingEntity> lst = new ArrayList<>(); 
  19.  while (rs.next()) { 
  20.  HistPendingEntity p = PendUtils.getHistPendingEntity(rs); 
  21.  lst.add(p); 
  22.  } 
  23.  MteExecutor.POR.submit(new ZlPendConsumer(lst)); 
  24.  Thread.sleep(2000); 
  25.  } 
  26.  .... 
  27.  } catch (Exception e) { 
  28.  e.printStackTrace(); 
  29.  } 
  30.  } 
  31. public class ZlReadProducer implements Runnable { 
  32.  ...已閱生產(chǎn)者處理邏輯同已辦生產(chǎn)者 

定義已辦消費(fèi)者線程/已閱生產(chǎn)者線程:ZlPendConsumer/ZlReadConsumer

  1. public class ZlPendConsumer implements Runnable { 
  2.  private String threadName; 
  3.  private List<HistPendingEntity> lst; 
  4.  public ZlPendConsumer(List<HistPendingEntity> lst) { 
  5.  this.lst = lst; 
  6.  } 
  7.  @Override 
  8.  public void run() { 
  9.  ... 
  10.  lst.forEach(v -> { 
  11.  try { 
  12.  String json = new Gson().toJson(v); 
  13.  EsClient.addDataInJSON(json, Const.ES.HistPendDB_Index, Const.ES.HistPendDB_type, v.getPendingId(), null); 
  14.  Const.COUNTER.LD_P.incrementAndGet(); 
  15.  } catch (Exception e) { 
  16.  e.printStackTrace(); 
  17.  System.out.println("err::PendingId::" + v.getPendingId()); 
  18.  } 
  19.  }); 
  20.  ... 
  21.  } 
  22. public class ZlReadConsumer implements Runnable { 
  23.  //已閱消費(fèi)者處理邏輯同已辦消費(fèi)者 

定義導(dǎo)入Elasticsearch數(shù)據(jù)監(jiān)控線程:Monitor

監(jiān)控線程-Monitor為了計(jì)算每分鐘導(dǎo)入Elasticsearch的數(shù)據(jù)總條數(shù),利用監(jiān)控線程,可以調(diào)整線程池的線程數(shù)的大小,以便利用多線程更快速的導(dǎo)入數(shù)據(jù)。

  1. public void monitorToES() { 
  2.  new Thread(() -> { 
  3.  while (true) { 
  4.  StringBuilder sb = new StringBuilder(); 
  5.  sb.append("已辦表數(shù)::").append(Const.TBL.TBL_PEND_COUNT) 
  6.  .append("::已辦總數(shù)::").append(Const.COUNTER.LD_P_TOTAL) 
  7.  .append("::已辦入庫總數(shù)::").append(Const.COUNTER.LD_P); 
  8.  sb.append("~~~~已閱表數(shù)::").append(Const.TBL.TBL_READ_COUNT); 
  9.  sb.append("::已閱總數(shù)::").append(Const.COUNTER.LD_R_TOTAL) 
  10.  .append("::已閱入庫總數(shù)::").append(Const.COUNTER.LD_R); 
  11.  if (ldPrevPendCount == 0 && ldPrevReadCount == 0) { 
  12.  ldPrevPendCount = Const.COUNTER.LD_P.get(); 
  13.  ldPrevReadCount = Const.COUNTER.LD_R.get(); 
  14.  start = System.currentTimeMillis(); 
  15.  } else { 
  16.  long end = System.currentTimeMillis(); 
  17.  if ((end - start) / 1000 >= 60) { 
  18.  start = end; 
  19.  sb.append("\n#########################################\n"); 
  20.  sb.append("已辦每分鐘TPS::" + (Const.COUNTER.LD_P.get() - ldPrevPendCount) + "條"); 
  21.  sb.append("::已閱每分鐘TPS::" + (Const.COUNTER.LD_R.get() - ldPrevReadCount) + "條"); 
  22.  ldPrevPendCount = Const.COUNTER.LD_P.get(); 
  23.  ldPrevReadCount = Const.COUNTER.LD_R.get(); 
  24.  } 
  25.  } 
  26.  System.out.println(sb.toString()); 
  27.  try { 
  28.  Thread.sleep(3000); 
  29.  } catch (InterruptedException e) { 
  30.  e.printStackTrace(); 
  31.  } 
  32.  } 
  33.  }).start(); 

初始化Elasticsearch:EsClient

  1. String cName = meta.get("cName");//es集群名字 
  2. String esNodes = meta.get("esNodes");//es集群ip節(jié)點(diǎn) 
  3. Settings esSetting = Settings.builder() 
  4.  .put("cluster.name", cName) 
  5.  .put("client.transport.sniff"true)//增加嗅探機(jī)制,找到ES集群 
  6.  .put("thread_pool.search.size"5)//增加線程池個(gè)數(shù),暫時(shí)設(shè)為5 
  7.  .build(); 
  8. String[] nodes = esNodes.split(","); 
  9. client = new PreBuiltTransportClient(esSetting); 
  10. for (String node : nodes) { 
  11.  if (node.length() > 0) { 
  12.  String[] hostPort = node.split(":"); 
  13.  client.addTransportAddress(new TransportAddress(InetAddress.getByName(hostPort[0]), Integer.parseInt(hostPort[1]))); 
  14.  } 

初始化數(shù)據(jù)庫連接

  1. conn = DriverManager.getConnection(url, user, password); 

啟動參數(shù)

  1. nohup java -jar mte.jar ES-Cluster2019 node1:9300,node2:9300,node3:9300 root 123456! jdbc:mysql://ip:3306/mte 130 130 >> ./mte.log 2>&1 & 

參數(shù)說明

ES-Cluster2019 為Elasticsearch集群名字

node1:9300,node2:9300,node3:9300為es的節(jié)點(diǎn)IP

130 130為已辦已閱分表的數(shù)據(jù)

程序入口:MteMain

干貨分享:利用java多線程技術(shù)導(dǎo)入數(shù)據(jù)到Elasticsearch
 
 
  1. // 監(jiān)控線程 
  2. Monitor monitorService = new Monitor(); 
  3. monitorService.monitorToES(); 
  4. // 已辦生產(chǎn)者線程 
  5. Thread pendProducerThread = new Thread(new ZlPendProducer(conn, "ZlPendProducer")); 
  6. pendProducerThread.start(); 
  7. // 已閱生產(chǎn)者線程 
  8. Thread readProducerThread = new Thread(new ZlReadProducer(conn, "ZlReadProducer")); 
  9. readProducerThread.start(); 
責(zé)任編輯:張燕妮 來源: 頭條科技
相關(guān)推薦

2021-04-28 08:00:16

多線程高并發(fā)操作

2019-09-16 12:55:27

HBaseKafka數(shù)據(jù)

2018-05-30 16:55:47

阿里Java多線程

2012-01-12 10:09:30

Java

2024-07-03 08:02:19

MySQL數(shù)據(jù)搜索

2016-12-21 14:14:51

SQOOP數(shù)據(jù)庫HDFS

2009-03-12 10:52:43

Java線程多線程

2009-04-27 13:15:04

多線程方法run()

2009-10-23 09:26:09

VB.NET多線程

2016-07-27 16:45:12

大數(shù)據(jù)IT

2016-11-11 11:11:25

2010-07-15 15:21:07

Perl線程

2010-07-16 13:21:26

Perl哈希表

2021-12-26 18:22:30

Java線程多線程

2009-06-29 17:49:47

Java多線程

2009-07-21 17:09:47

ASP.NET多線程

2023-10-18 15:19:56

2010-05-25 14:54:18

2019-08-15 11:11:38

Java數(shù)據(jù)庫設(shè)計(jì)

2024-10-24 17:13:55

WinformUI多線程
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號