[原创]HBase客户端开发举例(第二部…

来源:互联网 发布:纯文字游戏源码 编辑:程序博客网 时间:2024/04/26 03:06
3. 创建MonitorClient.java, 代码如下所示

public class MonitorClient {
   // 表名为jvmMonitor
    publicfinal static String TABLE_NAME = "jvmMonitor";  
  //列族名 "target", "memory","os"
    publicfinal static String[] COLUM_FAMILIES = new String[]{"target","memory", "os"}; 
   private MBeanServerConnectionconnect;
    privateHTable htable;
    privateString serverIP;  //Web服务器IP
    privateint serverPort;   //Web服务器端口
    privateString userName;  //Web服务器用户名
    privateString password;   //Web服务器密码
    privateHBaseAdmin admin;
    privateString partRowKey; //行健的前半部分

    publicstatic void main(String[] args) throws Exception{
      
      //为了方便测试,将所监控Web服务器的IP,端口,用户名,密码写死在主函数中。
       args = newString[4];
       args[0] ="192.168.1.107";
       args[1] ="8686";
       args[2] ="admin";
       args[3] ="adminadmin";

       MonitorClient monitorClient =new MonitorClient();
      monitorClient.setServerIP(args[0]);
      monitorClient.setServerPort(Integer.valueOf(args[1]));
      monitorClient.setUserName(args[2]);
      monitorClient.setPassword(args[3]);
      monitorClient.doMonitor();
   }

    publicvoid doMonitor()   {

       try {
           //创建表
          this.createTable();
          while (true) {
             //建立到web服务器的JMX协议连接
              if(this.connect == null) {
                 this.connect =getJMXConnector(serverIP, serverPort, userName,password);
             }
            //插入数据
             this.insertData();
             //暂停10秒后继续监控
             TimeUnit.SECONDS.sleep(10);
          }
       } catch (Exception ex){
         Logger.getLogger(MonitorClient.class.getName()).log(Level.SEVERE,null, ex);
       } finally{
          //若发生异常关闭到HBase连接
          if (this.htable != null) {
              try{
                this.htable.close();
              } catch(Exception ex) {
             }
          }
          if (this.admin != null) {
              try{
                this.admin.close();
              } catch(Exception ex) {
             }
          }
       }
   }

    //方法 getJMXConnector 用于建立到Web服务器的连接
    privateMBeanServerConnection getJMXConnector(String serverIP, intserverPort, String userName, String password) throws Exception{
       String jmxURL ="service:jmx:rmi:///jndi/rmi://" + serverIP + ":" + serverPort +"/jmxrmi";
       final JMXServiceURL target =new JMXServiceURL(jmxURL);
       Map env = newHashMap();
      env.put(JMXConnector.CREDENTIALS, new String[]{userName,password});
       JMXConnector jmxConnector =JMXConnectorFactory.connect(target, env);
       System.out.println("---successful to connect to " + this.serverIP + ":" +this.serverPort + " ---");
       returnjmxConnector.getMBeanServerConnection();
   }

   //在HBase中建立表,如果已存在则不再创建
    privatevoid createTable() throws Exception {
       boolean isExist =false;
       Configuration conf =HBaseConfiguration.create();
       admin = newHBaseAdmin(conf);
       HTableDescriptor[] tables =admin.listTables();
       for (HTableDescriptor eachTtd: tables) {
          if(TABLE_NAME.equals(eachTtd.getNameAsString())){
              isExist =true;
             System.out.println(" --- table " + TABLE_NAME + "exist---");
             break;
          }
       }
       if (!isExist){
          HTableDescriptor htd = newHTableDescriptor(TABLE_NAME);
          HColumnDescriptor hcd0 = newHColumnDescriptor(COLUM_FAMILIES[0]);
          HColumnDescriptor hcd1 = newHColumnDescriptor(COLUM_FAMILIES[1]);
          HColumnDescriptor hcd2 = newHColumnDescriptor(COLUM_FAMILIES[2]);

          htd.addFamily(hcd0);
          htd.addFamily(hcd1);
          htd.addFamily(hcd2);
          admin.createTable(htd);
          System.out.println(" ---successful to createtable " + TABLE_NAME + " ---");
       }
       this.htable = newHTable(conf, TABLE_NAME);

   }

   //从Web服务器从获取信息,并插入表中
    privatevoid insertData() throws Exception {
       final MemoryMXBean mxBean =ManagementFactory.newPlatformMXBeanProxy(
              connect,ManagementFactory.MEMORY_MXBEAN_NAME,MemoryMXBean.class);

       // JVM内存信息
       MemoryUsage muh =mxBean.getHeapMemoryUsage();
       long heapUsedKB =muh.getUsed() / 1024;
       long heapMaxKB = muh.getMax()/ 1024;
       long heapCommitKB =muh.getCommitted() / 1024;
       long heapUtil = (heapUsedKB *100L) / heapCommitKB;

       MemoryUsage munh =mxBean.getNonHeapMemoryUsage();
       long nonHeapUsedKB =munh.getUsed() / 1024;
       long nonHeapMaxKB =munh.getMax() / 1024;
       long nonHeapCommitKB =munh.getCommitted() / 1024;
       long nonHeapUtil =(nonHeapUsedKB * 100L) / nonHeapCommitKB;

      //服务器的操作系统信息
       ObjectName objOperatingSystem= newObjectName("java.lang:type=OperatingSystem");
       long virtMemCommitKB = (Long)connect.getAttribute(objOperatingSystem,"CommittedVirtualMemorySize") / 1024;
       long freePhyMemKB = (Long)connect.getAttribute(objOperatingSystem, "FreePhysicalMemorySize")/ 1024;
       long freeSwapKB = (Long)connect.getAttribute(objOperatingSystem, "FreeSwapSpaceSize") /1024;
       long totalPhyMemKB = (Long)connect.getAttribute(objOperatingSystem, "TotalPhysicalMemorySize")/ 1024;
       long totalSwapKB = (Long)connect.getAttribute(objOperatingSystem, "TotalSwapSpaceSize") /1024;
       long phyMemUtil = 100 - 100 *freePhyMemKB / totalPhyMemKB;
       long swapMemUtil = 100 - 100* freeSwapKB / totalSwapKB;
       long nanoBefore =System.nanoTime();
       long cpuBefore = (Long)connect.getAttribute(objOperatingSystem,"ProcessCpuTime");
      TimeUnit.SECONDS.sleep(3);
       long cpuAfter = (Long)connect.getAttribute(objOperatingSystem,"ProcessCpuTime");
       long nanoAfter =System.nanoTime();
       longcpuUtil;
       if (nanoAfter> nanoBefore) {
          cpuUtil = ((cpuAfter - cpuBefore) * 100L) /(nanoAfter - nanoBefore);
       } else {
          cpuUtil = 0;
       }

       String rowKey =this.makeRowKey();
       byte[] row =Bytes.toBytes(rowKey);
       byte[] bytesTarget =Bytes.toBytes(COLUM_FAMILIES[0]);
       byte[] bytesMemory =Bytes.toBytes(COLUM_FAMILIES[1]);
       byte[] bytesOS =Bytes.toBytes(COLUM_FAMILIES[2]);

       Put put = newPut(row);
       
     //插入Web服务器IP、端口信息
       put.add(bytesTarget,Bytes.toBytes("serverIP"),Bytes.toBytes(String.valueOf(this.serverIP)));
       put.add(bytesTarget,Bytes.toBytes("serverPort"),Bytes.toBytes(String.valueOf(this.serverPort)));
      
      //插入Web服务器JVM内存信息
       put.add(bytesMemory,Bytes.toBytes("heapUsedKB"),Bytes.toBytes(String.valueOf(heapUsedKB)));
       put.add(bytesMemory,Bytes.toBytes("heapMaxKB"),Bytes.toBytes(String.valueOf(heapMaxKB)));
       put.add(bytesMemory,Bytes.toBytes("heapCommitKB"),Bytes.toBytes(String.valueOf(heapCommitKB)));
       put.add(bytesMemory,Bytes.toBytes("heapUtil"),Bytes.toBytes(String.valueOf(heapUtil)));
       put.add(bytesMemory,Bytes.toBytes("nonHeapUsedKB"),Bytes.toBytes(String.valueOf(nonHeapUsedKB)));
       put.add(bytesMemory,Bytes.toBytes("nonHeapMaxKB"),Bytes.toBytes(String.valueOf(nonHeapMaxKB)));
       put.add(bytesMemory,Bytes.toBytes("nonHeapCommitKB"),Bytes.toBytes(String.valueOf(nonHeapCommitKB)));
       put.add(bytesMemory,Bytes.toBytes("nonHeapUtil"),Bytes.toBytes(String.valueOf(nonHeapUtil)));

      //插入Web服务器操作系统信息
       put.add(bytesOS,Bytes.toBytes("virtMemCommitKB"),Bytes.toBytes(String.valueOf(virtMemCommitKB)));
       put.add(bytesOS,Bytes.toBytes("freePhyMemKB"),Bytes.toBytes(String.valueOf(freePhyMemKB)));
       put.add(bytesOS,Bytes.toBytes("freeSwapKB"),Bytes.toBytes(String.valueOf(freeSwapKB)));
       put.add(bytesOS,Bytes.toBytes("totalSwapKB"),Bytes.toBytes(String.valueOf(totalSwapKB)));
       put.add(bytesOS,Bytes.toBytes("phyMemUtil"),Bytes.toBytes(String.valueOf(phyMemUtil)));
       put.add(bytesOS,Bytes.toBytes("swapMemUtil"),Bytes.toBytes(String.valueOf(swapMemUtil)));
       put.add(bytesOS,Bytes.toBytes("cpuUtil"),Bytes.toBytes(String.valueOf(cpuUtil)));

      htable.put(put);
      htable.flushCommits();

       System.out.println(" ---insert one row :  " + rowKey + "---");

   }

 
  //生成行健,行健用Web服务器IP和数据采集的时间来表示,共24位,前12位是IP,后12位是时间,不够的在前面用0填充
  // 例如:IP为10.1.2.122,时间为2012-11-2523:01:08,则行健为01000100212220121125230108
    privatevoid makePartRowKey() {
       String[] splits =this.serverIP.split("[.]");
       for (int i=0;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] ;
   }

  //根据Web服务器IP生成行健的前半部分
    privateString makeRowKey() {
       if (this.partRowKey == null){
          this.makePartRowKey();
       }
       SimpleDateFormat sdf = newSimpleDateFormat("yyyyMMddHHmmss");
       Date date = newDate();
       String formatData =sdf.format(date);
       return this.partRowKey +formatData;
   }
  
  // 以下为GetterSetter
    publicString getServerIP() {
       returnserverIP;
   }

    publicvoid setServerIP(String serverIP) {
       this.serverIP =serverIP;
   }
    public intgetServerPort() {
       returnserverPort;
   }
    publicvoid setServerPort(int serverPort) {
       this.serverPort =serverPort;
   }
    publicString getUserName() {
       returnuserName;
   }
    publicvoid setUserName(String userName) {
       this.userName =userName;
   }
    publicString getPassword() {
       returnpassword;
   }
    publicvoid setPassword(String password) {
       this.password =password;
   }
}


程序运行的控制台打印结果:
..............................
12/11/25 22:58:15 INFOzookeeper.ZooKeeper: Clientenvironment:java.compiler=<NA>
12/11/25 22:58:15 INFOzookeeper.ZooKeeper: Client environment:os.name=Windows7
12/11/25 22:58:15 INFOzookeeper.ZooKeeper: Client environment:os.arch=x86
12/11/25 22:58:15 INFOzookeeper.ZooKeeper: Client environment:os.version=6.1
12/11/25 22:58:15 INFOzookeeper.ZooKeeper: Clientenvironment:user.name=hui.li
12/11/25 22:58:15 INFOzookeeper.ZooKeeper: Clientenvironment:user.home=C:\Users\hui.li
12/11/25 22:58:15 INFOzookeeper.ZooKeeper: Clientenvironment:user.dir=D:\CentOS64\Share\Example
12/11/25 22:58:15 INFOzookeeper.ZooKeeper: Initiating client connection,connectString=192.168.1.201:2181 sessionTimeout=180000watcher=hconnection
12/11/25 22:58:15 INFOzookeeper.ClientCnxn: Opening socket connection to server/192.168.1.201:2181
12/11/2522:58:15 INFO zookeeper.ClientCnxn: Socket connection establishedto server1/192.168.1.201:2181, initiating session
12/11/25 22:58:15 INFOzookeeper.ClientCnxn: Session establishment complete on serverserver1/192.168.1.201:2181, sessionid = 0x3b35ddd5d4001d,negotiated timeout = 180000
 ---successful to create tablejvmMonitor ---
12/11/25 22:58:16 INFOzookeeper.ZooKeeper: Initiating client connection,connectString=192.168.1.201:2181 sessionTimeout=180000watcher=hconnection
12/11/25 22:58:16 INFOzookeeper.ClientCnxn: Opening socket connection to server/192.168.1.201:2181
12/11/25 22:58:16 INFOzookeeper.ClientCnxn: Socket connection established toserver1/192.168.1.201:2181, initiating session
12/11/25 22:58:16 INFOzookeeper.ClientCnxn: Session establishment complete on serverserver1/192.168.1.201:2181, sessionid = 0x3b35ddd5d4001e,negotiated timeout = 180000
 ---successful to connect to192.168.1.107:8686 ---
 --- insert one row : 19216800110720121125225820 ---
 --- insert one row : 19216800110720121125225833 ---
 --- insert one row : 19216800110720121125225846 ---
 --- insert one row : 19216800110720121125225859 ---
 --- insert one row : 19216800110720121125225912 ---
........................

在Hbase shell 下面查看表 jvmMonitor:

hbase(main):041:0> scan'jvmMonitor'  

........................................

19216800110720121126004237      column=memory:heapMaxKB,timestamp=1353861709757, value=506816                          
 19216800110720121126004237     column=memory:heapUsedKB, timestamp=1353861709757, value=76504                          
 19216800110720121126004237     column=memory:heapUtil, timestamp=1353861709757, value=46                              
 19216800110720121126004237     column=memory:nonHeapCommitKB, timestamp=1353861709757, value=84096                      
 19216800110720121126004237     column=memory:nonHeapMaxKB, timestamp=1353861709757, value=229376                       
 19216800110720121126004237     column=memory:nonHeapUsedKB, timestamp=1353861709757, value=83955                       
 19216800110720121126004237     column=memory:nonHeapUtil, timestamp=1353861709757, value=99                            
 19216800110720121126004237     column=os:cpuUtil, timestamp=1353861709757, value=0                                   
 19216800110720121126004237     column=os:freePhyMemKB, timestamp=1353861709757, value=960380                           
 19216800110720121126004237     column=os:freeSwapKB, timestamp=1353861709757, value=4194303                            
 19216800110720121126004237     column=os:phyMemUtil, timestamp=1353861709757, value=78                                
 19216800110720121126004237     column=os:swapMemUtil, timestamp=1353861709757, value=0                                
 19216800110720121126004237     column=os:totalSwapKB, timestamp=1353861709757, value=4194303                           
 19216800110720121126004237     column=os:virtMemCommitKB, timestamp=1353861709757, value=293684                        
 19216800110720121126004237     column=target:serverIP, timestamp=1353861709757,value=192.168.1.107                      
 19216800110720121126004237     column=target:serverPort, timestamp=1353861709757, value=8686                           
 19216800110720121126004250     column=memory:heapCommitKB, timestamp=1353861722751, value=164192                       
 19216800110720121126004250     column=memory:heapMaxKB, timestamp=1353861722751, value=506816                          
 19216800110720121126004250     column=memory:heapUsedKB, timestamp=1353861722751, value=77188                          
 19216800110720121126004250     column=memory:heapUtil, timestamp=1353861722751, value=47                              
 19216800110720121126004250     column=memory:nonHeapCommitKB, timestamp=1353861722751, value=84096                      
 19216800110720121126004250     column=memory:nonHeapMaxKB, timestamp=1353861722751, value=229376                       
 19216800110720121126004250     column=memory:nonHeapUsedKB, timestamp=1353861722751, value=83955                       
 19216800110720121126004250     column=memory:nonHeapUtil, timestamp=1353861722751, value=99                            
 19216800110720121126004250     column=os:cpuUtil, timestamp=1353861722751, value=0                                   
 19216800110720121126004250     column=os:freePhyMemKB, timestamp=1353861722751, value=953164                           
 19216800110720121126004250     column=os:freeSwapKB, timestamp=1353861722751, value=4194303                            
 19216800110720121126004250     column=os:phyMemUtil, timestamp=1353861722751, value=78                                
 19216800110720121126004250     column=os:swapMemUtil, timestamp=1353861722751, value=0                                
 19216800110720121126004250     column=os:totalSwapKB, timestamp=1353861722751, value=4194303                           
 19216800110720121126004250     column=os:virtMemCommitKB, timestamp=1353861722751, value=293684                        
 19216800110720121126004250     column=target:serverIP, timestamp=1353861722751,value=192.168.1.107                      
 19216800110720121126004250     column=target:serverPort, timestamp=1353861722751, value=8686                           
483 row(s) in 7.7500seconds



0 0
原创粉丝点击