zoukankan      html  css  js  c++  java
  • [原创]HBase客户端开发举例(第三部分)

    4.创建QueryDataClient.java ,其代码如下

    public class QueryDataClient{

        //表名为jvmMonitor
       public final static String TABLE_NAME ="jvmMonitor";  
      //列族名 "target","memory", "os"
       public final static String[] COLUM_FAMILIES =new String[]{"target", "memory", "os"};
       private String partRowKey; //行健的前半部分
       private String serverIP;  //Web服务器IP
       private StringbeginDataTime; // 查询的起始时间
       private String endDataTime;//查询的终点时间
       private HBaseAdmin admin;
       private HTable htable;

       public static void main(String[] args) throwsException {
    //
           //为了方便测试,将所监控Web服务器的IP,查询的起始时间,终点时间写死在main函数中

           args = newString[3];
           args[0] ="192.168.1.107";
           args[1] ="20121125231000";
           args[2] ="20121125232000";

          QueryDataClient queryDataClient = newQueryDataClient();
          queryDataClient.setServerIP(args[0]);
          queryDataClient.setBeginDataTime(args[1]);
          queryDataClient.setEndDataTime(args[2]);
          queryDataClient.doQuey();
       }

       public void doQuey() {
          Configuration conf = HBaseConfiguration.create();
           try{
              //连接HBase集群
              admin = newHBaseAdmin(conf);
              this.htable = newHTable(conf, TABLE_NAME);
              Scan scan = newScan();
              String startRow =this.makeRowKey(this.beginDataTime);
              String endRow =this.makeRowKey(this.endDataTime);
              //设置起始行健
              scan.setStartRow(Bytes.toBytes(startRow));
              //设置终点行健
              scan.setStopRow(Bytes.toBytes(endRow));
              ResultScanner resultScanner =htable.getScanner(scan);
              Iterator iterator =resultScanner.iterator();
              //打印结果
              byte[] bytesTarget = Bytes.toBytes(COLUM_FAMILIES[0]);
               byte[]bytesMemory = Bytes.toBytes(COLUM_FAMILIES[1]);
               byte[]bytesOS = Bytes.toBytes(COLUM_FAMILIES[2]);
              while (iterator.hasNext()){
                 Result result =iterator.next();
                 String rowKey =Bytes.toString(result.getRow());
                 String serverIP =Bytes.toString(result.getValue(bytesTarget,Bytes.toBytes("serverIP")));
                 String serverPort =Bytes.toString(result.getValue(bytesTarget,Bytes.toBytes("serverPort")));
                 String heapUsedKB =Bytes.toString(result.getValue(bytesMemory,Bytes.toBytes("heapUsedKB")));
                 String heapMaxKB =Bytes.toString(result.getValue(bytesMemory,Bytes.toBytes("heapMaxKB")));
                 String heapCommitKB =Bytes.toString(result.getValue(bytesMemory,Bytes.toBytes("heapCommitKB")));
                 String heapUtil =Bytes.toString(result.getValue(bytesMemory,Bytes.toBytes("heapUtil")));
                 String nonHeapUsedKB =Bytes.toString(result.getValue(bytesMemory,Bytes.toBytes("nonHeapUsedKB")));
                 String nonHeapMaxKB =Bytes.toString(result.getValue(bytesMemory,Bytes.toBytes("nonHeapMaxKB")));
                 String nonHeapCommitKB =Bytes.toString(result.getValue(bytesMemory,Bytes.toBytes("nonHeapCommitKB")));
                 String nonHeapUtil =Bytes.toString(result.getValue(bytesMemory,Bytes.toBytes("nonHeapUtil")));

                 String virtMemCommitKB =Bytes.toString(result.getValue(bytesOS,Bytes.toBytes("virtMemCommitKB")));
                 String freePhyMemKB =Bytes.toString(result.getValue(bytesOS,Bytes.toBytes("freePhyMemKB")));
                 String freeSwapKB =Bytes.toString(result.getValue(bytesOS,Bytes.toBytes("freeSwapKB")));
                 String totalSwapKB =Bytes.toString(result.getValue(bytesOS,Bytes.toBytes("totalSwapKB")));
                 String phyMemUtil =Bytes.toString(result.getValue(bytesOS,Bytes.toBytes("phyMemUtil")));
                 String swapMemUtil =Bytes.toString(result.getValue(bytesOS,Bytes.toBytes("swapMemUtil")));
                 String cpuUtil =Bytes.toString(result.getValue(bytesOS,Bytes.toBytes("cpuUtil")));
                 System.out.println("DataTime=" +rowKey.substring(12) + ", serverIP=" + serverIP + ", serverPort=" +serverPort + ", heapUsedKB=" + heapUsedKB
                        + ",heapMaxKB=" + heapMaxKB +", heapCommitKB=" + heapCommitKB + ", heapUtil=" + heapUtil + ",nonHeapUsedKB=" + nonHeapUsedKB
                        + ",nonHeapMaxKB=" +nonHeapMaxKB + ", nonHeapCommitKB=" + nonHeapCommitKB + ",nonHeapUtil=" + nonHeapUtil + ", virtMemCommitKB=" +virtMemCommitKB
                        + ",freePhyMemKB=" +freePhyMemKB + ", freeSwapKB=" + freeSwapKB + ", totalSwapKB=" +totalSwapKB + ", phyMemUtil=" + phyMemUtil
                        + ", swapMemUtil=" +swapMemUtil + ", cpuUtil=" + cpuUtil);
                 System.out.println();
              }
             resultScanner.close();
           } catch (Exception ex) {
             Logger.getLogger(QueryDataClient.class.getName()).log(Level.SEVERE,null, ex);
           } finally{
              //关闭连接
              if (this.htable != null){
                 try {
                    this.htable.close();
                 } catch (Exception ex) {
                 }
              }
              if (this.admin != null){
                 try {
                    this.admin.close();
                 } catch (Exception ex) {
                 }
              }
          }
       }

    // 生成行健,行健用Web服务器IP和数据采集的时间来表示,共24位,前12位是IP,后12位是时间,不够的在前面用0填充
      // 例如:IP为10.1.2.122,时间为2012-11-2523:01:08,则行健为01000100212220121125230108
       private String makeRowKey(String dataTime){
           if(this.partRowKey == null) {
             this.makePartRowKey();
          }
           returnthis.partRowKey + dataTime;
       }

    //根据Web服务器IP生成行健的前半部分
       private void makePartRowKey() {
           String[]splits = this.serverIP.split("[.]");
           for (int i= 0; i < splits.length; i++) {
              String s =splits[i];
              int len =s.length();
              while (len <3) {
                 s = "0" + s;
                 len++;
              }
              splits[i] = s;
          }
          this.partRowKey = splits[0] + splits[1] + splits[2] +splits[3];
       }

      //以下为Getter Setter

       public String getServerIP() {
           returnserverIP;
       }

       public void setServerIP(String serverIP){
          this.serverIP = serverIP;
       }

       public String getBeginDataTime() {
           returnbeginDataTime;
       }

       public void setBeginDataTime(StringbeginDataTime) {
          this.beginDataTime = beginDataTime;
       }

       public String getEndDataTime() {
           returnendDataTime;
       }

       public void setEndDataTime(String endDataTime){
          this.endDataTime = endDataTime;
       }
    }

    运行程序,控制台输出结果:

    12/11/25 23:30:50 INFOzookeeper.ZooKeeper: Client environment:os.name=Windows7
    12/11/25 23:30:50 INFOzookeeper.ZooKeeper: Client environment:os.arch=x86
    12/11/25 23:30:50 INFOzookeeper.ZooKeeper: Client environment:os.version=6.1
    12/11/25 23:30:50 INFOzookeeper.ZooKeeper: Clientenvironment:user.name=hui.li
    12/11/25 23:30:50 INFOzookeeper.ZooKeeper: Clientenvironment:user.home=C:Usershui.li
    12/11/25 23:30:50 INFOzookeeper.ZooKeeper: Clientenvironment:user.dir=D:CentOS64ShareExample
    12/11/25 23:30:50 INFOzookeeper.ZooKeeper: Initiating client connection,connectString=192.168.1.201:2181 sessionTimeout=180000watcher=hconnection
    12/11/25 23:30:50 INFOzookeeper.ClientCnxn: Opening socket connection to server/192.168.1.201:2181
    12/11/25 23:30:50 INFOzookeeper.ClientCnxn: Socket connection established toserver1/192.168.1.201:2181, initiating session
    12/11/25 23:30:50 INFOzookeeper.ClientCnxn: Session establishment complete on serverserver1/192.168.1.201:2181, sessionid = 0x3b35ddd5d40021,negotiated timeout = 180000
    12/11/25 23:30:50 INFOzookeeper.ZooKeeper: Initiating client connection,connectString=192.168.1.201:2181 sessionTimeout=180000watcher=hconnection
    12/11/25 23:30:50 INFOzookeeper.ClientCnxn: Opening socket connection to server/192.168.1.201:2181
    12/11/25 23:30:50 INFOzookeeper.ClientCnxn: Socket connection established toserver1/192.168.1.201:2181, initiating session
    12/11/2523:30:50 INFO zookeeper.ClientCnxn: Session establishment completeon server server1/192.168.1.201:2181, sessionid = 0x3b35ddd5d40022,negotiated timeout = 180000
     
    DataTime=20121125231002,serverIP=192.168.1.107, serverPort=8686,heapUsedKB=91746,heapMaxKB=506816, heapCommitKB=164192,heapUtil=55, nonHeapUsedKB=84047,nonHeapMaxKB=229376,nonHeapCommitKB=84064, nonHeapUtil=99,virtMemCommitKB=293580,freePhyMemKB=1063424, freeSwapKB=4194303,totalSwapKB=4194303, phyMemUtil=75, swapMemUtil=0,cpuUtil=0

    DataTime=20121125231015,serverIP=192.168.1.107, serverPort=8686,heapUsedKB=92581,heapMaxKB=506816, heapCommitKB=164192,heapUtil=56, nonHeapUsedKB=84047,nonHeapMaxKB=229376,nonHeapCommitKB=84064, nonHeapUtil=99,virtMemCommitKB=293580,freePhyMemKB=1061728, freeSwapKB=4194303,totalSwapKB=4194303, phyMemUtil=75, swapMemUtil=0,cpuUtil=0

    DataTime=20121125231028,serverIP=192.168.1.107, serverPort=8686,heapUsedKB=92994,heapMaxKB=506816, heapCommitKB=164192,heapUtil=56, nonHeapUsedKB=84047,nonHeapMaxKB=229376,nonHeapCommitKB=84064, nonHeapUtil=99,virtMemCommitKB=293580,freePhyMemKB=1062396, freeSwapKB=4194303,totalSwapKB=4194303, phyMemUtil=75, swapMemUtil=0,cpuUtil=0


    。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。
    12/11/25 23:30:51 INFOzookeeper.ClientCnxn: EventThread shut down
  • 相关阅读:
    venv(virtual environment)的使用
    Pytest 失败重运行机制
    Python 中方法和函数的区别
    手机移动端WEB资源整合
    Sass学习日志
    1、vue基础使用复习
    vscode配置笔记
    vue兼容IE浏览器、对低版本IE下的友好提示
    关闭eslint校验
    vue实现吸顶的效果
  • 原文地址:https://www.cnblogs.com/leeeee/p/7276658.html
Copyright © 2011-2022 走看看