由於最近在研究kettle,用到參數轉換的功能,領導讓用java代碼寫處理轉換的功能,而後用kettle調用,發現若是java代碼不作優化,4萬多的數據,要6分鐘左右才能跑完,如下是我代碼的優化處理,處理完後,4萬數據,11秒左右就抽取和處理完畢。由於我這如今沒什麼複雜的處理業務邏輯,你們能夠參考看看java
1.首先,建立轉換的實體類mysql
package entity; public class Comparison { private String id; private String codetype; private String codename; private String newcode; private String newcodename; private String oldcode; private String oldcodename; private String filename; private String remark; public String getId() { return id; } public void setId(String id) { this.id = id; } public String getCodetype() { return codetype; } public void setCodetype(String codetype) { this.codetype = codetype; } public String getCodename() { return codename; } public void setCodename(String codename) { this.codename = codename; } public String getNewcode() { return newcode; } public void setNewcode(String newcode) { this.newcode = newcode; } public String getNewcodename() { return newcodename; } public void setNewcodename(String newcodename) { this.newcodename = newcodename; } public String getOldcode() { return oldcode; } public void setOldcode(String oldcode) { this.oldcode = oldcode; } public String getOldcodename() { return oldcodename; } public void setOldcodename(String oldcodename) { this.oldcodename = oldcodename; } public String getFilename() { return filename; } public void setFilename(String filename) { this.filename = filename; } public String getRemark() { return remark; } public void setRemark(String remark) { this.remark = remark; } }
2,建立鏈接池和數據庫的鏈接sql
package util; import java.sql.Connection; import java.sql.DriverManager; import java.sql.SQLException; import java.util.Vector; public class JdbcUtil { private static final Vector<Connection> pool = new Vector<Connection>(); private static final int MAX_SIZE = 50; private static final int MIN_SIZE = 10; private static Connection createConnection(){ Connection conn = null; try { Class.forName("com.mysql.jdbc.Driver"); conn = DriverManager.getConnection( "jdbc:mysql://192.168.4.143:3306/loan2","root","123456"); } catch (ClassNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } return conn; } static { for (int i = 0; i < MIN_SIZE; i++) { pool.add(createConnection()); } } public static synchronized Connection getConnection() { Connection conn = null; //conn = createConnection(); if (pool.isEmpty()) { conn = createConnection(); } else { int last_idx = pool.size() - 1; conn = (Connection) pool.get(last_idx); pool.remove(conn); } return conn; } public static synchronized void close(Connection conn) { if (pool.size() < MAX_SIZE) { pool.add(conn); } else { try { conn.close(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } }
3,使用緩存,把數據庫讀到的數據放到緩存裏數據庫
package transform; import java.sql.Connection; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; import java.util.HashMap; import java.util.Map; import java.util.concurrent.locks.Lock; import util.JdbcUtil; public class trans { public static void main(String[] args) { trans trans = new trans(); String code = trans.transCode("SEXCODE", ""); } static int num; static Map<String, Map<String, String>> paramTypeKeyValueMap = new HashMap<String, Map<String, String>>(); static Lock lock = new java.util.concurrent.locks.ReentrantLock();//同步,須要手動釋放資源 public String transCode(String codetype, String oldcode) { num++; Thread.currentThread().setName("zhengxin" + num); if (oldcode == null) { return null; } Map<String, String> keyValue = paramTypeKeyValueMap.get(codetype); if (keyValue == null) { try { lock.lock();//獲取鎖 keyValue = paramTypeKeyValueMap.get(codetype); if (keyValue == null) { keyValue = load2Cache(codetype); } } finally { lock.unlock();// 釋放鎖 } } if (keyValue.get(oldcode) != null) { return keyValue.get(oldcode); } else { return oldcode; } } private Map<String, String> load2Cache(String codetype) { Map<String, String> keyValue = new HashMap<String, String>(); Connection conn = JdbcUtil.getConnection(); Statement stmt = null; try { stmt = conn.createStatement(); String sql = "select newcode,oldcode from comparison where codetype='" + codetype + "'"; ResultSet rs = stmt.executeQuery(sql); paramTypeKeyValueMap.put(codetype, keyValue); while (rs.next()) { String newcode = rs.getString(1); String oldc = rs.getString(2); if (oldc == null) { continue; } String[] codes = oldc.split(","); for (String s : codes) { if (s == null) { continue; } keyValue.put(s, newcode); } } } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } finally { if (stmt != null) { try { stmt.close(); } catch (Throwable e) { // TODO Auto-generated catch block e.printStackTrace(); } } if (conn != null) { JdbcUtil.close(conn); } } return keyValue; } }