Hbase简单操作

 

Hbase是我接触的新东西。项目组也准备使用它开发一个大的服务平台。我也趁机学习学习,先看看Hbase的简单操作方法吧:

虽然Hbase与传统的关系型数据库有很大的不同,但首先建张表还是必须的:

  1. /**  
  2.  * 定义几个常量  
  3.  */   
  4. public   static  HBaseConfiguration conf =  new  HBaseConfiguration();  
  5. static  HTable table =  null ;  
  6. /**  
  7.  * 创建hbase table  
  8.  * @param table  
  9.  * @throws IOException  
  10.  */   
  11. public   static   void  creatTable(String tablename)  throws  IOException {  
  12.     HBaseAdmin admin = new  HBaseAdmin(conf);  
  13.     if  (!admin.tableExists( new  Text(tablename))) {   //判断该表名存不存在
  14.         HTableDescriptor tableDesc = new  HTableDescriptor(tablename);  
  15.         tableDesc.addFamily(new  HColumnDescriptor( "ip:" ));  // 添加表的列族。
  16.         tableDesc.addFamily(new  HColumnDescriptor( "time:" ));  
  17.         tableDesc.addFamily(new  HColumnDescriptor( "type:" ));  
  18.         tableDesc.addFamily(new  HColumnDescriptor( "cookie:" ));  
  19.         //注意这个C列,下面我会简单以此列来说明列存储   
  20.         tableDesc.addFamily(new  HColumnDescriptor( "c:" ));  
  21.         admin.createTable(tableDesc);  //生成表
  22.         System.out.println("table create ok!!!" );  
  23.     } else  {  
  24.         System.out.println("table Already exists" );  
  25.     }  

表建好了就可以插入数据了:

  1. /**  
  2.  * 插入数据  
  3.  * @throws Exception  
  4.  */   
  5. public   static   void  insertData()  throws  Exception{  
  6.     //读取日志文件   
  7.     BufferedReader reader = new  BufferedReader( new  FileReader( "此处为读入的文件名" ));  
  8.     if (table== null )  
  9.         table = new  HTable(conf,  new  Text(tablename));  
  10.     String line;  
  11.     while ((line = reader.readLine()) !=  null ){  //是否读到文件的最后一行
  12.         //这里我就不说了,先前有说明   
  13.         LogAccess log = new  LogAccess(line);  
  14.         //这里我使用time+cookie为row关键字,确保不重复,如果cookie记录有重复,将区别对待,这里暂不多做说明   
  15.         String row = createRow(log.getTime(),log.getCookie());  
  16.           
  17.         long  lockid = table.startUpdate( new  Text(row));  
  18.         if (!log.getIp().equals( "" ) && log.getIp()!= null )  
  19.             table.put(lockid, new  Text( "ip:" ), log.getIp().getBytes());  
  20.         if (!log.getTime().equals( "" ) && log.getTime()!= null )  
  21.             table.put(lockid, new  Text( "time:" ), log.getTime().getBytes());  
  22.         if (!log.getType().equals( "" ) && log.getType()!= null )  
  23.             table.put(lockid, new  Text( "type:" ), log.getType().getBytes());  
  24.         if (!log.getCookie().equals( "" ) && log.getCookie()!= null )  
  25.             table.put(lockid, new  Text( "cookie:" ), log.getCookie().getBytes());  
  26.         //这里要注意,我是往c列中写入了5个字段,你可以想象,我在c列中存入了一个map   
  27.         if (!log.getRegmark().equals( "" ) && log.getRegmark()!= null )  
  28.             table.put(lockid, new  Text( "c:_regmark" ), log.getRegmark().getBytes());  
  29.         if (!log.getRegmark2().equals( "" ) && log.getRegmark2()!= null )  
  30.             table.put(lockid, new  Text( "c:_regmark2" ), log.getRegmark2().getBytes());  
  31.         if (!log.getSendshow().equals( "" ) && log.getSendshow()!= null )  
  32.             table.put(lockid, new  Text( "c:_sendshow" ), log.getSendshow().getBytes());  
  33.         if (!log.getCurrenturl().equals( "" ) && log.getCurrenturl()!= null )  
  34.             table.put(lockid, new  Text( "c:_currenturl" ), log.getCurrenturl().getBytes());  
  35.         if (!log.getAgent().equals( "" ) && log.getAgent()!= null )  
  36.             table.put(lockid, new  Text( "c:_agent" ), log.getAgent().getBytes());  
  37.         //存入数据   
  38.         table.commit(lockid);  
  39.     }  

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值