前段時間我負責在系統中添加了HBase用於管理海量數據,功能實現後自然到了性能測試階段,性能、可靠性、穩定性,性性都不能少。
因此就有了個內部的小需求:“給測試部提供一個工具,可以向HBase寫入數據,以達到測試所需的數據量”。
由於我們的系統是由Java編寫,Client的代碼也用的Hadoop的Java API,因此小工具的界面編寫理所應當的選擇了Java Swing。
之前沒有用過Swing,本次完全是在網上一邊翻博一邊編碼。
首先是成品圖:
OK,我知道很丑,咱不就圖個實用麼……
這裡可以輸入Hadoop集群的IP,可以選擇需要操作的表,可以清空表中的記錄,還有寫入耗時統計。
下面是代碼,代碼分為兩個文件,一個是MainView負責界面展示;另一個是Worker線程,負責干活和刷新進度條與顯示數據。
MainView代碼:
- package com.codeevoship.gui;
- import java.awt.Container;
- import java.awt.Font;
- import java.awt.GridBagConstraints;
- import java.awt.GridBagLayout;
- import java.awt.Insets;
- import java.awt.event.ActionEvent;
- import java.awt.event.ActionListener;
- import javax.swing.JButton;
- import javax.swing.JFrame;
- import javax.swing.JLabel;
- import javax.swing.JOptionPane;
- import javax.swing.JProgressBar;
- import javax.swing.JRadioButton;
- import javax.swing.JTextField;
- @SuppressWarnings("serial")
- publicclass MainWindow extends JFrame {
- private JLabel welcomeLbl;
- private JLabel ipLbl;
- private JTextField ipTxt;
- private JLabel numLbl;
- private JTextField numTxt;
- private JProgressBar probar;
- private JTextField txtResultShow;
- private JRadioButton archiveSel;
- private JRadioButton contentSel;
- private JRadioButton systemLogSel;
- private JRadioButton clearDataSel;
- private JButton startBtn;
- private JButton exitBtn;
- private Thread thdWorker;
- public MainWindow() {
- // 獲取本JFrame的ContentPane
- Container contentPane = getContentPane();
- GridBagLayout gridbag = new GridBagLayout();
- contentPane.setLayout(gridbag);
- GridBagConstraints c = new GridBagConstraints();
- // Title
- welcomeLbl = new JLabel("CodeEvoShip HBase 數據填充工具");
- welcomeLbl.setFont(new Font("Serif",Font.BOLD,20));
- c = getGridBagConstraints(0, 0, 0, 1, 0, 0,
- GridBagConstraints.CENTER, GridBagConstraints.NONE,
- new Insets(10, 0, 0, 0), 10, 0);
- gridbag.setConstraints(welcomeLbl, c);
- contentPane.add(welcomeLbl);
- // IP
- ipLbl = new JLabel("Cluster IP:");
- c = getGridBagConstraints(0, 1, 1, 1, 0, 0,
- GridBagConstraints.WEST, GridBagConstraints.NONE,
- new Insets(10, 0, 10, 0), 10, 0);
- gridbag.setConstraints(ipLbl, c);
- contentPane.add(ipLbl);
- ipTxt = new JTextField("127.0.0.1;127.0.0.2;127.0.0.3");
- c = getGridBagConstraints(1, 1, 0, 1, 0, 0,
- GridBagConstraints.WEST, GridBagConstraints.NONE,
- new Insets(10, 0, 10, 0), 85, 0);
- gridbag.setConstraints(ipTxt, c);
- contentPane.add(ipTxt);
- // 數量填寫
- numLbl = new JLabel("Write Number:");
- c = getGridBagConstraints(0, 2, 1, 1, 0, 0,
- GridBagConstraints.WEST, GridBagConstraints.NONE,
- new Insets(10, 0, 10, 0), 10, 0);
- gridbag.setConstraints(numLbl, c);
- contentPane.add(numLbl);
- numTxt = new JTextField("10000");
- c = getGridBagConstraints(1, 2, 0, 1, 0, 0,
- GridBagConstraints.WEST, GridBagConstraints.NONE,
- new Insets(10, 0, 10, 0), 230, 0);
- gridbag.setConstraints(numTxt, c);
- contentPane.add(numTxt);
- // 多選框
- archiveSel = new JRadioButton("Archive Table");
- c = getGridBagConstraints(0, 3, 1, 1, 0, 0,
- GridBagConstraints.WEST, GridBagConstraints.NONE,
- new Insets(0, 0, 0, 0), 0, 0);
- gridbag.setConstraints(archiveSel, c);
- contentPane.add(archiveSel);
- contentSel = new JRadioButton("Content Table");
- c = getGridBagConstraints(1, 3, 1, 1, 0, 0,
- GridBagConstraints.WEST, GridBagConstraints.NONE,
- new Insets(0, 0, 0, 0), 0, 0);
- gridbag.setConstraints(contentSel, c);
- contentPane.add(contentSel);
- systemLogSel = new JRadioButton("SystemLog Table");
- c = getGridBagConstraints(2, 3, 1, 1, 0, 0,
- GridBagConstraints.WEST, GridBagConstraints.NONE,
- new Insets(0, 0, 5, 0), 0, 0);
- gridbag.setConstraints(systemLogSel, c);
- contentPane.add(systemLogSel);
- clearDataSel = new JRadioButton("Clear old data of selected Tables!!");
- c = getGridBagConstraints(0, 4, 0, 1, 0, 0,
- GridBagConstraints.WEST, GridBagConstraints.NONE,
- new Insets(0, 0, 15, 0), 0, 0);
- gridbag.setConstraints(clearDataSel, c);
- contentPane.add(clearDataSel);
- // 進度條
- probar = new JProgressBar(0, 100);
- probar.setStringPainted(true);
- c = getGridBagConstraints(0, 6, 0, 1, 0, 0,
- GridBagConstraints.WEST, GridBagConstraints.NONE,
- new Insets(5, 5, 5, 5), 250, 0);
- gridbag.setConstraints(probar, c);
- contentPane.add(probar);
- txtResultShow = new JTextField("Not Begin");
- txtResultShow.setEditable(false);
- txtResultShow.setHorizontalAlignment(JTextField.CENTER);
- c = getGridBagConstraints(0, 7, 0, 1, 0, 0,
- GridBagConstraints.WEST, GridBagConstraints.NONE,
- new Insets(0, 0, 5, 0), 325, 0);
- gridbag.setConstraints(txtResultShow, c);
- contentPane.add(txtResultShow);
- //執行與退出
- startBtn = new JButton("Start");
- c = getGridBagConstraints(0, 5, 0, 1, 0, 0,
- GridBagConstraints.WEST, GridBagConstraints.NONE,
- new Insets(0, 0, 5, 0), 50, 0);
- gridbag.setConstraints(startBtn, c);
- startBtn.addActionListener(new ActionListener() {
- publicvoid actionPerformed(ActionEvent arg0) {
- if (!paraPassCheck()) {
- return;
- }
- if (null != thdWorker && thdWorker.isAlive()) {
- JOptionPane.showMessageDialog(null,"There already is a task running now, please wait");
- return;
- }
- DoWork doWork = new DoWork(ipTxt.getText(),Long.parseLong(numTxt.getText()),
- contentSel.isSelected(),archiveSel.isSelected(),
- systemLogSel.isSelected(),clearDataSel.isSelected(),
- probar, txtResultShow);
- thdWorker = new Thread(doWork);
- thdWorker.start();
- }
- });
- contentPane.add(startBtn);
- exitBtn = new JButton("Exit");
- c = getGridBagConstraints(1, 5, 0, 1, 0, 0,
- GridBagConstraints.EAST, GridBagConstraints.NONE,
- new Insets(0, 0, 5, 0), 50, 0);
- gridbag.setConstraints(exitBtn, c);
- exitBtn.addActionListener(new ActionListener() {
- publicvoid actionPerformed(ActionEvent arg0) {
- System.exit(0);
- }
- });
- contentPane.add(exitBtn);
- }
- privateboolean paraPassCheck() {
- try {
- Long.parseLong(numTxt.getText());
- returntrue;
- } catch (Exception e) {
- JOptionPane.showMessageDialog(null,"Invalid number ");
- returnfalse;
- }
- }
- privatestatic GridBagConstraints getGridBagConstraints(int gridx,
- int gridy, int gridwidth, int gridheight, double weightx,
- double weighty, int anchor, int fill, Insets insets, int ipadx,
- int ipady) {
- returnnew GridBagConstraints(gridx, gridy, gridwidth, gridheight,
- weightx, weighty, anchor, fill, insets, ipadx, ipady);
- }
- publicstaticvoid main(String args[]) {
- MainWindow mainWindow = new MainWindow();
- mainWindow.setTitle("CodeEvoShip");
- mainWindow.pack();
- mainWindow.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- mainWindow.setVisible(true);
- }
- }
Worker代碼:
- package com.codeevoship.gui;
- import javax.swing.JProgressBar;
- import javax.swing.JTextField;
- publicclass DoWork implements Runnable {
- JProgressBar probar;
- JTextField curTxtShow;
- long totalNum = 0;
- long curNum = 0;
- long startTime = 0;
- long curTime = 0;
- public DoWork(String ip, long num, boolean isWriteCon, boolean isWriteArc, boolean isWriteSys, boolean isClearData, JProgressBar probar, JTextField curTxtShow) {
- System.out.println("IP:" + ip + ", Num:" + num);
- System.out.println("Write Content[" + isWriteCon + "], Write Archive[" + isWriteArc + "], Write SystemLog[" + isWriteSys + "].");
- startTime = 0;
- curNum = 0;
- totalNum = num;
- this.probar = probar;
- this.curTxtShow = curTxtShow;
- }
- @Override
- publicvoid run() {
- startTime = System.currentTimeMillis();
- // 開始工作
- System.out.println("Do Work Start!");
- StringBuilder sb = new StringBuilder();
- while (true) {
- if (curNum >= totalNum) {
- break;
- }
- try {
- // TODO: Real work here
- Thread.sleep(10);
- // 更新界面顯示
- curNum += 1;
- probar.setValue((int) (curNum*100/totalNum));
- curTime = System.currentTimeMillis();
- sb.setLength(0);
- sb.append(curNum);
- sb.append('/');
- sb.append(totalNum);
- sb.append(" cost time: ");
- sb.append((curTime - startTime)/1000);
- sb.append('s');
- curTxtShow.setText(sb.toString());
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- System.out.println("Do Work End!");
- }
- }
大家如需要可隨意使用,改成自己的小工具,只要把Worker中的// TODO: 改為實際運行的程序即可。