1. mysql建库,建表
2. 准备数据文件cpu.txt
1447836374319,169,0,0
1447836375346,498,0,0
1447836376346,250,0,0
1447836377347,0,0,0
1447836378347,497,0,0
1447836379347,0,0,0
1447836380347,498,0,0
1447836381348,0,0,0
1447836382348,498,0,0
1447836383348,0,0,0
1447836384348,248,0,0
1447836385348,0,0,0
3. 收集数据记录到mysql
使用到的类说明:
3.1 File 获取文件句柄,File实例代表一个本地文件。
3.2 FileInputStream is meant for reading streams of raw bytes such as image data.
3.3 An InputStreamReader is a bridge from byte streams to character streams: It reads bytes and decodes them into characters using a specified charset.
3.4 BufferedReader: Reads text from a character-input stream, buffering characters so as to provide for the efficient reading of characters, arrays, and lines.
package collect_part; import java.io.BufferedReader; import java.io.File; import java.io.FileInputStream; import java.io.InputStreamReader; public class CollectData { public static void main(String[] args) { String path = "d:\logs\cpu.txt"; CollectData data = new CollectData(); data.readFile(path); } public void readFile(String filePath) { InfoBean infoBean = new InfoBean(); try { String encodeing = "GBK"; File file = new File(filePath); if (file.isFile() && file.exists()) { InputStreamReader isr = new InputStreamReader(new FileInputStream(file), encodeing); BufferedReader bufferedReader = new BufferedReader(isr); String lineTxt = null; OperationDB opdb = new OperationDB(); while ((lineTxt = bufferedReader.readLine()) != null) { String[] s = lineTxt.split(","); //System.out.println(s[0].toString() + s[1].toString() + s[2].toString() + s[3].toString() ); infoBean.setTimeStamp(s[0].toString()); infoBean.setElapsed(s[1].toString()); infoBean.setGrpThreads(s[2].toString()); infoBean.setAllThreads(s[3].toString()); opdb.addRcorder(infoBean); System.out.println(lineTxt); } isr.close(); } else { System.out.println("not found specific file"); } } catch (Exception e) { System.out.println("read file content error"); e.printStackTrace(); } } }
创建连接
package collect_part; import java.sql.Connection; import java.sql.DriverManager; import java.sql.SQLException; import java.util.logging.Level; import java.util.logging.Logger; public class MysqlConnection { public static Connection getConnection() { Connection conn = null; String url = "jdbc:mysql://10.1.1.148:3306/testresults"; String user = "user"; String password = "passwd"; try { conn = DriverManager.getConnection(url, user, password); } catch (SQLException ex) { Logger.getLogger(MysqlConnection.class.getName()).log(Level.SEVERE,null, ex); } return conn; } }
操作数据库
package collect_part; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.SQLException; import java.util.logging.Level; import java.util.logging.Logger; public class OperationDB { private Connection conn = null; public void addRcorder(InfoBean infoBean) throws SQLException { if (conn == null) { conn = MysqlConnection.getConnection(); } String sql = "INSERT INTO cpuInfo (`timeStamp`, `elapsed`, `grpThreads`, `allThreads`) VALUES (?, ?, ?, ?)"; PreparedStatement pstmt = conn.prepareStatement(sql); pstmt.setLong(1, infoBean.getTimeStamp()); pstmt.setLong(2, infoBean.getElapsed()); pstmt.setLong(3, infoBean.getGrpThreads()); pstmt.setLong(4, infoBean.getAllThreads()); pstmt.executeUpdate(); } }
记录信息
package collect_part; public class InfoBean { long timeStamp; long elapsed; long grpThreads; long allThreads; public long getTimeStamp() { return timeStamp; } public void setTimeStamp(String timeStamp) { this.timeStamp = Long.parseLong(timeStamp); } public long getElapsed() { return elapsed; } public void setElapsed(String elapsed) { this.elapsed = Long.parseLong(elapsed); } public long getGrpThreads() { return grpThreads; } public void setGrpThreads(String grpThreads) { this.grpThreads = Long.parseLong(grpThreads); } public long getAllThreads() { return allThreads; } public void setAllThreads(String allThreads) { this.allThreads = Long.parseLong(allThreads); } }